Edit a user crontab safely

Over time a user crontab file can become a complex and vital part of a GNU/Linux system.

If, while editing a crontab file, any issues occur it could be lost and may not be easily recoverable.

Using the following procedure can help to avoid disaster:

Use the -l switch to list the current file and redirect this to a text file

crontab -l > current-crontab

Edit this text file. eg:

vim current-crontab

This text file is now a backup of the crontab for later reference.

The file will contain lines at the top containing a warning etc. These lines can be removed, or left to indicate an update history as they contain a date and time that the crontab was last updated:

  # DO NOT EDIT THIS FILE - edit the master and reinstall.
  # (current-crontab installed on Sun Feb 25 09:53:24 2007)
  # (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)

  # Perform backup at 2am every day
  00 02 * * * /usr/local/bin/backup.sh

  # Backup via Rsync the firefox bookmarks files
  46 01 * * * /usr/local/bin/rsync-bookmarks.sh

Once the current-crontab text file has been edited we need to install this file as the new crontab. This is done as:

crontab current-crontab

To confirm the changes have been applied run the list option (-l) again:

crontab -l
Back to GNU/Linux Command Line Tips