The simplest way to create a crontab file is to use the crontab -e command to invoke the text editor set up for your system environment, defined by the EDITOR environment variable. If this variable has not been set, crontab uses the default editor ed. Define your EDITOR environment to be an editor you are familiar with. The following example shows how to check to see whether an editor has been defined, and how to set up vi as the default.
$ which $EDITOR $ $ EDITOR=vi $ export EDITOR |
When you create a crontab file, it is automatically placed in the /var/spool/cron/crontabs directory and is given your user name. You can create or edit a crontab file for another user, or root, if you have superuser privileges.
Enter crontab command entries as described in "Syntax of crontab File Entries".
(Optional) Become superuser to create or edit a crontab file belonging to root or another user.
Create a new crontab file, or edit an existing one.
$ crontab -e [username] |
username |
Name of another user's account, requires root privileges to create or edit. |
If you accidentally enter the crontab command with no option, press the interrupt character for your editor. This allows you to quit without saving changes. Exiting the file and saving changes at this point would overwrite an existing crontab file with an empty file.
Add command lines to the file, following the syntax described in "Syntax of crontab File Entries".
The crontab file will be placed in /var/spool/cron/crontabs.
Verify the crontab file by using the crontab -l command.
# crontab -l [username] |
The following example shows how to create a crontab file for another user.
# crontab -e jones |
The following command entry added to a new crontab file will automatically remove any log files from the user's home directory at 1:00 am every Sunday morning. Because the command entry does not redirect output, redirect characters are added to the command line after *.log to make sure that the command executes properly.
# This command helps clean up user accounts. 1 0 * * 0 rm /home/jones/*.log > /dev/null 2>&1 |
To verify that a crontab file exists for a user, use the ls -l command in the /var/spool/cron/crontabs directory. For example, the following display shows that crontab files exist for users smith and jones.
$ ls -l /var/spool/cron/crontabs -rw-r--r-- 1 root sys 190 Feb 26 16:23 adm -rw------- 1 root staff 225 Mar 1 9:19 jones -rw-r--r-- 1 root root 1063 Feb 26 16:23 lp -rw-r--r-- 1 root sys 441 Feb 26 16:25 root -rw------- 1 root staff 60 Mar 1 9:15 smith -rw-r--r-- 1 root sys 308 Feb 26 16:23 sys |
Verify the contents of user's crontab file by using crontab -l as described in "How to Display a crontab File".