12

How do I edit crontab files in FreeBSD?

$ crontab -e
# ... I do my thing in vim & :wq

"crontab.9ZcXiFaawt" 6L, 203C written
crontab: temp file must be edited in place
Exit 1

Using EDITOR=nvi works fine... Linux also seems to work fine...

19

The problem is how Vim writes files. By default, it makes a copy of the file and overwrite the original one.

You can observe this with:

# Show the file's inode
$ ls -i a
3156153 a

# Open file in Vim, :wq

# inode changed!
$ ls -i a
3155322 a

crontab gets confused by this (see :help crontab)

You need to use :set backupcopy to yes to make Vim overwrite the original file:

"yes"   make a copy of the file and overwrite the original one

You can do this automatically for crontab files with:

au FileType crontab setlocal bkc=yes
2

@Martin's self-answer is correct. A variation though, for people like myself who try to limit populating their vimrc file with occasional exceptions -- here editing a crontab, is to use modelines. This allows to place the exceptions directly in the target file.

Below is my crontab modeline:

# vim: nu et tw=130 ts=8 sts=4 sw=4 ff=unix fo-=l fo+=tcroq2 bkc=yes

The important bit here is the bkc=yes which matches @Martin's vimrc configuration. The rest are personal convenience settings.

This said for completeness and food for thought, @Martin's initial answer is the most adequate in the majority of cases.

1

Not the same error but also happening quite often:

Error detected while processing /root/.vimrc:
line    2:
E319: Sorry, the command is not available in this version: syntax enable
line    7:
...

This error will show up, if you use vi instead of vim for crontab. You need to set the editor of crontab by adding the following code in your ~/.bashrc file:

export EDITOR=vim
-1

The same happens on OSX. After investigating the problem, I've found it works with plain vi, but it doesn't work with vim.

So the fix could be:

EDITOR=vi crontab -e

However vi is usually linked to vim (e.g. on OSX) so it's the same thing.


This can be solved by the following line in your vimrc file:

au BufNewFile,BufRead crontab.* set nobackup | set nowritebackup

There is another workaround for this by adding the following lines to .vimrc file:

if $VIM_NOBACKUP
  set nobackup
  set nowritebackup
endif

and run the command:

VIM_NOBACKUP=1 crontab -e

You may also try to define the following variable:

EDITOR='VIM_NOBACKUP=1 vim'

Source: crontab: temp file must be edited in place


The reason what this is happening has to do with the way vim/vi treats backup files.

  • 1
    Since crontab typically lifts the value of EDITOR or VISUAL (if it is defined), you can set them: EDITOR='VIM_NOBACKUP=1 vim'. – muru Mar 26 '15 at 7:56
  • @muru I've tried, but I don't think it works. crontab -e generates crontab: VIM_NOBACKUP=1 vim: No such file or directory. – kenorb Mar 26 '15 at 14:07
  • 1
    Must be an OSX thing, because it works on Arch Linux and Ubuntu, bash and zsh. – muru Mar 26 '15 at 14:10
  • 2
    All the mucking about with environment variables seems silly to me; Vim has a way to deal with file-specific settings, and it's called autocmds. It looks like you just copied this from the link, but it seems that the author of that is not very familiar with Vim. – Martin Tournoij Mar 26 '15 at 14:58
  • 2
    Why include it at all? It's a silly way of doing things, and there's almost never a reason to not use an autocmd. When we remove that, we are left with just the autocmd, which is the same answer which has been here for over a month and a half. This answer adds nothing but noise. – Martin Tournoij Mar 26 '15 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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