The purpose of the crontab command is to submit, edit, list, or remove cron jobs.
Grammar
crontab [-E [UserName] |-l [USERNAME] |-r [UserName] |-v [USERNAME] | File]
Describe
The crontab command submits, edits, lists, or removes cron jobs. A cron job is a command that is run by the Cron daemon at the scheduled interval of the rule. To submit a cron job, specify the crontab command with the-e flag. The crontab command invokes an edit session that allows you to create a crontab file. In this file, create an entry for each cron. Each entry must be a format acceptable to the cron daemon. For information on creating an entry, refer to the Crontab file entry format.
After creating the entry and exiting the file, the crontab command copies it to the/var/spool/cron/crontabs directory and places it in a file whose name is the current user name. If a file named after the user name already exists in the Crontabs directory, the crontab command overrides it.
Similarly, you can create a crontab file by specifying a file parameter. If this file already exists, the format must be required by the cron daemon. If the file does not exist, the crontab command invokes the editor. If the editor environment variable exists, this command invokes the editor it specifies. Otherwise, the crontab command calls the VI editor.
To list the contents of a crontab file, specify the crontab command and use the-l flag. To remove a file that exists, use the-R flag.
The optional UserName parameter can be used by the owner of the crontab file or by the root user to edit, list, remove, or verify the status of the cron job for the specified user. If the UserName is invalid, an error message is generated and the program exits. If the optional UserName parameter is not specified, the CRONTAB flag is available for the root user and the current user.
Security
Only the root user and the owner of the crontab file can use UserName after the-e,-L,-R, and-V flags to edit, list, drop, or verify the crontab file for the specified user.
Cron Daemon
The cron daemon runs the command and crontab file entries that correspond. If you do not redirect the output of a cron job to a standard output or standard error, the cron daemon will notify you by email of any command output or error. If an incorrect cron job is specified in the crontab file, the cron daemon does not run the job.
The cron daemon checks the crontab file only when the cron daemon is initialized. When the crontab file is changed with the crontab command, the message indicating the change is sent to the cron daemon. This eliminates the overhead of checking for new or changed files at the scheduled interval of the rule.
Control using the crontab command
The/var/adm/cron/cron.allow and/var/adm/cron/cron.deny files control which users can use the crontab command. The root user can create, edit, or delete these files. The entries in these files are user logins, one name per line. If the login ID and multiple login names are associated, the crontab command takes the first login name in the/etc/passwd file, regardless of which login name is currently used. Also, to allow a user to start a cron job, you should use the Chuser command to set the Daemon property in the/etc/security/user file to TRUE.
The following is an example of a Cron.allow file:
Root, Nick Dee, Sarah.
If the Cron.allow file exists, only the user whose login name appears in the file can use the crontab command. The login name of the root user must appear in the Cron.allow file if the file exists. The system administrator can explicitly stop a user by using the crontab command while listing the user's login name in the Cron.deny file. If only the Cron.deny file exists, the crontab command can be used by any user who does not have a name that appears in the file.
Users will not be able to use the crontab command if one of the following conditions is true:
Cron.allow files and Cron.deny files do not exist (only allow root users).
The Cron.allow file exists, but the user's login name is not listed.
The Cron.deny file exists and the user's login is listed.
If both the Cron.allow and Cron.deny files do not exist, only the person authorized by the root user can submit a job with the crontab command.
Crontab File Entry format
A crontab file contains entries for each cron job. Entries are separated by newline characters. Each crontab file entry consists of 6 fields separated by a space or tab in the following format:
Time-of-Day month-week command
These fields receive the following values:
Score from 0 to 59
Time 0 to 23
Date 1 to 31
Month 1 to 12
Week 0 to 6 (Sunday to Saturday)
Command shell commands
You must specify a value for each field. In addition to the command field, these fields can contain the following: A number within the specified range. To run a command in May, specify 5 in the Month field.
Two numbers separated by dashes indicate the scope of the inclusion. To run a cron job from Tuesday to Friday, put 2-5 in the weekday field. A list of numbers separated by commas. To run commands on the first and last day of January, you can specify 1, 31 in the Day_of_month field.
* (asterisk), meaning all allowable values. To run one job per hour, specify an asterisk in the hour field.
Note: A backslash symbol (including%) before any character will cause the character to be literally processed. The specification of a day is made up of two fields (days of the month and days of the week). If you specify both as a list of elements, both fields are included. For example, the following entries:
0 0 1,15 * 1 command
The command will be run on the first and 15th days of each month, as well as every Monday. To specify days in only one field, the other field should contain *.
Specifying commands
This cron daemon runs fields that are named in the sixth field of the selected date and time. If you include a percent (percent sign) in the Sixth field, the cron daemon handles all tasks like a command call and makes all subsequent tasks available on standard input unless you escape the percent sign (\%). A blank line and the first non-null character are numbered tags (#) that are ignored. If the command contains a backslash (\) argument, the backslash should be added with a backslash before the backslash.
Note : The shell only runs the first line of the command field. All other rows are available as standard inputs for the command.
The cron daemon launches a child shell from the HOME directory. If you schedule a command to run, and you are not logged in, and you want to run the command in the. profile file, this command must explicitly read your. profile file.
The cron daemon provides a default environment for each shell, defining HOME, LOGNAME, Shell (=/usr/bin/sh), and PATH (=/usr/bin).
Sign
-e UserName edits a copy of the user's crontab file, or creates an empty file for editing if a valid UserName crontab file does not exist. When the edits are complete, the files are taken to the Crontab directory as the user's crontab file.
-L UserName Lists the user's crontab files.
-R UserName Remove the user's crontab file from the crontab directory.
-V UserName lists the status of the user cron job.
Security
Audit event: If the audit subsystem has been properly configured and enabled, the crontab daemon will generate the following audit record (or event) each time the command is executed:
Event information
Cron_jobremove lists which user removed the cron job and when it was dropped.
Cron_jobadd lists which users added cron jobs and when they were added.
For more detailed information about how to correctly select and group audit events and how to configure audit event data collection, see Setting up Auditing in security.
Exit status
The command returns the following exit values:
0 completed successfully.
>0 an error occurred.
Example
To copy a file named Mycronjobs to the/var/spool/cron/crontabs directory, enter the following:
Crontab Mycronjobs
The file will be copied as:
/var/spool/cron/crontabs/<username>
Where <username> is your current user name.
To write time to the console (in hours) per hour, enter:
0 * * * * echo the hour is ' date '. >/dev/console
To run the calendar command on every Monday, Wednesday, and Friday, enter:
6 * * 1,3,5/usr/bin/calendar
To run the calendar command 6:30 every day of the year, enter:
6 * * */usr/bin/calendar
To run a script named maintenance at midnight every day in August, enter:
0 0 * 8 */u/harry/bin/maintenance
To define text for the standard input of a command, enter:
0 * 5/usr/sbin/wall%happy Holiday!%remember to turn in your time card.
Define the standard input text for the wall command after% (percent semicolon):
HAPPY holiday! Remember to turn in your time card.
File
/var/adm/cron/fifo the specified pipeline, which sends a message to the cron daemon when a new job is submitted with the crontab or at command.
/VAR/SPOOL/CRON/CRONTABS Specifies the crontab buffer pool area.
/VAR/ADM/CRON/CRON.ALLOW Specifies the list of users who are allowed to access the crontab command.
/var/adm/cron/cron.deny Specifies the list of users who are not allowed to access the crontab command.
Linux Novice Academy crontab command syntax