Why I've got no crontab entry on OS X when using vim?

前端 未结 12 1753
执念已碎
执念已碎 2020-12-07 07:41

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

相关标签:
12条回答
  • 2020-12-07 08:10

    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 讨论(0)
  • 2020-12-07 08:12

    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.

    0 讨论(0)
  • 2020-12-07 08:15

    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.

    0 讨论(0)
  • 2020-12-07 08:16

    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
    
    0 讨论(0)
  • 2020-12-07 08:17

    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"
    
    0 讨论(0)
  • 2020-12-07 08:19

    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 讨论(0)
提交回复
热议问题