(I don't have enough reputation to comment, so I'm adding as an answer: feel free to add it as a comment next to his answer)
Joe Casadonte one -l iner is perfect unless you run with set -e , i.e. if your script is configured to fail on error, and if there are no cronjobs yet. In this case, one iner -l will NOT create a cronjob, but will NOT stop the script. A silent mistake can be very deceiving.
The reason is that crontab -l returns with a return code of 1 , as a result of which the subsequent command ( echo ) is not executed ... thus, a cronjob is not created. But since they execute as a subprocess (due to parentheses), they do not stop the script.
(Interestingly, if you run the same command again, it will work: after you run crontab - once, crontab -l still doesnβt display anything, but does not return an error (you do not get no crontab for <user> more). So the subsequent echo is done and crontab is created)
In any case, if you run with set -e , the line should be:
(crontab -l 2>/dev/null || true; echo "*/5 * * * * /path/to/job -with args") | crontab -
Faria Jul 24 '18 at 11:25 2018-07-24 11:25
source share