Table of Contents
Feel free to add a comment if you have any good or better way to achieve this task.
More “bash snippets & functions” posts
- Bash snippets & functions : Make a variable readable/reachable from outside a pipe + loop (AKA process substitution)
- Bash snippets & functions : Testing a command return code
- Bash snippets & functions : Find the greater / lower number
- Bash snippets & functions : Yes / No question
- Bash snippets & functions
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).
- 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
- Editing cronjob
Editing can be used to change an existent cronjob or add a new one. This is usually done by the
crontab -ecommand, 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
- Saving cronjob
Saving will apply the edited cronjob to a
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
\n combined with the
-e option for the
echo command : this how you can add a “new-line” character with echo.
- Base de données : De l’algèbre relationnel au SQL (LMD)
- NFS, the Network File System : Practical examples