The use of CRONTAB commands under Linux

Source: Internet
Author: User
Tags exit

crontab command

Use

Submit, edit, list, or drop a cron job.

Grammar

crontab[-e[username]|-l[username]|-r[username]|-v[username]| File]

Describe

This crontab command submits, edits, lists, or drops cron jobs. A cron job is a command that is run by a cron daemon at the scheduled interval of a rule. To submit a cron job, specify the crontab command with the-e flag. The crontab command invokes an editing session, allowing the creation of a crontab file. In this file, create entries for each cron. Each entry must be a format that the cron daemon can accept. For information about creating entries, see the crontab file entry format.

When the entry is created and the file is exited, the crontab command copies it to the/var/spool/cron/crontabs directory and puts it in a file whose name is the current user name. If a file named with a user name already exists in the Crontabs directory, the crontab command overwrites 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, the command invokes the editor it specifies. Otherwise, the crontab command invokes the VI editor.

To list the contents of the crontab file, specify the crontab command and use the-l flag. To remove existing files, 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, drop, 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, this crontab flag is available to the root user and the current user.

Security

Only the owners of the root and crontab files can use username after the-e,-L,-R, and-V flags to edit, list, drop, or validate the crontab files for the specified user.

Cron Daemon

The command and crontab file entries that this cron daemon runs are relative. If the output of the cron job is not redirected to standard output or standard error, the cron daemon notifies you by e-mail of any command output or error. This cron daemon does not run this job if an incorrect cron job is specified in the crontab file.

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 that indicates the change is sent to the cron daemon. This eliminates the overhead of checking for new or changed files at the scheduled interval of a rule.

Controlling the use of the crontab command

/var/adm/cron/cron.allow and/var/adm/cron/cron.deny files control which user can use the crontab command. The root user can create, edit, or delete these files. The entries in these files are the user login name, one name per line. If the login identity is associated with multiple login names, the crontab command takes the first login name in the/etc/passwd file, regardless of which login name is currently actually used. Also, to allow users to start a cron job, you should use the Chuser command to set the Daemon property in the/etc/security/user file to True.

An example of the following about the Cron.allow file:

root nick dee sarah

If the Cron.allow file exists, the crontab command is available only to users whose logon name appears in the file. The root login name must appear in the Cron.allow file if the file exists. The system administrator can explicitly stop a user by using the crontab command and listing the user's login name in the Cron.deny file. If only the Cron.deny file exists, a user with any name that does not appear in the file can use the crontab command.

If one of the following conditions is true, the user will not be able to use the crontab command:

Cron.allow files and Cron.deny files do not exist (only root users are allowed).

The Cron.allow file exists, but the user's login name is not listed therein.

The Cron.deny file exists and the user's login is listed.

If Cron.allow and Cron.deny files do not exist, only people authorized by Root can submit a job with the crontab command.

Crontab File Entry format

An crontab file contains entries for each cron job. Entries are separated by newline characters. Each crontab file entry includes 6 fields, which are separated by spaces or tabs in the following format:

Minute hour day_of_month month weekday command

These fields receive the following values:

Minute 0 to 59
Hour 0 to 23
Day_of_month 1 to 31
Month 1 to 12
Weekday 0 to 6 (Sunday to Saturday)
Command Shell command

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.

The number of two separated by dashes indicates 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 the command on the first and last day of January, you can specify 1, 31 in the Day_of_month field.

* (asterisk), which means 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 processed literally. The specification of the 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 entry:

001,15*1command

command will be run on the first and 15th days of each month, as well as every Monday. To specify the day in only one field, the other field should contain *.

Specifying commands

This cron daemon runs the field named in field sixth of the selected date and time. If you include a% (percent semicolon) in the sixth field, the cron daemon handles all tasks like a command call, and makes all subsequent tasks available on standard input, unless the escape percent sign (\%) is used. The blank line and the first non-null character are number marks (#) that will be ignored. If the command contains an argument for a backslash (), a backslash should be added with a different backslash before the backslash.

Note: The shell only runs the first line of the command field. All other rows are available as standard input for the command.

The cron daemon starts a child shell from the home directory. If you schedule a command to run, 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
-eusername Edit a copy of the user's crontab file or, if a valid username crontab file does not exist, create an empty file for editing. When the edit is complete, the file is crontab directory as the user's crontab file.
-lusername Lists the user's crontab files.
-rusername Removes the user's crontab file from the crontab directory.
-vusername 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 produce the following audit records (or events) each time the command is executed:

Event Information
Cron_jobremove Lists which users removed the cron job and the time 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 settingupauditing in security.

Exit status

This command returns the following exit values:

0 Successfully completed.
>0 An error has occurred.
Example

To copy the file named Mycronjobs to the/var/spool/cron/crontabs directory, enter the following:

 crontabmycronjobs

The file will be copied as:

 /var/spool/cron/crontabs/<username>

Where <username> is your current user name.

To write the time to the console hourly (in hours), enter:

0****echoThehouris`date`。>/dev/console

To be 6:30a.m in every week, Wednesday and Friday. To run the calendar command, enter:

306**1,3,5/usr/bin/calendar

To run the calendar command at 6:30 every day of the year, enter:

306***/usr/bin/calendar

To run the script named maintenance every night in August, enter:

00*8*/u/harry/bin/maintenance

Define the text for the standard input of the command, enter:

016*125/usr/sbin/wall%HAPPYHOLIDAY!%Rememberto turninyourtimecard.

The text that defines the standard input for the Wall command after the% (Percent semicolon):

HAPPYHOLIDAY!  Remembertoturninyourtimecard.

File
/var/adm/cron/fifo A specified pipe that 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 access to the crontab command.
/var/adm/cron/cron.deny Specifies the list of users who are not allowed to access the crontab command.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.