How to Use cron in RedHat Linux

Source: Internet
Author: User
In RedHat Linux Cron configuration file:
/Etc/crontab records environment variables (shell, path, mailto, home) and pre-defined cron tasks (cron. Hourly/daily/weekly/monthly ).
(When the cron task cannot be executed, check the environment variables and command path here)
/Etc/cron. deny the user recorded here cannot submit cron tasks
/Etc/cron. allow the user recorded here can submit cron tasks
(If these two files do not exist, only root has the permission to submit cron tasks)
/Etc/cron. d/,/etc/cron. daily/, cron. Hourly/,/cron. Monthly/store the pre-defined cron tasks of the system.
/Var/spool/cron tasks of all users, corresponding to the user name
The crontab configuration file of/var/log/cron is abbreviated as "cron table. Cron searches for the crontab file in three places:/etc/crontab,/etc/cron. d/,/var/spool/cron.
Crontab file format:
Minute hour day month weekday [username] command
Minute 0-59
Hour hour 0-23
Day date 1-31
Month 1-12
Weekday day of a week 0-6 (0 represents Sunday)
These time-related fields are separated by spaces. Each field must specify a value:
* (Asterisk), meaning all allowed values
An integer for exact match
Two integers separated by the broken number, indicating the range
Multiple integers or ranges separated by commas (,) indicate matching the listed values.
/N indicates the step size, range:/n
The "day" is determined by day and weekday.
Asterisks can match all characters. An integer must be exactly matched. two integers separated by short dashes match the value range;
An integer or range separated by commas (,) to match any column value.
For example: 45 10 ** 1-5 from Monday to Friday do not place the asterisk in the first field unless you want to execute this command every minute.
If both weekday and day are specified, either of them is executed.
Example: * 13*5 Friday every half hour, and every half hour on the 13th of every month
30 2 ** 1 (CD/users/Joe/project; Make)

20 1 *** find/tmp-atime + 3-exec Rm-f {}';'
Delete all files not accessed within 3 days in the/tmp directory at every day
55 23 ** 0-3, 6/Staff/Trent/bin/Acct-script
Run Acct-script at every day except Thursday and Friday

Crontab command
Crontab [-u user] [-L |-r |-E]
-L list user cron tasks
-R: delete a Cron task
-E: edit a Cron task
-I Prompt before deletion
Note: The crontab command without parameters will try to read the crontab content from the standard input. In this mode, if you exit with control-D, the entire crontab content will be deleted. Control-C should be used to exit.
Crontab [-u user] filename
Import a crontab task from a file.

Command is the sh command to be executed. It can be any valid shell command and should not be enclosed in quotation marks. Cron thinks that the command is always at the end of this line. It can contain spaces or tabs.
Cron uses the percent sign (%) to indicate the line feed of the command field. Only the text before the first percent sign is included in the actual command, and other lines are used as the standard input for this command.
For example:
Echo the time is now 'date'>/dev/console
Write Garth % Hi Garth % remember to get a job.
CD/etc;/bin/mail-s "Password File" EVI <passwd
Manage crontab
Crontab filename installs filename as the crontab file, which replaces the previous version of the crontab file.
Crontab-e checks out a copy of a crontab, opens it in the editor, and resubmit it to the crontab directory (/var/spool/cron/'username ').
Crontab-l columns the content in crontab on the standard output.
Crontab-R: delete crontab.
Crontab without command line will try to read crontab content from its own standard output. If you log in unexpectedly, do not try to exit by using Ctrl-D. Instead, the entire crontab content will be deleted and Ctrl-C should be used to exit.
For root, you can run the following command: crontab-u Loki-R to delete the crontab file belonging to Loki. Control the use of crontab commands
The/etc/cron. allow/etc/cron. deny file controls which user can use the crontab command. Root users can create, edit, or delete these files. The entries in these files are user login names, and each name occupies one row. If the logon ID is associated with multiple logon names, the crontab command uses the first logon name in the/etc/passwd file, regardless of which logon name is used currently. In addition, to allow the user to start a cron job, use the chuser command to set the daemon attribute in the/etc/security/user file to true. The following is an example of the cron. allow file:
Root
Nick
Dee
Sarah
If the cron. allow file exists, you can use the crontab command only for users whose logon name appears in the file. The root user's login name must appear in the cron. allow file, if the file exists. The system administrator can explicitly stop a user and use the crontab command to list the user's logon name in the cron. deny file. If only the cron. deny file exists, you can use the crontab command if any name does not exist in the file.
If the following condition is true, you cannot use the crontab command:
The cron. allow 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 in it.
The cron. deny file exists, and the user's logon is listed in it.
By default, all users can submit a crontab file to cron. The default user configuration file is an empty/etc/cron. deny file. Only root can submit crontab without these two files.
Importantly, access control is implemented by crontab instead of cron. If a user can transfer the crontab secret to a suitable directory in other ways, cron will blindly execute the commands in the file.
Linux Pre-installed crontab items are mainly in/etc/cron. d. If you want to disable an item, comment it out.
In addition, there is a script that runs once a day in/etc/cron. daily; a script that runs once a week in/etc/weekly. Crond Management
Service crond {START | stop | status | reload | restart | condrestart}
/Etc/init. d/crond {START | stop | status | reload | restart | condrestart} common purposes:
Clear file systems
Find/-xdev-name core-atime + 7-exec Rm-f {}';'
Delete a core image file that has never been accessed for a week ( Program Image files generated during crash ).
Find/-xdev-atime + 3' ('-name' # *'-o-name '*. CKP '-o-name '*~ '-O-name'. NFS ~ '') '-Exec Rm-f {}';'
Delete a file starting with #,. #, or NFS, or use ~ As well as files ending with. CKP, there are still files not accessed for three days. Different types of temporary files and backup files in the editor are typical examples of this mode.
CD/tmp; find .! -Name .! -Name lost + found-type D-mtime + 3-exec/bin/Rm-RF {}';'
Recursively Delete All subdirectories that have not been modified for 72 hours under/tmp. Common files under/tmp will be deleted by the system startup script when the system is started, but some systems will not delete the directory.
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.