9

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.

3
  • 1
    Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer.
    – Someone
    Nov 20, 2021 at 15:42
  • Is it okay now ? @Algins
    – bNi
    Nov 20, 2021 at 15:47
  • You can approve my edit.
    – Someone
    Nov 20, 2021 at 15:53

3 Answers 3

13

Alternatively you may use run-parts :

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

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

3
  • Not worked for me.
    – bNi
    Nov 20, 2021 at 16:09
  • 2
    run-parts by default do not accept a dot in filename. Rename 1.sh to 1 and so on. Nov 20, 2021 at 16:35
  • Thanks, its worked !
    – bNi
    Nov 20, 2021 at 22:39
5

Try

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

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.

1
  • @terdon correct, of course. I changed this
    – vanadium
    Nov 21, 2021 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.