How To Edit Crontab File
One of the most frequently asked question by Linux newbies or experts alike is how to edit crontab file.
Crontab demon reads the crontab file from user’s home folder. Each command which needs to be executed has to be defined with crontab expression.
Crontab expression has 6 fields, first five filed defined minute, hour, day of the month (1-31), month(1 -12 ), day of the week (0 – 6) i.e. ( 0 Sun, 1 Mon, 2, Tue, 3 Wed, 4 Thu, 5 Fri, 6 Sat, 7 Sun).
Cron jobs are defined one job per line, using Crontab expression. Cron job entry is specified with 6 fields each spearted by single space. First 5 fields are time information i.e. minute, hour, day, month week day and executable.
Following are special rules to define range or to use special characters.
Use a comma to separate multiple values i.e.
- 45 23 * * Fri /app/zehntech/exec/update-value-date.sh # runs every Friday 23:45 PM
Use a hyphen to select a range of values
- 00 0-23/2 * * * /app/zehntech/exec/delete-old-stock.sh # runs every day every other hour
Use an asterisk as a wildcard to include all possible values
- * * * * * /app/zehntech/exec/analyze-web-logs.sh # runs every minutes
Use a comment mark at the starting of a line to specify a comment or a blank line
- # this line is a comment for next cronjob 45 18 * * * /app/zehntech/exec/delete-old-stock.sh # runs every day 18:45
Create or edit crontab file
To start editing a existing crontab file or to create new file , just enter crontab -e, this command will open default crontab file for the user. Crontab uses the default text editor set in the user’s environment.
Verify or list all crontab entries
To list or verify if your changes updates are being saved, you can list all the cron jobs using crontab -l command
Create crontab for other user
To create a crontab file for another user, use this command
crontab -u zehntech ~/crontab
To confirm that a crontab file exists for a user, use the ls -l command in the /var/spool/cron/crontabs or /user/home/crontab directory.