Comment posted How to add cron jobs from command line or bash scripts / Add crontab jobs in a script by .
Recent comments by
Tags: bash scripts, bash shell script, blog, classical example, cron, cron job, cron jobs, crontab, definitions, dev, echo, echo 5, echo command, exact code, file, fileThe, gurus, irc, jambo, kind, line, Linux, mambo jambo, mean time, minute, nbsp nbsp nbsp nbsp nbsp, nickname, ovewrite, pipe, pitfall, root, scripting, Shell, solution, someone, Thanksfully, time, use, way
Mozilla/5.0 (Windows NT 6.0; rv:5.0) Gecko/20100101 Firefox/5.0
There is a better way to edit crontab jobs. Just append your commands to /var/spool/cron/ file where is the user which cron jobs you are going to modify. Make sure your cron user is allowed to wrtie to that file (by default it belongs only to root).
View CommentView CommentMozilla/5.0 (Windows NT 6.0; rv:5.0) Gecko/20100101 Firefox/5.0
I meant /var/spool/cron/username file (wordpress engine suddenly does not allow to insert text in ‘less-than’ and ‘more-than’ braces)
View CommentView CommentMozilla/5.0 (X11; U; Linux x86_64; en-us) AppleWebKit/531.2+ (KHTML, like Gecko) Version/5.0 Safari/531.2+ Debian/squeeze (2.30.6-1) Epiphany/2.30.6
Thanks a lot,
This tip is helpful. I might use this one as well it’s more readable kind of way for sure. Though it will require root user 😉
Hope to see you around,
Best!
View CommentView CommentGeorgi
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2
Thanks 🙂
View CommentView CommentMozilla/5.0 (Windows NT 5.1) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.152 Safari/535.19
Thanks so much. This helped me a lot. How would you write the script in reverse to remove the same cron job?
View CommentView CommentMozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3
Write the script in reverse? wdym?
View CommentView CommentMozilla/5.0 (X11; Linux x86_64; rv:13.0) Gecko/20100101 Firefox/13.0.1
I was recently wondering the same thing as I’m automating a bunch of systems and need to add quite a few cronjobs and did not want to overwrite anything they currently had. So I found this page. The method I used however was this (using your example cronjob):
crontab -l|sed ‘$a*/5 * * * * /root/myscript.sh’|crontab –
In this way, you obtain the previous values using crontab -l and append */5 * * * * /root/myscript.sh to the entries using sed and then pipe the entirety back into crontab in one fell swoop without having to mess with temporary files.
The best part, is this can be modified easily to alter cronjobs for different users (if you have permission) using the -u flag for crontab. Like so:
crontab -u john -l|sed ‘$a*/5 * * * * /root/myscript.sh’|crontab -u john –
View CommentView CommentMozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36
Cool .. thats awesom.
View CommentView CommentMozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36
Alternative way to add to crontab user with existing cron jobs is to use something like:
crontab -l | { cat; echo “0 0 1 * * rsync -qt rsync://rsync.blitzed.org/countries/zz.countries.nerd.dk.rbldnsd \
View CommentView Comment/usr/local/etc/powerdns/zz.countries.nerd.dk.rbldnsd && \
/usr/bin/pdns_control rediscover > /dev/null”; } | crontab –