1. run commands in the background
Cron: System scheduling process. It can be used to run jobs during off-peak hours every day, or during different periods of one week or one month.
At: Command. It is used to run some special jobs at a specific time, or in a later non-Load peak or peak load period.
&: It is used to run a process that takes a short time in the background.
Nohup: use it to run a command in the background, even if the user exits, it will not be affected.
2 cron and crontab
Cron is the main scheduling process of the system. It can run jobs without human intervention.
A crontab command allows you to submit, edit, or delete a job.
Each user can have a crontab file to save scheduling information. You can use it to run any shell script or command once an hour or Wednesday, depending on you.
Every user can have their own crontab files, but in a large system, the system administrator usually disallow these files, but only keeps one such file in the entire system. The system administrator uses cron. deny
And cron. allow files to prohibit or allow users to own their own crontab files.
2.1 crontab Fields
To run a job at a specific time, you need to understand the meaning and format of each field in each entry of the crontab file.
1st columns minute 0 ~ 59
2nd columns hour 0 ~ 23 (0 indicates midnight)
3rd columns daily 1 ~ 31
4th columns Month 1 ~ 12
5th columns in a week 0 ~ 6 (0 indicates Sunday)
6th-1970 columns per year ~ 2099(Optional)
7th columns of commands to run
* ***** Command to be executed
______
|
| + ----- Year (1970-2099) (optional, if not be used, you can keep blank)
| + ----- Day of week (0-6) (Sunday = 0)
| + ------- Month (1-12)
| + --------- Day of month (1-31)
| + ----------- Hour (0-23)
+ ------------- Min (0-59)
If multiple parameters are set for one item, they can be separated by commas (,), such as 3,4, 5, and 6.
If it is a parameter composed of several consecutive numbers, you can use "-" to connect first and end, for example, 3-6.
The crontab format is as follows:
Minute <> hour <> day <> month <> day <> week <> command to run, where <> space.
An entry in the crontab file is read from the left. The first column is the minute, And the last column is the command to be run. It is located after the week.
In these domains, you can use a horizontal bar "-" to indicate a time range. For example, if you want to run a job from Monday to Friday, you can use 1-5 in the weekly domain. You can also use commas (,) in these domains. For example, if you want to run a job on Monday or Thursday, you only need to use 1, 4. The asterisk (*) can be used to represent consecutive time periods. If you do not have a special limit on a time domain, you should also enter * in this domain *. Each entry in the file must contain five time fields, and each field must be separated by a space. All the comment lines in this file must be expressed as # At the beginning of the line.
Example of crontab file entries:
30 21 ***/apps/bin/cleanup. Sh
The preceding example shows cleanup. Sh in the/apps/bin directory at every night.
45 4, 10, 22 **/apps/bin/backup. Sh
The example above indicates the backup. Sh in the/apps/bin directory at 4: 45 on the 1, 1, 0, and 2 every month.
10 1 ** 6, 0/bin/find-name "core"-exec RM {}\;
The preceding example shows that a find command is run every Saturday and Sunday.
0, 30 18-23 ***/apps/bin/dbcheck. Sh
The preceding example shows running dbcheck. Sh in the/apps/bin directory every 30 minutes between and every day.
0 23 ** 6/apps/bin/qtrend. Sh
The preceding example shows qtrend. Sh in the/apps/bin directory run at every Saturday.
In the preceding example, each command provides an absolute path. When you use crontab to run the shell script, you must give the absolute path of the script and set the corresponding environment variables. Remember, since the user submitted these jobs to Cron, it is necessary to provide cron with all the required environments. Do not assume that cron knows the required special environment.
Therefore, make sure that all necessary paths and environment variables are provided in the shell script, except for some automatically set global variables.
If cron cannot run the script, you will receive an email explaining the cause.
2.2 crontab command options
The crontab command is generally used as follows:
Crontab [-u user]-E-l-R
Where:
-U user name.
-E: edit the crontab file.
-L list the content in the crontab file.
-R: Delete the crontab file.
If you log on with your own name, you do not need to use the-u option, because the current user can be known when executing the crontab command.
2.3 create a new crontab file.
Before submitting a crontab file to the cron process, you must set the environment variable editor.
The cron process determines which editor to use to edit the crontab file. 99% of UNIX and Linux users use VI. If so, edit the. profile file in the $ home directory and add the following line to it:
Editor = VI; export Editor
Save and exit.
Create a file named <user> Cron, where <user> is the user name, such as lanseyokeycron.
First, create a s hell file, such as time. Sh.
Echo "now time is :"
Date
Open the file lanseyokeucron and add the following content:
# Echo the date to the console every
#15 minutes between 6 am and 6 on Sunday
0, 15, 30, 45 06-18 ** 0/lanseyokey/time. Sh
Save and exit. Make sure that the first five fields are separated by spaces.
In the preceding example, the system outputs the current time every 15 minutes. If the system crashes or hangs, you can see at the last displayed time when the system stops working.
To submit the just-created crontab file, you can use the newly created file as a parameter of the cron command:
$ Crontab lanseyokeycron
Now the file has been submitted to the cron process and will run every 15 minutes.
At the same time, a copy of the new file has been stored in the/var/spool/cron directory, and the file name is the user name (that is, lanseyokey ).
2.4 list crontab Files
To list crontab files, you can use:
$ Cron-l
# Do not edit this file-edit the master and reinstall.
# (Lanseyokeycron installed on Sun Dec 20 11:28:00 2009)
# (Cron vesion -- $ ID: crontab. C. V 2.13 1994/01/17 04:20:37 vixie exp $)
# Echo the date to the console every
#15 minutes between 6 am and 6 on Sunday
0, 15, 30, 45 06-18 ** 0/lanseyokey/time. Sh
Note: If you log on with the lanseyokey user name, you do not need to use the-u option; otherwise
$ Crontab-u lanseyokey-l
You can use this method to back up the crontab file in the $ home directory:
$ Crontab-L> $ home/mycron
2.5 edit the crontab file
If you want to add, delete, or edit entries in the crontab file, and set the editor environment variable to VI, you can use VI to edit the crontab file. The corresponding command is:
$ Crontab-e
You can modify the crontab file and exit as if you were using VI to edit any other files. If some entries are modified or new entries are added, cron will perform necessary integrity checks when saving the file. If a field contains a value out of the permitted range, it will prompt you.
When editing the crontab file, a new entry may be added. For example, add the following one:
# Echo greeting to user
0 06-18 ***/bin/echo "Hello, welcome to Linux"
Save and exit. It is best to add a comment to each entry in the crontab file so that you can know its function and running time. More importantly, you can know which user's job this is.
Now let's use the previous crontab-l command to list all its information:
$ Cron-u lanseyokey-l
# Do not edit this file-edit the master and reinstall.
# (Lanseyokeycron installed on Sun Dec 20 11:28:00 2009)
# (Cron vesion -- $ ID: crontab. C. V 2.13 1994/01/17 04:20:37 vixie exp $)
# Echo the date to the console every
#15 minutes between 6 am and 6 on Sunday
0, 15, 30, 45 06-18 ** 0/lanseyokey/time. Sh
# Echo greeting to user
0 06-18 ***/bin/echo "Hello, welcome to Linux"
Delete the crontab file. You can use:
$ Crontab-R
Restore the lost crontab file. If you accidentally delete the crontab file, assume that you have a backup in your $ home directory, you can copy it to/var/spool/cron/<username>, where <username> is the user name.
If the copy cannot be completed due to permission issues, you can use:
$ Crontab <FILENAME>
<FILENAME> indicates the file name of the copy in the $ home directory.
We recommend that you save a copy of the file in your $ home directory, because the r key is next to the right of the E key, and it is easy to delete it by mistake.
In some system documents, it is recommended that you do not directly edit the crontab file, but edit a copy of the file and resubmit the new file. Some crontab variants are somewhat weird, so be careful when using the crontab command.
If any option is missing, crontab may open an empty file or it looks like an empty file. Press delete to exit. Do not press <Ctrl-D>, otherwise, the crontab file will be lost.