tags:

views:

1648

answers:

9

Does crontab have an argument for creating cronjobs without using the editor (crontab -e). If so, What would be the code create a cronjob from a bash script?

A: 

Create a text file (in this case jobs.cron) with the following format per line for scripts you want to execute;

1 2 3 4 5 bash_script

where

1 - day of the week (0-6, Sunday = 0)
2 - month (1-12)
3 - day of month (1-31)
4 - hour of day (0-23)
5 - minute (0-59)

then run

 crontab jobs.cron
Kirschstein
+1  A: 

Maybe this post will help you programmatically create crontabs

simon622
+5  A: 

You can add to the crontab as follows:

#write out current crontab
crontab -l > mycron
#echo new cron into cron file
echo "00 09 * * 1-5 echo hello" >> mycron
#install new cron file
crontab mycron
rm mycron
dogbane
You should use tempfile or mktemp
Reef
A: 

EDIT (fixed overwriting):

cat <(crontab -l) <(echo "1 2 3 4 5 scripty.sh") | crontab -
duckyflip
This will replace any crontab contents
TheBonsai
@TheBonsai oh I see, I fixed it now so it should APPEND the new command to the existing crontab contents
duckyflip
A: 

No, there is no option in crontab to modify the cron files.

You have to: take the current cron file (crontab -l > newfile), change it and put the new file in place (crontab newfile).

If you are familiar with perl, you can use this module Config::Crontab.

LLP, Andrea

andcoz
+2  A: 

You may be able to do it on-the-fly

crontab -l | { cat; echo "0 0 0 0 0 some entry"; } | crontab -
TheBonsai
A: 

If you're using the Vixie Cron, e.g. on most Linux distributions, you can just put a file in /etc/cron.d with the individual cronjob.

This only works for root of course. If your system supports this you should see several examples in there. (Note the username included in the line, in the same syntax as the old /etc/crontab)

It's a sad misfeature in cron that there is no way to handle this as a regular user, and that so many cron implementations have no way at all to handle this.

chuck
A: 

Are there some systems that don't use the order of crontab fields described here (minute, hour, day of month, month, day of week)? Wikipedia entry I was just surprised to see someone suggesting the exact opposite order.

David
A: 

Chances are you are automating this, and you don't want a single job added twice. In that case use:

CRON="1 2 3 4 5 /root/bin/backup.sh"
cat < (crontab -l) |grep -v "${CRON}" < (echo "${CRON}")
kvz