Linux scheduled task and Linux task
Crond of scheduled tasks
Crond is a service or software used in linux to regularly execute commands/scripts or specify program tasks. Generally, after installing Centos5/6 linux, by default, the Crond task scheduling service is started.
The Crond service checks whether there are tasks to be executed on a regular basis (which is checked every minute by default). If yes, the scheduled task is automatically executed according to its preset scheduled task rules. The crond scheduled Task Service is the same as the alarm clock we usually use early morning.
Start service that must be retained:
Crond: scheduled Task Service
Network: network SERVICE
Rsyslog: Log Service
Sshd: remote connection service
Sysstat: System Monitoring Service (involving a series of monitoring tools)
Why should I use a scheduled task?
- For example, our database or code program needs to perform a full backup at every night, so that periodic work needs to be performed every night, you have to get up in the middle of the night, log on to the system to execute the task, and then go to bed. In addition, the task execution process may last for several hours, so that we don't have to go to bed for one night. This obviously doesn't work. So how can we solve this periodic task execution requirement?
- This is the linux scheduled task Crond, which is equivalent to the alarm function in our daily life. We set the timer every night and wake you up on time in the morning.
Ls-l/var/log/messages * # system logs ls-l/var/log/secure * # User Logon logs
Tasks performed by users: tasks scheduled by a user or system administrator, such as synchronizing time every five minutes with servers on the Internet (this is part of basic system optimization ), back up website data and database data at every night. Generally, these tasks need to be set by each O & M personnel.
Server Time Synchronization
echo "*/5 * * * * /usr/sbin/ntpdate time.nist.gov > /dev/null 2>&1" >> /var/spool/cron/root
You can run the preceding command every five minutes. Synchronize the Internet time to the local Linux system each time.
Crontab-l view the current user's scheduled task configuration. * Details will be given below ~~
Time.nist.gov is the NTP server address time.windows.com, Microsoft's own time server. For more information, see ==> common NTP server addresses and IP addresses in China
Crontab: As mentioned earlier, this command can periodically execute tasks, for example, synchronizing server time every five minutes. To execute the crontab command, you also need to start a service crond. This crontab command is the most commonly used command in production.
1. Start the crond service at startup
2. Current Service Status (view, enable, and disable)
3. How to view Processes
Ps-ef | grep crond
Tip:
- The crond service is a running program, while crontab is a command used to manage users' scheduled tasks (rules ).
- Crond is an important service commonly used in enterprise production. at and anacron are rarely used and can be ignored.
- Almost every server uses the crond service.
- Thousands of servers can develop distributed scheduled task projects (Batch Management of ansible/saltstack)
Crond instructions
[root@chengliang ~]# crontab --helpcrontab: invalid option -- '-'crontab: usage error: unrecognized optionusage: crontab [-u user] file crontab [-u user] [ -e | -l | -r ] (default operation is replace, per 1003.2) -e (edit user's crontab) -l (list user's crontab) -r (delete user's crontab) -i (prompt before deleting user's crontab) -s (selinux context)[root@chengliang ~]#
- Crontab-l view the current user's scheduled task Configuration
- Crontab-e enters the current user's timer task vim editing mode
- Crontab-u username-l view the scheduled task settings of a specified user
Key Points
With crontab, we can execute the specified system command or script at a fixed interval. The unit of time interval can be minute, hour, day, month, week, or above (Note: Do not combine day and week ). The crond Service uses the crontab command to easily implement periodic log analysis, data backup, and other enterprise O & M scenarios.
User Permissions and scheduled task files
File |
Description |
/Etc/cron. deny (reject) |
Users listed in this file are not allowed to use crontab. |
/Etc/cron. allow (allowed) |
This file has a higher priority than cron. deny (it does not exist by default and is generally not used) |
/Var/spool/cron/ |
This directory exists in all user crontab configuration files by default. The file name is named after the user name.
|
[root@chengliang ~]# cat /etc/cron.deny zcl[root@chengliang~]# su - zcl[chensiqi@chengliang~]$ crontab -lYou (chensiqi) are not allowed to use this program (crontab)See crontab(1) for more information[zcl@chengliang~]$ crontab -eYou (chensiqi) are not allowed to use this program (crontab)See crontab(1) for more information
Crontab {-l |-e} is actually an object like/var/spool/cron/current user.
By default, after you create a scheduled task rule, the configuration file corresponding to the rule record will exist in/var/spool/cron, the name of the crontab configuration file is the same as that of the logon user, for example:The configuration file of the scheduled task of the root user is/var/spool/cron/root.
[Root @ chengliang ~] # Ls/var/spool/cron/root # No by default. [root @ chengliang ~] is available only when a scheduled task is created. # Cat/var/spool/cron/root */5 *****/sbin/ntpdate time.windows.com>/dev/null 2> & 1
The writing format of Crontab scheduled tasks is very simple. Your scheduled task rules are generally divided into six segments (each segment is separated by spaces. The scheduled task of the system is/etc/crontab, divided into 7 segments, separated by spaces), the first five segments are the time set segments, and the sixth segment is the command or script task segment to be executed.
Scheduled task instance:
01 *** cmd # Run the command within 01 minutes every hour
02 04 *** cmd # Run the command at every day
22 14*00 cmd # Run at every Sunday
42 04 01 ** cmd # execute at 04:42 on the first day of every month
Tip:
1. cmd is the command or script to be executed, for example,/bin/sh/server/scripts/chensiqi. sh.
2. Each column must have a space. Can multiple spaces be entered? Practice by myself ==> I have tested that multiple spaces can be executed !!
Time memory tip: time-based day-month week
The following table lists the meanings of special symbols in crontab syntax.
Special symbols |
Description |
* |
* Indicates any time, which actually means "every" |
- |
The minus sign indicates a separator, indicating a time range and a range, such as to. For example, the task is executed at, and every day. 00 17-19 *** cmd |
, |
Comma (,) indicates the time period to be separated. For example, a task is executed at every day, and a period of * cmd |
/N |
N represents a number, that is, "every n units of time". For example, a task can be executed every 10 minutes and written/* 10* *** Cmd, where,/10,The value range is 0-59. Therefore, it can be written as 0-59/10.
|
Reference blog: http://www.cnblogs.com/chensiqiqi/p/6367890.html