39

Until now, I have never attempted adding a crontask on my Mac. To my surprise, it threw an error..

[hayek@mac:/www/] 02:33:22 PM: crontab -e                                                                                                         1 .
crontab: no crontab for hayek - using an empty one
crontab: "/usr/bin/vi" exited with status 1

How can I figure out why it's throwing that error?

I'm running Mac OS X Lion 10.7.2

Hayek
  • 1,975
  • 3
  • 21
  • 26
  • 2
    Are you using cron for a specific reason? On OS x it's largely been superseded by launchd – Daniel Beck Nov 20 '11 at 22:48
  • I wanted to use vnstat to track bandwidth usage, which requires a cronjob. I know I could use an alternative app, but it would still be useful to know how to make cron work on Mac, if at all possible. – Hayek Nov 20 '11 at 23:10
  • 1
    Have you tried `crontab file` to install the table from an external file? Or using a different editor like `EDITOR=NANO crontab -e`? – Lri Nov 21 '11 at 06:22
  • @DanielBeck I use cron because it's much quicker to set up a new entry than launchd. – Doug Harris Nov 21 '11 at 18:32
  • What does `crontab -l` show? Are there any other messages in `/var/log/cron.log` or `/var/log/system.log`? – Doug Harris Nov 21 '11 at 18:38
  • @Lri good call! the editor was the issue. It looks like `vi` is the culprit, changing the editor to NANO or even `vim` got cron to work. – Hayek Nov 23 '11 at 23:07
  • Related: [How do I edit crontab files with Vim when I get the error: 'temp file must be edited in place'?](http://vi.stackexchange.com/q/137/467) – kenorb Mar 26 '15 at 00:40
  • Getting this on OS X 10.11 - found [this answer below](https://superuser.com/a/907889/90231) worked best. – RichVel Jul 24 '17 at 06:42

9 Answers9

44

The issue turned out to be vi and nothing to do with cron. Doing export EDITOR=vim fixed it

Journeyman Geek
  • 127,463
  • 52
  • 260
  • 430
Hayek
  • 1,975
  • 3
  • 21
  • 26
  • 2
    Yes i understand you found the issue, and the solution is... what? – Gökhan Barış Aker Oct 16 '12 at 14:02
  • 3
    Try doing `export EDITOR=vim` then giving it a go. – Hayek Oct 16 '12 at 21:56
  • Thanks @Hayek. Sorry for latency, i couldn't responsed as quickly as you did :P. However, running crontab with the almighty 'sudo' solved my problem. – Gökhan Barış Aker Oct 21 '12 at 06:49
  • Glad you got it working. – Hayek Oct 21 '12 at 07:36
  • 1
    I have just had the same issue. Setting `EDITOR=vim` did not fix it -- it just led me to a new error `crontab: temp file must be edited in place`. I much prefer vim, however i edit cronjobs so infrequently that i'm happy enough doing it in `NANO` ... though if someone knows the fix, i'm very interested. – ricardo Mar 10 '13 at 05:38
  • this is not answer at all. one should set 'nobackup nowritebackup' for vim! look at other answers below. IMHO augroup is best option. – vigilancer Dec 15 '17 at 00:21
  • This worked perfectly. – Napster_X Jan 28 '18 at 05:28
  • This worked for me and I was getting `$ crontab -e crontab: no crontab for JayRizzo - using an empty one crontab: subl -w: No such file or directory crontab: "subl -w" exited with status 1` – JayRizzo Feb 26 '19 at 07:07
43

In ~/.vimrc add:

    autocmd filetype crontab setlocal nobackup nowritebackup
user442586
  • 446
  • 5
  • 2
35

On a related issue, if you get the message:

crontab: temp file must be edited in place

Try:

1) Add to .bash_profile

alias crontab="VIM_CRONTAB=true crontab"

2) Add to .vimrc

if $VIM_CRONTAB == "true"
    set nobackup
    set nowritebackup
endif

Source: http://drawohara.com/post/6344279/crontab-temp-file-must-be-edited-in-place

xgMz
  • 561
  • 5
  • 5
  • 3
    This is the correct answer!! – Jay Sep 03 '14 at 02:09
  • 1
    This worked for me and I feel this should be the correct answer. – Shubhamoy Nov 17 '16 at 17:05
  • 1
    For csh or tcsh, use `alias crontab "export VIM_CRONTAB=true; /usr/bin/crontab"` – J.A.Faucett Nov 22 '16 at 19:15
  • 1
    I also think this is a better answer although the question from the OP does not mention the exact error @xgMz described. crontab does not appear to like the vim backup on write/save behavior so disabling it as described here just works. – Shyam Habarakada Mar 22 '17 at 18:08
  • 1
    I just had this issue with vim and cron on macOS Sierra. This is the correct answer! – James McCormac Apr 30 '17 at 09:39
  • I was able to use `:set nobackup nowritebackup` after starting `crontab -e` as an alternative to adding to adding the four lines to my `~/.vimrc`. No need to add an alias into my `~/.bashrc` either. – Sasha Aug 08 '17 at 23:47
8

Your editor on system variable EDITOR is vi and vi itsn't work.

Try:

export EDITOR=nano
2

I had the same problem and followed the advice posted for creating the table:

crontab file

And that created the cron table, and then I was able to run

crontab -e

with vi as the default editor and had no problems. It is as if vi could not save the file, but once created, it could access it. This is consistent with being able to run:

sudo crontab -e 

As a curiosity, the tables are stored in

/usr/lib/cron/tabs/UserName

which can only be read as sudo.

user214155
  • 21
  • 2
0

The best way to diagnose this would be to create a fresh crontab with a simple entry like:

* * * * * /bin/date >> /tmp/cron_output

If that works then the issue is with the specific command you've added. Could you share it with us, and also share the results when you execute it directly from Terminal.app, rather than from a crontab?

tog22
  • 915
  • 3
  • 11
  • 21
  • 1
    Same error with the command you suggested. In terminal, `/bin/date` returns `Wed Nov 23 15:03:49 PST 2011` as expected. – Hayek Nov 23 '11 at 23:04
  • OK, just seen your answer, which explains why you'd get the same error - just thought it might help to diagnose the problem :) – tog22 Nov 24 '11 at 11:55
0

cron is deprecated in favour of launchd.

Lingon is a great little tool for setting up launchd agents; it used to be free but appears to now be $3.

Wikipedia has a good launchd page describing all the keys and the launchctl tool you use to activate/deactivate them.

chrish
  • 910
  • 5
  • 9
0

Adding au BufEnter /private/tmp/crontab.* setl backupcopy=yes to vimrc fixed it for me. See here:

http://vim.wikia.com/wiki/Editing_crontab

mtl
  • 1
-1

I saw this same issue on OSX. The answer is to run as root using the sudo command, e.g.:

sudo crontab -e

I got that idea from Gökhan Barış Aker above.

  • with `sudo` you are going to place cron jobs under root user. The cron files are different for your normal user and the root one. – Timofey Sep 06 '15 at 00:30