Commands for Linux scheduled task startup

Source: Internet
Author: User

To use the automatic process startup function, you must master the following startup commands:

At command
========================================================== ======================================
At [-V] [-q queue] [-f file name] [-mldbv] Time
At-c job [job...]
At allows a fairly complex set of time-based methods, which actually extends the POSIX.2 standard. It can be specified in the hh: mm (hour: minute) format of the current day. If the time has passed, it will be executed on the next day. The user can also use a 12-hour hourly system, that is, add AM (morning) or PM (afternoon) after the time to indicate whether it is morning or afternoon. You can specify the date on which the command is executed, in the format of month day (month day), mm/dd/yy (month/day/year), or dd. mm. yy (Day. month. year ). The specified date must be later than the specified time.

The above is the absolute timing method. In fact, you can also use the relative timing method. The specified format is: now + count time-units, now is the current time, and time-units is the time unit. Here it can be minutes (minutes), hours (hours), days (days) and weeks (week ). Count indicates the number of times, days, hours, and so on. Another method of timing is to use today (today) and tomorrow (tomorrow) to specify the time for command completion.

[Example] specify to execute a command at this afternoon. Assume that the current time is noon and May 26, February 24, 2008. The command format is as follows:
At 5: 30 pm
At 17: 30
At 17: 30 today
At now + 5 hours
At now + 300 minutes
At 24.2.99
At 2/24/99
At 17: 30 Feb 24

[Example]
$ At-f work 4 + 3 days
Execute the job in file work at four o'clock P.M. three days later.
$ At-f work 10 am Jul 31
Execute the job in file work at ten o'clock A.M., January 1, July 31.
Super Users can use this command in any situation. For other users, whether it can be used depends on two files:/etc/at. allow and/etc/at. deny. If/etc/. if the allow file exists, only the users listed in it can use the at command. If the file does not exist, check/etc/. whether the deny file exists. Users listed in this file cannot use this command. If both files do not exist, only the super user can use this command. The empty/etc/at. deny file means that all users can use this command, which is also the default state.

If the at command is switched from a su command to the User shell for execution, the current user is considered to be the execution user, and all errors and output results will be sent to the user. However, if an email is sent, the email will be sent to the original user, that is, the shell Owner at login.
Parameters in the at command
-V prints the standard version number to a standard error.
-Q queue uses the specified queue. The queue name is composed of a single letter, valid queue name can be a-z or A-Z. Queue a is the default queue of the at command.
-After the m job is completed, an email is sent to the user who executes the at command.
-F file uses this option to read the command from the specified file, rather than from the standard input.
-L an alias for the atq command. This command is used to view the scheduled job sequence. It lists the jobs in the queue. If it is a Super User, it lists all jobs in the queue. Command syntax format: atq [-V] [-q queue] [-v]
An alias for the-d atrm command. This command is used to delete the sequence of commands to be executed. The syntax format is as follows: atrm [-V] job [job...]
-C sends the jobs listed on the command line to the standard output.
$ At 12/25/08
Warning: command will be executed using/bin/sh.
Remind the user which shell will be used to execute the command sequence. Then the system prompts at>, waiting for the user to enter further information, that is, the command sequence to be executed:
At> find/-name "*. txt" | lpr
At> echo "foxy: All texts have been printed. You
Can take them over. Good day! River "| mail-s" job
Done "foxy
After entering each line of commands, press Enter. After all command sequences are input, use the combination key to end the at command input. At this time, the screen will display the following information:
Job 1
In fact, if the command sequence is long or frequently executed, it is generally written to a file and then processed as an at command input. This is not prone to errors.
[Example] Find all the files suffixed with txt in the system and print them. After the printing is completed, send an email notification to the user foxy for pickup. The specified time is, January 1, December 25.
$ At </tmp/aa 12/25/08
The content in/tmp/aa is
Find/-name "*. txt" | lpr
Echo "foxy: All texts have been printed. River" | mail-s
"JobDone" foxy
The system will search for all txt files in the system at a.m. on April 9, December 25, and send an email to foxy after the printing is completed. Here the redirection character <The-f parameter can be used

Batch command

========================================================== ======================================
Batch uses a low-priority job to run jobs. This command has almost the same functionality as the at command. The only difference is that the at command executes the specified command at a specified time and at a very precise time; however, batch executes commands when the system load is low and resources are relatively idle. This command is suitable for executing commands that consume more resources.
The syntax format of the batch command is similar to that of the at command, that is, the batch [-V] [-q queue] [-f file name] [-mv] [time]
Generally, you do not need to specify a time parameter for the batch command, because the batch itself is characterized by the time that the system determines the task to be executed.
If you set a time, the original meaning is lost. For specific parameter explanations, refer to the at command.
$ Batch
At> find/-name *. txt | lpr
At> echo "foxy: All texts have been printed. You can take them over. Good day! River "| mail-s" job done "foxy
After all command sequences are input, use the combination key to end the at command input.

Cron command

========================================================== ======================================

The previous at and batch Commands will complete certain tasks within a certain period of time, but note that they can only be executed once. That is to say, after the command is run, the system completes the task at the specified time, And everything ends. However, in many cases, you need to repeat some commands. In this case, you can use the cron command to complete the task.
The cron command is automatically started by a shell when the system is started and enters the background (no & symbol is required ).
Generally, you do not have the permission to run this command. Although super users can manually start cron, we recommend that you put it in shell and start it by the system.
First, the cron command will search for the/var/spool/cron directory and find the crontab file named after the username in the/etc/passwd file. The found files will be loaded into the memory. For example, if a user in/etc/passwd is foxy, the corresponding crontab file should be/var/spool/cron/foxy. That is to say, the crontab file named after this user is stored in the/var/spool/cron directory. The cron command also searches for the/etc/crontab file, which is written in different formats.
After cron is started, it first checks whether the crontab file has been set by the user. If no crontab file is set, it is transferred to the "Sleep" State to release system resources. Therefore, this background process occupies a very small amount of resources. It wakes up once every minute to check whether there are commands to be run. After the command is executed, any output will be sent to the owner of crontab, or the user specified in the MAILTO environment variable in the/etc/crontab file.

The following describes how crontab works, but the execution of cron commands does not require user interference. You need to modify the sequence of commands to be executed in crontab.

Crontab command

========================================================== ======================================

The crontab command is used to install, delete, or list tables used to drive cron background processes. That is to say, the user puts the command sequence to be executed into the rontab file for execution. Each user can have their own rontab file. The following describes how to create a crontab file.
Crontab files under/var/spool/cron cannot be directly created or modified. The crontab file is obtained through the crontab command. Assume that you have a username of foxy, and you need to create your own crontab file. First, you can use any text editor to create a new file, and then write the commands to be run and the time to be periodically executed to it. Then save the disk and exit. Assume the file is/tmp/test. cron. Then, use the crontab command to install the file and make it the user's crontab file. Enter "crontab test. cron" to create a crontab file. You can go to the/var/spool/cron directory and check that there is an additional foxy file. This file is the required crontab file.

Syntax format of the crontab command
Crontab [-u user] file
Crontab [-u user] {-l |-r |-e}
The first format is used to install a new crontab file. The installation source is the file referred to by the file. If the "-" symbol is used as the file name, it means that the standard input is used as the installation source.
-U if this option is used, that is, the crontab file of the specified user will be modified. If this option is not specified, crontab is the crontab of the operator by default, that is, the crontab file of the user executing the crontab command will be modified. However, if you use the su command and then use the crontab command, there may be confusion. Therefore, if the su command is used, it is best to use the-u option to specify the user's crontab file.
-L display the current crontab on the standard output.
-R: Delete the current crontab file.
-E use the EDITOR referred to by VISUAL or EDITOR environment variables to edit the current crontab file. After editing is completed, the edited files are automatically installed.

Crontab file format description:
* Comma (',') specifies the list value. For example: ", 8"
* The range value specified by the hyphen ('-') is "1-6", indicating "1, 2, 4, 5, 6"
* Asterisk ('*') indicates all possible values.
In the crontab file, each line contains six fields. The first five fields specify the command execution time, and the last field is the command to be executed. Each field is separated by spaces or tabs. When there are multiple values in each field, you can use commas to separate them.
# Use the hash sign to prefix a comment
# + ------------ Minute (0-59)
# | + ---------- Hour (0-23, is pm)
# | + -------- Day of month (1-31)
# | + ------ Month (1-12)
# | + ---- Day of week (0-7) (Sunday = 0 or 7)
# |
# ***** Command to be executed

[Example]
5, 15, 25, 35, 45, 55, 16, 17, 18 *** command
This indicates the calendar month of any day, in fact, the command is executed every day at, 5 min, 15 min, 25 min, 35 min, 45 min, and 55 min.
[Example] Run the bin/account command in the user directory every hour:
1 * bin/account
[Example]
Execute the following two commands in the user directory at 03:20 every day (each command is separated)
20 3 *** (/bin/rm-f expire. ls logins. bad; bin/expire> expire.1st)
[Example] Here is a Super User crontab file:
# Run the 'atrun' program every minutes
# This runs anything that's due to run from 'at'. See man 'at' or 'atrun '.
, 20, 25, 30, 35, 40, 45, 50, 55 ***/usr/lib/atrun
40 7 * updatedb
, */Bin/sync

If you want to change the command content, you need to re-edit the original file and then use the crontab command to install it.
Users who can use the crontab command are limited. If/etc/cron. if the allow file exists, only the listed users can use this command. If the file does not exist but cron. if the deny file exists, only users not listed in the file can use the crontab command. If neither file exists, it depends on the setting of some parameters, it is possible that only the super user is allowed to use this command, or that all users can use this 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.