The crontab command is commonly used in UNIX and Linux systems to set instructions that are executed periodically. The command reads the instruction from the standard input device and stores it in the crontab file for reading and execution. Typically, crontab stored instructions are activated by daemons, and Crond services are often run in the background. By setting the time to check if there is a job to be performed, this type of job is generally known as: cron jobs. The crontab file crontab file contains a series of jobs and instructions sent to the cron daemon. Each user can have their own crontab file, while the operating system saves a crontab file for the entire system. cron expression time is set to follow the cron expression crontab service crontab command is a Linux self-command, usually run in the background, auto-start. Different versions of Linux systems, crontab service names are not all the same: Cron/crond see if the service is already running:
PS grep Cron/crond
The common commands for starting or shutting down a service are as follows:
sudo service Cron/crond Start/stop/restart
the crontab option #. The basic syntax of crontab
file crontab [-u user] [-i] {-e |-l |-r}
#. The parameter description of the crontab command crontab-u User: Sets the cron service for a customer, which is typically required by the root user to perform this command crontab-l: Lists the details of the user's cron service crontab-r: Delete a user's Cron service (the user's Cron service will all be removed and need to be used with caution) Crontab-e: Edit The syntax rules for a user's cron service cron file #. Time-Sharing Week order
0-59 0-23 1-31 1-12 0-6 COMMAND #. The time settings follow the syntax rules for cron expressions, which are the meanings of special symbols:
*: The number representing the range of values/: represents each-: represents from a number to a number: separate several discrete numbers for example, in December, every 20 minutes in the morning from 6 to 12, the/usr/bin/backup is executed:
**6- /usr/bin/backup
Some examples are as follows:
- 3 Ten, -* *ls#每月10号及20号的3:30 Execute LS command [note: "," used to connect multiple discontinuous periods] - 8- One* * *ls#每天8-The 11-point 25-minute execution of the LS command [note: "-"To connect a continuous period of time.")*/ the* * * *ls#每15分钟执行一次ls命令 [i.e. the No. 0 per hour the - $60 Minutes execution of LS command] - 6*/Ten* *ls#每个月中, the LS command is executed every 10 days 6:30 [that is, 1 per month, One、 +, 31st Yes 6:30 executes the LS command once. ] - 7* * * Root run-parts/etc/cron.daily #每天7:50 Execute all executables in the/etc/cron.daily directory as root
[Note: The Run-parts parameter indicates that all executables in the following directory are executed.] ]
Create Crontab
Before considering submitting a crontab file to the cron process, you can set the [editor] environment variable editor. The VI Editor is used by default when editing a crontab file (Ubuntu opens by default using the GNU Editor), as follows: 1, Edit the. profile file under your home path: Vi/home/qi/.profile2, add the following:
Editor=VI; export EDITOR
3, source/home/qi/.profile default VI editor set successfully, execute command test: crontab-e#. Create a file, where the content is crontab to execute the command, file name suggestion: <user>cron Add crontab Task command action as follows: Crontab <user> Cron Lists crontab file contents: Crontab-l Edit crontab file contents: Crontab-e Delete crontab file contents: Crontab-r < note: All execution tasks under the current user are deleted, so be careful to do so > Note crontab file contents: # < description: Each line of crontab file content is an execution task >#. The crontab output configuration crontab often configures the run script output to:>/dev/null 2>&1 to avoid content output in crontab runs. Where does the > delegate redirect to, for example: echo "123" >/home/123.txt/dev/null represents an empty device file 1 means stdout standard output, the system default is 1, so ">/dev/null" equals "1 >/dev /null "2 means stderr standard error & denotes equivalent meaning, 2>&1, which means 2 of output redirection equals 1 (note:" >> "in Linux) ------------------------------------------------------------Example: Execute the LS command every 5:30 and output the result to the/home/test file 5 * * ls & Gt;/home/test 2>&1
Note: 2>&1 indicates execution results and error messages. ------------------------------------------------------------a few additional notes#. The cron service does not only read all the files in the/var/spool/cron once per minute, but also reads/etc/crontab. So we configure this file to do something with the cron service. The crontab configuration is for a user, while the edit/etc/crontab is a task for the system. Edit/etc/crontab file configuration cron sudo vim/etc/crontab--------------------------------------------------------------------- --------------------------------shell=/bin/sh Path=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin Mailto=root//If there is an error, or if there is data output, the data is sent to this account home=///user Run path, here is the root directory # m h Dom Mon Dow user Command 17 * * * Root CD/&& run-parts--report/etc/cron.hourly 6 * * * Root test-x/usr/sbin/anacron | | (CD/&& run-parts--report/etc/cron.daily) 6 * * 7 root test-x/usr/sbin/anacron | | (CD/&& run-parts--report/etc/cron.weekly) 6 1 * * Root test-x/usr/sbin/anacron | | (CD/&& run-parts--report/etc/cron.monthly) #----------------------------------------------------------- ------------------------------------------RThe Un-parts parameter indicates that all executables in the following directory are executed. If you remove this parameter, you can then write the name of the script you want to run, not the folder name.Summary: Two ways to create a crontab task1) at the command line, enter: Crontab-e and then add the appropriate task. (You need to create a file that performs user tasks first) 2) edit the/etc/crontab file directly and add the appropriate task