Why don't I have crontab login on OS X when using vim?

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 get the crontab -e command to work. It starts vim, I enter the test task:

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

But after saving and shutting down ( :wq ),

 crontab -l 

He speaks:

 No crontab for mollerhoj3 

What am I doing wrong?

+74
vim cron macos
Mar 13 '13 at 20:10
source share
9 answers

Follow these steps:

  • In the terminal: crontab -e .
  • Press i to switch to vim insert mode.
  • Enter your cron job, for example:

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

  • Enter ZZ to exit vim (must be uppercase).
  • You should see the following message: crontab: installing new crontab . You can check the crontab file with crontab -l .

Please note that this may not work depending on the contents of your ~/.vimrc file.

+178
May 24 '13 at 9:48
source share

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

 crontab ~/.crontab 

Worked for me for 20 years through many varieties of unix.

+69
Sep 15 '13 at 21:25
source share

NOTE : the answer, which says that the ZZ command is used, does not work for me on my Mavericks system, but it is probably related to something in my vim configuration, because if I start with an untouched .vimrc , the accepted answer works . My answer may work for you if there is no other solution.

On MacOS X, according to the crontab man page, the temporary crontab file created with crontab -e must be edited in-place. Vim does not edit by default by default (but crontab -e may require a special case to support it), so if your environment variable $EDITOR set to vi (default) or vim , editing crontab will always fail.

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

 :setlocal nowritebackup 

This will allow you to update crontab when you run crontab -e with the commands :wq or ZZ .

You can add an auto command to your .vimrc so that it works automatically when editing crontabs:

 autocmd FileType crontab setlocal nowritebackup 

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

 ""In ~/.vim/after/ftplugin/crontab.vim if has("mac") setlocal nowritebackup endif 
+38
Jan 17 '14 at 19:22
source share

Using cron on OS X is not recommended. Instead, launchd used. Try man launchctl to get started. You must create special XML files that define your tasks and place them in a special place with certain rights.

Usually you just need to find out launchctl load

http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/launchctl.1.html

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

Edit

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

+22
Mar 13 '13 at 20:22
source share

I did 2 things to solve this problem.

  • I touched the crontab file described in this link coderwall.com/p/ry9jwg (Thanks @Andy).
  • Used by Emacs instead of my default vim: EDITOR = emacs crontab -e (I have no idea why vim doesn't work)

crontab -l now prints cronjobs. Now I just need to find out why the cronjob is still not working ;-)

+7
Mar 13 '13 at 23:11
source share

The difference between cron and launchd

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

This is taken from developer.apple.com

The effect of sleep mode and power off

If the system is shut down or asleep, cron jobs are not executed; they will not work until the next appointed time.

If you plan to start the task by setting the StartCalendarInterval key, and the computer sleeps when the task should be started, your task will be executed when the computer wakes up. However, if the machine is turned off when the task is to be completed, the task is not performed until the next scheduled time.

All other startup tasks are skipped when the computer is turned off or asleep; they will not work until the next appointed time.

Therefore, if the computer is always turned off at the scheduled time, both cron jobs and startup jobs never start. For example, if you always turn off your computer at night, the task must be completed within 1 AM will never be started.

+5
Mar 31 '14 at 22:38
source share

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

Correct cron:

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

The user field syntax is for /etc/crontab :

 0-59 * * * * mollerhoj3 echo "Hello World" 
+4
Dec 23 '13 at 15:44
source share

Since the previous posts did not work for me due to some permission problems, I found that I was creating a separate crontab file and adding it to the crontab user with the -u option, and root worked for me.

 sudo crontab -u {USERNAME} ~/{PATH_TO_CRONTAB_FILE} 
0
Sep 13 '16 at 21:14
source share

The above combination of correct answers. What worked for me for the same thing was:

1) edit the bash configuration file

$ cd ~ && & vim.bashrc

2) in the bash configuration file, make sure that the default editor is vim and not vi (which causes the problem)

export EDITOR = vim

3) edit the vim configuration file

$ cd ~ && & vim.vimrc

4) make sure in your .vimrc

yes backup installed.

set backupcopy = yes

5) restart the 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 the following command; you should see a new cron job. Hope this helps.

$ crontab -l

0
Nov 29 '16 at 11:50
source share



All Articles