How to quit crontab -e without overwritting cron

Imagine my crontab

* * * * * /usr/bin/date > /tmp/foo

I am writing the date to /tmp/foo every minute
$ cat /tmp/foo
Thu Jul 5 08:45:01 CEST 2012

Now I want to view my crontab in my EDITOR (vi).


$ crontab -e

I do not quit yet.

In the meantime, my colleague modify the crontab.


* * * * * /usr/bin/date > /tmp/bar

Later, I quit vi with :q!

O Surprise, the crontab is * * * * * /usr/bin/date > /tmp/foo again

According to the doc :

When you finish creating entries and exit the file, the crontab command
copies it into the /var/spool/cron/crontabs directory

Even if you did not make change, you overwrite the content of your crontab !

If you want to exit your crontab editor really without overwritting the crontab, you need to kill yourself.

Ex:

CTRL-Z
[2] + Stopped (SIGTSTP) crontab -e
$ kill %2
$
[2] + Stopped (SIGTTOU) crontab -e

Thanks to Colin comment, I realized I could not kill with kill, let’s kill with -9

$ kill -9 %2
$
[2] + Killed crontab -e

6 thoughts on “How to quit crontab -e without overwritting cron

  1. Colin 't Hart

    Has the editor really been killed?
    According to your “screenshot” it’s still in state “Stopped”.
    You’ll probably need to
    kill -9 %2

  2. Laurent Schneider Post author

    Indeed, it is still running lol :

    $ jobs
    [2] + Stopped (SIGTTOU) crontab -e
    [1] – Stopped (SIGTTOU) crontab -e

    I will update the post, thanks

  3. joel garry

    At least on hp-ux, I found out the hard way that commenting out certain perfectly good commands creates a syntax error, so when you write exit, it wipes out the entire file. At this time I’m not feeling up to providing proof with my production system… I do recall it was a backup to tape command I was commenting out, oh the irony. I’ve gotten quite redundant with crontab backups. I don’t use tape anymore.

Comments are closed.