4

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.

4
  • 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. Commented Oct 31, 2018 at 19:28
  • without crontab command?
    – Tim
    Commented Oct 31, 2018 at 19:29
  • Without using contrab -e....you can use vi. But I think the ownership and chmods have to be right. Commented Oct 31, 2018 at 19:30
  • And becouse You can does not mean You should. crontab basically checks for syntax errors when You try save the file. Commented Oct 31, 2018 at 23:14

4 Answers 4

5

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.

4
  • For crontab files, I think you do not need a restart.... Commented Oct 31, 2018 at 19:31
  • You could read the source code to check whether cron rereads the crontab file without a message from crontab.
    – schily
    Commented Oct 31, 2018 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 Commented Oct 31, 2018 at 19:40
  • 1
    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
    Commented Nov 1, 2018 at 10:17
0

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

No

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

Yes, but only if you have a user named t, which seems unlikely.

Of course, as others have pointed out, the 'preferred' method is to run crontab. However, this is unnecessary if, for example, you're creating a new image and you already know that the commands are valid. The rules are as follows:

  1. The file should be given the name of an existing user; it will be run on their behalf
  2. The file should be owned by user:crontab, with mode 600 (where user is the name of the user and the name of the file)

If you need confirmation, run crontab -e (after setting up the EDITOR var, ie. export EDITOR=emacs or whatever), and enter your proposed commands. This will create the file with the correct name and permissions; you can just edit this file.

-2

in UBUNTU you can create whatever cron job as root and reloaded this without using crontab -e only put the 2 lines

echo '@reboot /etc/esperandoroute.sh' >> /var/spool/cron/crontabs/root

crontab -u root /var/spool/cron/crontabs/root
 
crontab -l
2
  • What is the point of doing this? You are using essentially all the other options of crontab apart from -r to avoid using crontab -e?
    – Kusalananda
    Commented Dec 17, 2022 at 8:12
  • the point is not use cronrab -e on ubuntu to create a cron job Commented Dec 18, 2022 at 14:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .