http://stackoverflow.com/questions/878600/how-to-create-cronjob-using-bash/8106460#8106460
Thanks everybody for your help. Piecing together what I found here and elsewhere I came up with this:
command="php $INSTALL/indefero/scripts/gitcron.php"
job="0 0 * * 0 $command"
cat <(fgrep -i -v "$command" <(crontab -l)) <(echo "$job") | crontab -
I couldn't figure out how to eliminate the need for the two variables without repeating myself.
command
is obviously the command I want to schedule. job
takes $command
and adds the scheduling data. I needed both variables separately in the line of code that does the work.
- Credit to duckyflip, I use this little redirect thingy (
<(*command*)
) to turn the output ofcrontab -l
into input for thefgrep
command. fgrep
then filters out any matches of$command
(-v
option), case-insensitive (-i
option).- Again, the little redirect thingy (
<(*command*)
) is used to turn the result back into input for thecat
command. - The
cat
command also receivesecho "$job"
(self explanatory), again, through use of the redirect thingy (<(*command*)
). - So the filtered output from
crontab -l
and the simpleecho "$job"
, combined, are piped ('|') over tocrontab -
to finally be written. - And they all lived happily ever after!
This line of code filters out any cron jobs that match the command, then writes out the remaining cron jobs with the new one, effectively acting like an "add" or "update" function.
To use this, all you have to do is swap out the values for the command
and job
variables.