0

I heard that /etc/crontab and /etc/cron.d/* can only be edited manually without crontab command from How would you edit `/etc/crontab` and files under `/etc/cron.d/?.

Do I need to run crontab -e to create and edit a user-specific crontab file under /var/spool/cron/crontabs/?

Can I just manually create and edit a crontab file /var/spool/cron/crontabs/t?

Does crontab -e do some work to make cron daemon know and load the user-specific crontab file, which manually creation and editing fail to do?

Thanks.

  • You can create the files as long as you are root, and you have got to have the user t, I think. Beware of the right permissions. – Rui F Ribeiro Oct 31 '18 at 19:28
  • without crontab command? – Tim Oct 31 '18 at 19:29
  • Without using contrab -e....you can use vi. But I think the ownership and chmods have to be right. – Rui F Ribeiro Oct 31 '18 at 19:30
  • And becouse You can does not mean You should. crontab basically checks for syntax errors when You try save the file. – Piotr Sawicki Oct 31 '18 at 23:14
3

With Vixie Cron, they're just ordinary files — as long as you get the permissions right, you can edit them as you wish. Cron will notice the modified files and reload the crontab (it checks once per minute). This is all actually documented in the cron manpage "NOTES" section, at least on Debian.

But you really shouldn't.

First off, you really don't need to: you can just pass a file to install as a crontab to the crontab program: crontab -u bob FILE will install FILE as Bob's crontab. And FILE can be - to use stdin. If you want to script a crontab change, you can use crontab -l -u bob to list the crontab, edit that, and then load it back. You could, for example, do this (untested) to make sure your term as root is short-lived:

#!/bin/bash

while read -r -u 9 user; do
    {
        crontab -l -u "$user"
        printf '%s\n' '* * * * * fortune -o | mail -s "DegradedArray event on /dev/md0" root'
    } | crontab -u "$user" -
done 9< <(getent passwd | cut -d: -f1)

Second, a good reason not to was hinted at above: that is documented to apply to Debian's cron. But there are a lot of different Crons. RHEL, for example, uses a different one. Arch uses systemd timers by default (not sure if it uses the systemd crontab-to-timer bridge or not), but gives you the choice of 5 different implementations to pick from if you want an actual Cron. Using crontab to install the crontab will work regardless, or at least fail with an error message so you know it didn't work. It's far more portable.

0

Besides the fact that /var/spool/cron/crontabs/ is only writable by root for security reasons crontab informs the cron process via the named pipe /etc/cron.d/FIFO.

So if you are root and add or modify crontab entries you would need to wait until cron is restarted to make your changes active.

  • For crontab files, I think you do not need a restart.... – Rui F Ribeiro Oct 31 '18 at 19:31
  • You could read the source code to check whether cron rereads the crontab file without a message from crontab. – schily Oct 31 '18 at 19:34
  • 1
    "cron checks each minute to see if its spool directory's modtime (or the modtime on the /etc/crontab file) has changed, and if it has, cron will then examine the modtime on all crontabs files and reload those which have changed." -- cron manpage, debian – user4556274 Oct 31 '18 at 19:40
  • You are mistaken, cron runs a pselect() on the named pipe /etc/cron.d/FIFO with a timeout that is computed from the time of the next crontab or at action. See file cron.c line 2792. – schily Nov 1 '18 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.