Score:9

Run multi scripts in a folder with cron

us flag
bNi

I'm working with many scripts. So I do add/remove scripts with "crontab -e" so many times. I tried to make an "Available/Enabled" structure for these.

Example:
This is my structure's tree:

# all scripts
scripts/available/1.sh
scripts/available/2.sh
scripts/available/3.sh

# enabled scripts 
scripts/enabled/1.sh -> scripts/available/1.sh
scripts/enabled/2.sh -> scripts/available/2.sh

Then I add this line to "crontab -e":

* * * * * /home/user/scripts/enabled/* &> /dev/null

This is the output of sudo systemctl status cron.service :

     CGroup: /system.slice/cron.service
             ├─17763 /bin/bash /home/user/scripts/enabled/1.sh /home/user/scripts/enabled/2.sh

Finally:
1.sh is working every minute, but 2.sh is not working.

I want to run scripts from only cron. What can I do for this, if I do not want to make a script for "run scripts in a directory" with another script,?

Also do you know any Telegram group for active "GNU/Linux System Administrators"?

Thank you.

Someone avatar
my flag
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer.
bNi avatar
us flag
bNi
Is it okay now ? @Algins
Someone avatar
my flag
You can approve [my edit.](https://askubuntu.com/suggested-edits/174342)
Score:14
mx flag

Alternatively you may use run-parts :

* * * * * /usr/bin/run-parts --regex='.+' /home/user/scripts/enabled

Parameter --regex='.+' allows any filenames. Details: man run-parts.

bNi avatar
us flag
bNi
Not worked for me.
pasman pasmański avatar
mx flag
run-parts by default do not accept a dot in filename. Rename `1.sh` to `1` and so on.
bNi avatar
us flag
bNi
Thanks, its worked !
Score:5
ar flag

Try

for script in /home/user/scripts/enabled/*; do $script &>/dev/null; done
Score:4
cn flag

When you specify

/home/user/scripts/enabled/*

cron sees

/home/user/scripts/enabled/script1.sh /home/user/scripts/enabled/script2.sh ...

Thus, you are executing script1.sh with as arguments the pathnames of the other scripts in that directory. Your script1 does not read arguments: so it runs and it runs fine.

Instead, have cron run a script e expression to "not sure if cron works with wildcards the way you think"that, in turn, runs all scripts in the enabled directory. You can do that with a for ... do loop.

vanadium avatar
cn flag
@terdon correct, of course. I changed this
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.