Bash snippets & functions : Adding a cronjob from script

Dali's clock

Introduction

In this quick post i present some easy and reliable ways of adding a cronjob to a crontab from inside a bash script. I will not present the general concept of a cronjob or even for crontab, there is quite a lot of tutorials out there and even some web site to generate a custom crontab entry from a graphical interface (this one is my favourite).

Feel free to add a comment if you have any good or better way to achieve this task.

 

More “bash snippets & functions” posts

 

 

1 Generalities

Editing a cronjob from inside a script is usually a 3 part process, all connected through a pipe or by using a temporary file. I personally prefer using pipe for security purposes and because it is more reliable and clean (from my point of view).

  1. Listing existent cronjob
    Listing is used to check existent jobs, once you get the list of existent cronjob you may decide to edit, remove or save them. It is usually done with the crontab -l command.
  2. Editing cronjob
    Editing can be used to change an existent cronjob or add a new one. This is usually done by the crontab -e command, but inside a scripting context this usually cannot be done like this, we will have to use a flux redirected (using a pipe) to the crontab stdin.
  3. Saving cronjob
    Saving will apply the edited cronjob to a crontab.

 

In the following examples i will use TestScript.bash as the script to be launched by the cronjob.

 

2 Adding (overwrite) existent cronjob

The following command would replace any existent cronjob(s), so please be careful and make sure you know what you are doing (i very recently did a mistake with that one!).

Using a HERE STRING (i do love that) :

crontab <<<$(echo -n '44 23 * * *  /Admin/bin/TestScript.bash')

Or using a pipe :

echo '44 23 * * *  /Admin/bin/TestScript.bash' |crontab

 

3 Appending to pre-existent cronjob

This command will add the cronjob given in the echo statement without overwriting the already set cronjob.

(crontab -l; echo '44 23 * * *  /Admin/bin/TestScript.bash') |crontab

I did not find the trick to use a HERE STRING here (i did not have enough time, but you can be sure that i will!)

 

4 Adding (overwrite) multi line cronjob

This one can be used to either add a PATH or multiple cronjob at once. PATH can be mandatory as cronjob usually have a very minimalist shell. Please note that the below command will overwrite existing jobs.

(echo -e "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\n44 23 * * *  /Admin/bin/TestScript.bash\n") |crontab

Same as above : i did not find the trick to use a HERE STRING here (i did not have enough time, but you can be sure that i will!)

 

5 Appending a multi line cronjob to pre-existent cronjob

This very last command is a concatenation of the 2 previous ones, which means that it will add a multi line jobs without overwriting existent cronjob.

(crontab -l ; echo -e "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\n55 23 * * *  /Admin/bin/TestScript.bash\n") |crontab

Note the \n combined with the -e option for the echo command : this how you can add a “new-line” character with echo.

Tagged on: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

This site supports SyntaxHighlighter via WP SyntaxHighlighter. It can highlight your code.
How to highlight your code: Paste your code in the comment form, select it and then click the language link button below. This will wrap your code in a <pre> tag and format it when submitted.