There are two ways of editing one's crontab
:
interactively, using crontab -e
, which will open the crontab
in the editor specified by $VISUAL
or $EDITOR
, or
non-interactively, using crontab crontab.txt
, which will simply import the crontab
entries from the file crontab.txt
, replacing the existing active crontab for the current user.
The issue that you have is that you are simply using the crontab
command wrong.
The following concerns non-interactive crontab manipulation:
So, to remove particular tasks programmatically, you could do something like
$ crontab -l | grep -v 'PATTERN' >crontab.txt && crontab crontab.txt
where PATTERN
is a regular expression that will match the task(s) that you'd like to remove. Here, crontab -l
will give you your current crontab
.
Or, if you have entries in a file called crontab-fragment.txt
that you want to remove from the active crontab,
$ crontab -l | grep -v -Fx -f crontab-fragment.txt >crontab.txt && crontab crontab.txt
This reads the current crontab and filters out (removes) any line that also occurs in the file crontab-fragment.txt
in the current directory (using a full line string comparison). The result is saved to crontab.txt
and then loaded from there to replace the current crontab.
To add one or several task, do something like
$ crontab -l | cat - crontab-fragment.txt >crontab.txt && crontab crontab.txt
This is assuming that the file crontab-fragment.txt
contains the entries that you would like to add. It reads the current crontab, appends the entries from crontab-fragment.txt
to this and creates crontab.txt
. The crontab.txt
file then replaces the current crontab.