How to add cron jobs from command line or bash scripts / Add crontab jobs in a script

Saturday, 9th July 2011

I’m currently writting a script which is supposed to be adding new crontab jobs and do a bunch of other mambo jambo.

By so far I’ve been aware of only one way to add a cronjob non-interactively like so:

                  linux:~# echo '*/5 * * * * /root/myscript.sh' | crontab -

Though using the | crontab – would work it has one major pitfall, I did completely forgot | crontab – OVERWRITES CURRENT CRONTAB! with the crontab passed by with the echo command.
One must be extremely careful if he decides to use the above example as you might loose your crontab definitions permanently!

Thanksfully it seems there is another way to add crontabs non interactively via a script, as I couldn’t find any good blog which explained something different from the classical example with pipe to crontab –, I dropped by in the good old irc.freenode.net to consult the bash gurus there 😉

So I entered irc and asked the question how can I add a crontab via bash shell script without overwritting my old existing crontab definitions less than a minute later one guy with a nickname geirha was kind enough to explain me how to get around the annoying overwridding.

The solution to the ovewrite was expected, first you use crontab to dump current crontab lines to a file and then you append the new cron job as a new record in the file and finally you ask the crontab program to read and insert the crontab definitions from the newly created files.
So here is the exact code one could run inside a script to include new crontab jobs, next to the already present ones:

linux:~# crontab -l > file; echo '*/5 * * * * /root/myscript.sh >/dev/null 2>&1' >> file; crontab file

The above definition as you could read would make the new record of */5 * * * * /root/myscript.sh >/dev/null be added next to the existing crontab scheduled jobs.

Now I’ll continue with my scripting, in the mean time I hope this will be of use to someone out there 😉

Share this on:

Download PDFDownload PDF

Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

9 Responses to “How to add cron jobs from command line or bash scripts / Add crontab jobs in a script”

  1. Wellwet says:
    Firefox 5.0 Firefox 5.0 Windows Vista Windows Vista
    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 Comment
    • Wellwet says:
      Firefox 5.0 Firefox 5.0 Windows Vista Windows Vista
      Mozilla/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 Comment
      • admin says:
        Epiphany 2.30.6 Epiphany 2.30.6 Debian GNU/Linux x64 Debian GNU/Linux x64
        Mozilla/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!
        Georgi

        View CommentView Comment
  2. Chris says:
    Google Chrome 15.0.874.121 Google Chrome 15.0.874.121 Windows 7 x64 Edition Windows 7 x64 Edition
    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 Comment
  3. Seth Fuller says:
    Google Chrome 18.0.1025.152 Google Chrome 18.0.1025.152 Windows XP Windows XP
    Mozilla/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 Comment
  4. John says:
    Firefox 13.0.1 Firefox 13.0.1 GNU/Linux x64 GNU/Linux x64
    Mozilla/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 Comment
  5. Dilip says:
    Google Chrome 35.0.1916.153 Google Chrome 35.0.1916.153 Windows 7 x64 Edition Windows 7 x64 Edition
    Mozilla/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 Comment
  6. admin says:
    Google Chrome 40.0.2214.115 Google Chrome 40.0.2214.115 Windows 7 x64 Edition Windows 7 x64 Edition
    Mozilla/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 \
    /usr/local/etc/powerdns/zz.countries.nerd.dk.rbldnsd && \
    /usr/bin/pdns_control rediscover > /dev/null”; } | crontab –

    View CommentView Comment

Leave a Reply

CommentLuv badge