Error adding cronjobs in Mac OS X Lion

37

8

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

Posted 2011-11-20T22:41:28.153

Reputation: 1 527

Getting this on OS X 10.11 - found this answer below worked best.

– RichVel – 2017-07-24T06:42:11.433

2Are you using cron for a specific reason? On OS x it's largely been superseded by launchd – Daniel Beck – 2011-11-20T22:48:41.617

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 – 2011-11-20T23:10:40.327

1Have you tried crontab file to install the table from an external file? Or using a different editor like EDITOR=NANO crontab -e? – Lri – 2011-11-21T06:22:28.527

@DanielBeck I use cron because it's much quicker to set up a new entry than launchd. – Doug Harris – 2011-11-21T18:32:57.250

What does crontab -l show? Are there any other messages in /var/log/cron.log or /var/log/system.log? – Doug Harris – 2011-11-21T18:38:24.820

@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 – 2011-11-23T23:07:59.360

Answers

41

In ~/.vimrc add:

    autocmd filetype crontab setlocal nobackup nowritebackup

user442586

Posted 2011-11-20T22:41:28.153

Reputation: 426

great simple trick! works well for me. – Tri Nguyen – 2017-01-03T17:20:44.570

This is the best answer and works well - much simpler than the one involving setting an env variable in the shell profile as well as editing ~/.vimrc – RichVel – 2017-07-24T06:40:48.643

Marking this as the correct answer since my own reply is no longer appropriate nor the most effective. – Hayek – 2017-12-28T00:31:28.733

1This works in Mac OS High Sierra too! Thanks for posting this – Kearney Taaffe – 2018-01-10T17:10:30.503

44

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

Hayek

Posted 2011-11-20T22:41:28.153

Reputation: 1 527

this is not answer at all. one should set 'nobackup nowritebackup' for vim! look at other answers below. IMHO augroup is best option. – vigilancer – 2017-12-15T00:21:43.020

This worked perfectly. – Napster_X – 2018-01-28T05:28:11.750

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 – 2019-02-26T07:07:59.100

2Yes i understand you found the issue, and the solution is... what? – Gökhan Barış Aker – 2012-10-16T14:02:34.673

3Try doing export EDITOR=vim then giving it a go. – Hayek – 2012-10-16T21:56:07.187

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 – 2012-10-21T06:49:54.193

Glad you got it working. – Hayek – 2012-10-21T07:36:13.980

1I 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 – 2013-03-10T05:38:41.937

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

Posted 2011-11-20T22:41:28.153

Reputation: 561

2This is the correct answer!! – Jacob – 2014-09-03T02:09:49.533

This worked for me and I feel this should be the correct answer. – Shubhamoy – 2016-11-17T17:05:13.267

1For csh or tcsh, use alias crontab "export VIM_CRONTAB=true; /usr/bin/crontab" – J.A.Faucett – 2016-11-22T19:15:52.843

1I 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 – 2017-03-22T18:08:43.250

I just had this issue with vim and cron on macOS Sierra. This is the correct answer! – James McCormac – 2017-04-30T09:39:23.127

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 – 2017-08-08T23:47:26.527

8

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

Try:

export EDITOR=nano

Abdel Karim Mateos Sanchez

Posted 2011-11-20T22:41:28.153

Reputation: 210

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

Posted 2011-11-20T22:41:28.153

Reputation: 21

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

Posted 2011-11-20T22:41:28.153

Reputation: 725

1Same error with the command you suggested. In terminal, /bin/date returns Wed Nov 23 15:03:49 PST 2011 as expected. – Hayek – 2011-11-23T23:04:23.907

OK, just seen your answer, which explains why you'd get the same error - just thought it might help to diagnose the problem :) – tog22 – 2011-11-24T11:55:03.310

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

Posted 2011-11-20T22:41:28.153

Reputation: 826

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

Posted 2011-11-20T22:41:28.153

Reputation: 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.

Nathan Beach

Posted 2011-11-20T22:41:28.153

Reputation: 99

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 – 2015-09-06T00:30:54.893