I have a shell script which, when run as root, performs various tasks to prepare a Debian (9/stretch) server for running a web application. Amongst the tasks that the script does is append cronjob lines to the crontab files for root
and www-data
(in /var/spool/cron/crontabs/
), using cat
and heredoc text.
Each cronjob that is added to the file is enclosed by marker comments, so that when using the uninstall function of the script, these cronjobs can be found and removed from the crontab files using sed
.
This seems to be working OK, although I have now noticed that when reviewing each of the crontabs via crontab -l
the first 3 lines of the crontab do not appear, although they do still exist when checking the actual crontab file directly. Some research has revealed that this is a side-effect of an intentional feature in Debian's crontab implementation, which hides the first 3 lines of a crontab as it expects those lines to be a 3 line "DO NOT EDIT THIS FILE" header.
However, if I am appending to a previously non-existent crontab file, this header does not exist and so does not get created, which is why the real first 3 lines of the crontab are hidden instead.
I am probably not doing the right thing by writing to a crontab file directly in any case. How can I update my script so that it can automatically add to and remove from crontabs in a way which keeps the system happy?
(I see from the man page that there is a CRONTAB_NOHEADER
which can be set to N in order to not hide the 3 lines.)