How do I add an entry to my crontab?
You can't use crontab
like that. Use man crontab
to read about the correct way of calling this utility.
You'll want to use crontab -e
to edit the current user's cron entries (you can add/modify/remove lines). Use crontab -l
to see the current list of configured tasks.
As for seeing other user's crontabs, that's not possible without being root on default installations. See How do I list all cron jobs for all users for some ways to list everything (as root).
Note: be very careful when you use shell globbing characters on the command line (*
and ?
especially). *
will be expanded to the list of files in the current directory, which can have unexpected effects. If you want to pass *
as an argument to something, quote it ('*'
).
There are two ways of editing one's crontab
:
interactively, using
crontab -e
, which will open thecrontab
in the editor specified by$VISUAL
or$EDITOR
, ornon-interactively, using
crontab crontab.txt
, which will simply import thecrontab
entries from the filecrontab.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.
If you want to modify the crontab interactively, run the command crontab -e
, with no other option. This will start an editor on a copy of the crontab; when you exit the editor, the edited copy will become the new crontab. You can control which editor is started through the VISUAL
and EDITOR
environment variables. To list your crontab, run crontab -l
.
If you want to modify the crontab in a script, set VISUAL
and EDITOR
to the path to a script or to a shell snippet that modifies the supplied file in place. The editor ed
is a possibility here, or sed -i
if your implementation of sed
has this option. If you want to unconditionally add a line, you can use echo … >>
. Take care with quoting; if at all in doubt, write a script and pass the name of the script as EDITOR
.
script=$(mktemp)
cat <<'EOF' >"$script"
#!/bin/sh
ed -s "$1" <<'EOS'
g/^ *[^= ][^ =]* *[^= ][^ =]* *[^= ][^ =]* *[^= ][^ =]* *[^= ][^ =]* *echo "hi"$/d
$a
* * * * * echo "hi"
.
w
q
EOS
EOF