DISCLAIMER: I am not a Mac user. However, I have used crontab a great deal on Linux and similar systems. I presume that crontab on Mac works similarly.
A number of answers and comments suggest using an editor other than vim. That's fine, but there's no good reason that vim shouldn't work.
The cron
system maintains a crontab for each user. The location of that file is unimportant; only the crontab
implementation needs to know about it. cron
provides an interface that allows users to request updates to their own crontabs.
The crontab -e
command performs the following steps:
- Copy the user's crontab to a temporary file.
- Invoke the editor (
$VISUAL
or $EDITOR
) on that temporary file.
- When the editor command terminates, copy the temporary file back to the user's crontab (after doing some sanity checks).
- Reload the user's crontab.
The vim
editor, when you use it to edit a file, usually creates a new file with the same name. This shouldn't matter. The crontab -e
command creates a temporary file, waits for the user to edit it, and then re-reads the temporary file by name. It doesn't keep the temporary file open while the user is editing it. If the Mac crontab implementation does this, I would argue that it's buggy. I'll also note that the original poster did not mention the "crontab: temp file must be edited in place" message; that was mentioned in a comment.
The most plausible explanation I can think of is that the OP was using gvim
or something similar. gvim
is a GUI version of vim
, and if you invoke it from a shell prompt you'll get a new prompt immediately; it launches a GUI editor that runs in the background. If you use crontab -e
and it tries to use gvim
to edit the temporary file, then crontab
will see the editor command terminating immediately, and it won't see any updates.
In fact, I was able to reproduce the problem by invoking
VISUAL=gvim EDITOR=gvim crontab -e
on a Linux system. (Again, I'm not a Mac user, and there might be some Mac-specific weirdness that I'm missing.)
A number of other answers and comments suggest using a different editor. That shouldn't be necessary. Any correctly working text editor should work with crontab -e
-- as long as the editor command doesn't terminate until after the temporary file is updated. I've always had vim (or vi or nvi) as my default editor, and I've never had a problem using it with crontab.
The crontab line in the question:
0-59 * * * * mollerhoj3 echo "Hello World"
appears to be intended for a system crontab The first 5 fields specify the schedule, and the 6th is the user name. A normal user crontab does not have this 6th field, since the crontab
command keeps track of the owner (the account that ran the crontab
command). 99% of the time, you don't need to worry about system crontabs. Even root
can use the crontab
command to install a user crontab for the root
account. However, this alone doesn't explain the problem seen by the OP; the syntax is valid for a user crontab, but it will attempt to invoke a command called mollerhoj3
with arguments echo "Hello World"
. That will cause an error when the command is scheduled, not when the crontab is created or updated.
Finally, a bit of advice that doesn't address the original question. Using crontab -e
can be slightly dangerous, since it's easy to damage or destroy your crontab without meaning to. Instead, I suggest keeping a file in your home directory (possibly maintained in a version control system) and applying the contents of that file using crontab filename
. For example, you might create and edit $HOME/.crontab
, and run crontab $HOME/.crontab
after updating it.
The question is over 9 years old, but the original poster is still more or less active (but may not have the same system).
crontab: installing new crontab
to the terminal?crontab: no crontab for mollerhoj3
and the DYLD_ message:set backupcopy=yes
. My MacVim does not work with crontab for this reason, but I saw it due to crontab complaining after saving:crontab: temp file must be edited in place
.