1

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

/mydata/cronjobs

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
  • 1
    Doesn't CentOS already provide /etc/cron.d for this sort of arrangement?
    – tripleee
    Jul 12, 2013 at 12:31

1 Answer 1

0

How about a script like this?

#!/bin/bash

cronjobs_dir=/mydata/cronjobs
crontab_tmp=/tmp/crontab

crontab -l > "$crontab_tmp"
for i in $mydir/*
do
    cat "$i" >> "$crontab_tmp"
done
crontab "$crontab_tmp"
4
  • I think you mean for i in $mydir/*. Parsing ls output is useless and the ls itself is also useless.
    – tripleee
    Jul 12, 2013 at 12:30
  • Or more succinctly ( crontab -l; cat "$mydir"/* ) | crontab -
    – tripleee
    Jul 12, 2013 at 12:33
  • But then there is no facility for, say, removing one job but keeping the others (programmatically).
    – tripleee
    Jul 12, 2013 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. Jul 12, 2013 at 19:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .