I'm building a script for my ubuntu server. the script creates a lot of files which works just fine, but now i come to a part where i need to add a line to the crontab of the root user.

If I do it manually i would type sudo crontab -e -u root and edit it crontab, but i want to automate it for this server. In my script i use cat <<'EOF' > /sample/of/a/location.php to create and fill files.

how do i do this for the crontab of the root user? I've read that the location is /var/spool/cron/crontabs/ but i am not supposed to edit them directly.

Can somebody help me out here?

Greetings, Lennart

  • 1
    On Stack Overflow: How can I programmatically create a new cron job? – pa4080 Oct 9 '18 at 18:13
  • Also: askubuntu.com/q/1080045/504066 – PerlDuck Oct 9 '18 at 18:21
  • Ok, so after some testing the only thing i got working (i have zero linux knowledge) is (crontab -l -u root ; echo "#0 20 * * * php /user/bin/scripts/mailqueue.php") | sort - | uniq - | crontab - This does seem to work but it doesnt append. it just replaces it completely with this entry. also if i edit the crontab it says "File: /tmp/crontab.ZwVMiq/crontab". tis /tmp/ part gives me the feeling its not the right file? – Lennart Giaccotto Oct 10 '18 at 18:47
  • Don't worry, that's fine: when you do crontab -e then the real crontab is copied to a temporary file (like your /tmp/crontab.ZwVMiq/crontab) and the editor is invoked on that file. When you leave the editor, that temporary file is copied back over to the real crontab file (/var/spool/cron/crontabs/$user). So that's normal and expected behaviour. – PerlDuck Oct 12 '18 at 18:17

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.