MOHAMED
MOHAMED

Reputation: 43558

What is the risk when editing crontab file without the "crontab -e" command?

I developed a script in which I add lines to the crontab file with echo command and I remove lines with sed command.

I do not know the risk of that especially that I find in some web site that we have to edit crontab file with

crontab -e
  1. What is the risk of does not using crontab -e?
  2. Are there a risk that my edit will not taken account in the cron schedule?
  3. Should I restart cron with /etc/init.d/cron restart?

Upvotes: 1

Views: 703

Answers (2)

Yvo Volders
Yvo Volders

Reputation: 61

When editing the crontab file using your regular editor, the schedule will not be updated. The system will still do the scheduled tasks as before the edit. This will drive the administrator mad.

When cron restarts, such as a reboot or a restart of the process, the current crontab files will be read and processes are scheduled as described in the current present files.

I remember sending a 'kill -HUP' or 'kill -15' to the PID of the cron process to force the process flushing the cache and reading the crontab files again.

Upvotes: 1

rici
rici

Reputation: 241891

The main risk to not using crontab to edit or replace cronfiles is that directly writing to the cronfile (including using sed to do so) does not lock the file, and therefore two simultaneous edits could have unpredictable and even disastrous consequences.

You don't have to use crontab -e; the following forms of editing are semi-safe, in the sense that they won't randomly combine two edits. (But they are still unsafe; an edit could be overwritten):

To add lines:

{ crontab -u user -l; echo "$this"; echo "$this_too"; } | crontab -u user -

To delete lines or do more complicated edits:

crontab -u user -l | sed "$my_wonderful_sed_script" | crontab -u user -

In any case, you don't need to restart cron. It will notice the changed modification time of the cronfile.

Upvotes: 2

Related Questions