Backend / DevOps / Architect
Brit by birth,
located worldwide

All content © Alex Shepherd 2008-2024
unless otherwise noted

Slackware/Vim crontab not saving fix.

Published
1 min read
image
Image Credit: Unknown (if this is your work, reach out to me and I'll credit you!)

Hi folks!

Off the electronics and back to Linux for a second... I was trying to reconfigure root's crontab on one of my Slackware servers, and found that when using Vim, it wouldn't save. I couldn't see any errors printed to the screen, but it silently failed, and left me with my old crontab in place. What seemed odd, was that on an Ubuntu server, it worked absolutely fine, and when using vi as the crontab editor (export EDITOR=vi && export VISUAL=vi), it also worked fine on the Slackware box. Now while vi deserves a lot of respect for being the basis for Vim, it's also a total and utter cow to use, so it wasn't an option to revert to vi.

After a little research, the problem turned out to be thanks to Slackware's vimrc. It's set to save a backup containing the file's previous state whenever saved (and this has saved my day numerous times!). However this causes problems with crontab -e. This is described in Vim's help on crontab (:help crontab) as follows:

One situation where "no" and "auto" will cause problems: A program
that opens a file, invokes Vim to edit that file, and then tests if
the open file was changed (through the file descriptor) will check the
backup file instead of the newly created file. "crontab -e" is an
example.

When a copy is made, the original file is truncated and then filled
with the new text. This means that protection bits, owner and
symbolic links of the original file are unmodified. The backup file
however, is a new file, owned by the user who edited the file. The
group of the backup is set to the group of the original file. If this
fails, the protection bits for the group are made the same as for
others.

When the file is renamed this is the other way around: The backup has
the same attributes of the original file, and the newly written file
is owned by the current user. When the file was a (hard/symbolic)
link, the new file will not! That's why the "auto" value doesn't
rename when the file is a link. The owner and group of the newly

written file will be set to the same ones as the original file, but
the system may refuse to do this. In that case the "auto" value will
again not rename the file.

To turn off the backup file function, you can either change your vimrc if you want a permanent change, or once you're editing the file, use the following commands (press : first)...

set nobackup
set nowritebackup

Once you've done this, you should see that the problem goes away nicely!

n00b