Windows provides the task scheduling function. In the control panel, you can choose performance and maintenance> task scheduling to schedule tasks that run automatically. You can create a scheduled task through step-by-step guidance of 'add Task Scheduler. The same functions are available in Linux:
Part 1: AT command
1. at command: to execute a specified task at a specified time, it can only be executed once. You must enable the ATD process (PS-Ef | grep ATD view, and enable/etc/init. d/ATD start or restart; run chkconfig -- level 2345 ATD on when starting the system ).
[Root @ localhost test] At now + 1 MINAT> echo "hello">/dev/tty2 and press Enter # Send "hello" to the tty2 terminal one minute later
-- Note: use Ctrl + D to terminate character input (that is, <EOT>)
At 2009-12-13at> echo "test at command">/home/test.txt at 2009-12-13at>/sbin/shutdown-H now # Use the absolute path here
The task created by the AT command is placed under/var/spool/at. It is a script file and can be edited using vi.
At now + 1 MINAT> echo "hello" <EOF> # at this time, an email is sent to the currently executed person. You can run the MAIL command to view the information, that is, use the MAIL command first, enter '1' when & appears.
ATQ command: queries which tasks are waiting for execution on the current machine. The first column is the work number. Atrm command: cancel the task. For example: atrm 10 #10 indicates the work number. Batch Commands: execute tasks with a low-priority latency.
Batchat> echo "hello">/home/abc.txt <EOF>
2. at command security control/etc/. allow # List users who can use the AT command. Find at first. allow file, find. deny file/etc/. deny # List users that cannot use the AT command, one row at a time. If no at. allow file or at. deny file exists, only the root user can execute the AT command.
Part 2: cron service mechanism
1. cron service Introduction
Cron saves the command line in the/etc/crontab file. If each system user sets his/her Cron, the crontab corresponding to the user name will appear under/var/spool/cron. Whether writing files in the/var/spool/cron directory or the/etc/crontab file, cron can accurately execute the scheduled tasks, the difference is that the crontab files of system users under/var/spool/cron are user-level task configurations, while the/var/crontab files are system-level task configurations. The cron server reads all files and/etc/crontab files in the/var/crontab/cron directory every minute.
2. Use the cron service to execute scheduled tasks
# Service crond start/restart/stop
Start/restart/Close the cron Service
# Crontab-l
View the current user's crontab, that is, view the existing scheduled task list
# Crontab-e
Edit the current user crontab. If you have never edited the crontab file before, crontab is a blank file. You can manually compile the list of required scheduled tasks, the following describes how to write a scheduler task list.
The following are examples:
- 9 9 ***/usr/local/bin/cvsb
- 3 3 ** 0/usr/local/bin/qbbak
- **/6 ***/usr/local/bin/esbbak
- 20, 30 ***/usr/local/bin/esbak
The meanings of the six fields are as follows:
9 9 ***
Minute Hour Date month week
- Indicates that the cvsb file in the/usr/local/bin/directory is executed at 09:09 every morning.
- Indicates that the qbbak file in the/usr/local/bin directory is executed at 03:03 every Sunday.
- It indicates that the execution is performed once every 6 hours. The execution time starts from the first execution. Of course, you can also customize the execution time. For example, if the execution time is 0 */6 *, the program will be executed on the whole point.
- Indicates that the task is executed at and every hour.
The above briefly introduces some basic cron usage. The time combination of this stuff is not generally strong ~ It can be as complicated as you cannot imagine. We only need to know a little about it to meet our daily needs.
3. cron advanced usage
(1). Prevent cron from generating junk files
Careful friends may find that the system/var/spool/clientmqueue/directory often contains a large number of files, because some users in the system have enabled Cron, while the programs executed in cron have output content, the output content will be sent to cron users by email, and these files are generated because Sendmail (the mail server provided by the system) is not started.
In this case, you can use output redirection, that is, add>/dev/null 2> & 1 after the scheduled task.
Example:
- 9 9 ***/usr/local/bin/cvsb>/dev/null 2> & 1
(2> & 1: redirect the error to the place where the output is to be sent .)
Redirects the execution result of the command to/dev/null, that is, discards the generated error.
(2 ). in a situation that is often encountered, the file to be executed is okay, but it cannot be correctly executed through cron. In this case, we can output the cron execution process to a file through output redirection, analyze the execution process to find the cause of the Error
Example:
- 9 9 ***/usr/local/bin/cvsb>/var/log/crontab_log 2> & 1
In the preceding example, the execution process of cron is output to a text file crontab_log. You can manually create this file and store it wherever you want. The rewrite directory writes to the correct directory.