I want to have a separate folders for cron jobs like:


Now in that folder I want to have files like backup_server which will have the content like:

30 3 * * 1-5 /home/user/scripts/backup.sh 
30 3 * * 1-5 /home/user/scripts/backup2.sh 

Similarly, I want to have more files in that directory for each separate cron job so that I can centralize and separate the cron jobs from one folder.

How can I make root run those jobs for all files in that folder?

  • 1
    Doesn't CentOS already provide /etc/cron.d for this sort of arrangement? – tripleee Jul 12 '13 at 12:31

How about a script like this?



    crontab -l > $crontab_tmp
    for i in $mydir/*
            cat $i >> $crontab_tmp
    crontab $crontab_tmp
  • I think you mean for i in $mydir/*. Parsing ls output is useless and the ls itself is also useless. – tripleee Jul 12 '13 at 12:30
  • Or more succinctly ( crontab -l; cat "$mydir"/* ) | crontab - – tripleee Jul 12 '13 at 12:33
  • But then there is no facility for, say, removing one job but keeping the others (programmatically). – tripleee Jul 12 '13 at 12:34
  • @tripleee You are right, with a little more thinking before posting, I could have optimized out ls, but I didn't know that you can actually use STDIN with crontab. I should have read the manpage. Thank you for pointing this out! "Removing and keeping the others": It's like removing everything and re-adding what's needed. And I assumed that: a, only those files/jobs are in the directory which are actually needed. b, all the files/jobs are there which are needed. – Ferenc Géczi Jul 12 '13 at 19:19

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.