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...
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
@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.
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
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.
crontab
typically lifts the value of EDITOR
or VISUAL
(if it is defined), you can set them: EDITOR='VIM_NOBACKUP=1 vim'
.
crontab -e
generates crontab: VIM_NOBACKUP=1 vim: No such file or directory
.