78

I would like to use cron on my Mac. I choose it over launchd, because I want to be able to use my new knowledge on Linux as well. However, I cannot seem to get the crontab -e command to work. It fires up vim, I enter my test job:

0-59 * * * * mollerhoj3 echo "Hello World"

But after saving and quitting (:wq),

crontab -l

says:

No crontab for mollerhoj3

What am I doing wrong?

7
  • after saving and quitting, does it print out crontab: installing new crontab to the terminal?
    – shx2
    Commented Mar 13, 2013 at 20:12
  • so when quitting the editor you get no output at all?
    – shx2
    Commented Mar 13, 2013 at 20:35
  • Oh, yes sorry for being so cryptic, I getting: crontab: no crontab for mollerhoj3 and the DYLD_ message
    – mollerhoj
    Commented Mar 13, 2013 at 20:59
  • 4
    The reason is probably that your vim is configured not to edit files in place. See stackoverflow.com/a/11043630/200987 for a fix. Basically :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.
    – oligofren
    Commented Aug 15, 2016 at 13:18
  • 1
    Additionally, it could because you are using gvim (OP didn't specify). gvim return the command prompt immediately (if you don't use the -f flag), and therefore crontab doesn't wait for it to finish,
    – Brian
    Commented Feb 6, 2019 at 0:41

14 Answers 14

191

Just follow these steps:

  1. In Terminal: crontab -e.
  2. Press i to go into vim's insert mode.
  3. Type your cron job, for example:

    30 * * * * /usr/bin/curl --silent --compressed http://example.com/crawlink.php
    
  4. Press Esc to exit vim's insert mode.

  5. Type ZZ to exit vim (must be capital letters).
  6. You should see the following message: crontab: installing new crontab. You can verify the crontab file by using crontab -l.

Note however that this might not work depending on the content of your ~/.vimrc file.

11
  • 13
    On the Mac you can set nano as your default editor with: export EDITOR=nano Commented Nov 5, 2013 at 19:39
  • 7
    @JohnHunt You should try :x, fewer character :)
    – Nicklas A.
    Commented Dec 21, 2013 at 0:38
  • 2
    @Nicklas A More complex to type though.. you have to release the shift key half way through.
    – John Hunt
    Commented Dec 23, 2013 at 11:20
  • 2
    Once you have the crontab installed you may still have to create the /etc/crontab file on recent versions of OS X in order to enable the cron daemon. sudo touch /etc/crontab Commented Jan 15, 2014 at 17:32
  • 10
    @NicklasA :x runs the risk of confusion with :X - several times as a noob unix dude I encrypted files and exited with no idea what I entered as the encryption key! ZZ seems much safer :-) Commented Apr 28, 2014 at 10:45
74

I've never had this problem, but I create a ~/.crontab file and edit that (which allows me to back it up, Time Machine or otherwise), then run

crontab ~/.crontab

Has worked for me for 20+ years across many flavors of unix.

3
  • 4
    @Rohmer No, you can't. All that does is modify the currently running set of instructions your cron uses; it doesn't edit the file from which those instructions came. Try it; you'll see that cron is now using your edited instructions, but .crontab has not changed. Commented Jun 26, 2014 at 13:04
  • 6
    This is a little misleading. crontab ~/.crontab just installs the directives in the file. It can be named absolutely anything and it will 'work', but crontab -e will never edit ~/.crontab or whatever file you chose. read man crontab. You can even delete ~/crontab and your crons will run just fine.
    – cmroanirgo
    Commented Feb 1, 2015 at 5:12
  • 1
    I don't see anything misleading here. The answer specifically says to create and edit ~/.crontab. Commented Jun 3, 2022 at 4:00
42

NOTE: the answer that says to use the ZZ command doesn't work for me on my Mavericks system, but this is probably due to something in my vim configuration because if I start with a pristine .vimrc, the accepted answer works. My answer might work for you if the other solution doesn't.

On MacOS X, according to the crontab manpage, the crontab temporary file that gets created with crontab -e needs to be edited in-place. Vim doesn't edit in-place by default (but it might do some special case to support crontab -e), so if your $EDITOR environment variable is set to vi (the default) or vim, editing the crontab will always fail.

To get Vim to edit the file in-place, you need to do:

:setlocal nowritebackup

That should enable you to update the crontab when you do crontab -e with the :wq or ZZ commands.

You can add an autocommand in your .vimrc to make this automatically work when editing crontabs:

autocmd FileType crontab setlocal nowritebackup

Another way is to add the setlocal nowritebackup to ~/.vim/after/ftplugin/crontab.vim, which will be loaded by Vim automatically when you're editing a crontab file if you have the Filetype plugin enabled. You can also check for the OS if you're using your vim files across multiple platforms:

""In ~/.vim/after/ftplugin/crontab.vim
if has("mac")
  setlocal nowritebackup
endif
3
  • @Dave This is not working for me. I keep getting this error message : crontab: "/usr/bin/vi" exited with status 1 Commented Jan 26, 2017 at 16:36
  • I fixed it by changing the editor from vi to vim. I always thought both were the same. Commented Jan 26, 2017 at 16:50
  • I've noticed this happens when I also try to edit other tmp files on OSX with vim as well. Can someone explain what is special about how OSX or vim handles tmp files that need to be modified in place? Reading the documentation on 'writebackup' it simply says vim will make a backup before overwriting a file. If it does eventually overwrite the file I don't understand why it wouldn't work properly.
    – Jeff
    Commented Jun 5, 2017 at 15:49
22

The use of cron on OS X is discouraged. launchd is used instead. Try man launchctl to get started. You have to create special XML files that define your jobs and put them in a special place with certain permissions.

You'll usually just need to figure out launchctl load

https://www.unix.com/man-page/osx/1/launchctl/

http://nb.nathanamy.org/2012/07/schedule-jobs-using-launchd/

Edit

If you really do want to use cron on OS X, check out this answer: https://superuser.com/a/243944/2449

6
  • I cannot find anything that should indicate that cron is not supported on OS X. cron is depricated, yes, but as I mentioned in my question, I have a reason for wanting to get to know cron.
    – mollerhoj
    Commented Mar 13, 2013 at 20:28
  • 1
    If you're trying to learn linux operations on an OS X native platform, consider using something like VirtualBox (open source VM) to install a Linux distro in a VM on your OS X machine so you can play with both at the same time. You'll learn even more seeing the similarities and differences between them. Commented Mar 13, 2013 at 22:16
  • 25
    Bah-Foo-Wee! You lost me at "You have to create special XML files". 1 crontab line is much simpler and it works just fine. I'll learn launchd the day cron on mac is actually dead.
    – Michael M
    Commented Oct 25, 2013 at 7:08
  • 7
    launchd is a nightmare to use IMHO
    – jwl
    Commented Apr 22, 2014 at 14:26
  • Apple link is dead
    – blackjacx
    Commented Aug 19, 2022 at 21:18
8

Difference between cron and launchd

As has been mentioned cron is deprecated (but supported), and launchd is recommended for OS X.

This is taken from developer.apple.com

Effects of Sleeping and Powering Off

If the system is turned off or asleep, cron jobs do not execute; they will not run until the next designated time occurs.

If you schedule a launchd job by setting the StartCalendarInterval key and the computer is asleep when the job should have run, your job will run when the computer wakes up. However, if the machine is off when the job should have run, the job does not execute until the next designated time occurs.

All other launchd jobs are skipped when the computer is turned off or asleep; they will not run until the next designated time occurs.

Consequently, if the computer is always off at the job’s scheduled time, both cron jobs and launchd jobs never run. For example, if you always turn your computer off at night, a job scheduled to run at 1 A.M. will never be run.

0
7

I did 2 things to solve this problem.

  1. I touched the crontab file, described in this link coderwall.com/p/ry9jwg (Thanks @Andy).
  2. Used Emacs instead of my default vim: EDITOR=emacs crontab -e (I have no idea why vim does not work)

crontab -lnow prints the cronjobs. Now I only need to figure out why the cronjobs are still not running ;-)

1
  • 1
    I had no problem editing the crontab with vim (I got the crontab: installing new crontab message), but jobs weren't running. sudo touch /etc/crontab worked for me on Mavericks..
    – sjy
    Commented Mar 28, 2014 at 2:06
4

In user crontab (crontab -e) do not put the user field.

Correct cron is:

0-59 * * * * echo "Hello World"

Syntax with user field is for /etc/crontab only:

0-59 * * * * mollerhoj3 echo "Hello World"
1
  • Correct -- but that doesn't explain the "No crontab for mollerhoj3" message. With the crontab entry in the question, it would attempt to execute a command mollerhoj3 with arguments echo "Hello World" (which would of course fail). Commented Jun 3, 2022 at 3:59
2

The error crontab: temp file must be edited in place is because of the way vim treats backup files.

To use vim with cron, add the following lines in your .bash_profile
export EDITOR=vim
alias crontab="VIM_CRONTAB=true crontab"

Source the file:
source .bash_profile

And then in your .vimrc add:
if $VIM_CRONTAB == "true" set nobackup set nowritebackup endif

This will disable backups when using vim with cron. And you will be able to use crontab -e to add/edit cronjobs.

On successfully saving your cronjob, you will see the message:
crontab: installing new crontab

Source:
http://drawohara.com/post/6344279/crontab-temp-file-must-be-edited-in-placeenter link description here

0
2

Other option is not to use crontab -e at all. Instead I used:

(crontab -l && echo "1 1  * * *  /path/to/my/script.sh") | crontab -

Notice that whatever you print before | crontab - will replace the entire crontab file, so use crontab -l && echo "<your new schedule>" to get the previous content and the new schedule.

1

As the previous posts didn't work for me because of some permissions issues, I found that creating a separate crontab file and adding it to the user's crontab with the -u parameter while root worked for me.

sudo crontab -u {USERNAME} ~/{PATH_TO_CRONTAB_FILE}
1
  • There's no need to use sudo for this. If you're logged into the USERNAME account, just run crontab ~/PATH_TO_CRONTAB_FILE. Commented Jun 3, 2022 at 4:09
0

The above has a mix of correct answers. What worked for me for having the exact same errors are:

1) edit your bash config file

$ cd ~ && vim .bashrc

2) in your bash config file, make sure default editor is vim rather than vi (which causes the problem)

export EDITOR=vim

3) edit your vim config file

$cd ~ && vim .vimrc

4) make sure set backupcopy is yes in your .vimrc

set backupcopy=yes

5) restart terminal

6) now try crontab edit

$ crontab -e

10 * * * * echo "hello world"

You should see that it creates the crontab file correctly. If you exit vim (either ZZ or :wq) and list crontab with following command; you should see the new cron job. Hope this helps.

$ crontab -l

0

Use another text editor

env EDITOR=nano crontab -e 

or

env EDITOR=code crontab -e 
0

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).

-1

@Hanzaplastique answer in this comment worked for me: export EDITOR=nano

2
  • 1
    It shouldn't matter which editor you use. Commented Jun 3, 2022 at 4:03
  • That comment is not an answer to the OP's question. It's a different thing altogether. Commented Sep 12, 2022 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.