Linux scheduled task and linux task
I am used to using windows scheduled tasks. It is not suitable for managing scheduled tasks using crontab in linux.
The basic usage is summarized as follows.
Create a simple scheduled task
Output the current time every minute to the time. log file in the user's home directory.
$ crontab -e* * * * * echo `date` >> /home/xxx/time.log
The crontab program path is/usr/bin/crontab.
You can query the scheduled tasks of each user in the/var/spool/cron/crontabs/directory.
You must use the root permission to access this directory. The user's scheduled tasks are recorded in each file named after the user name.
Note: Do not use vi to directly edit these files.
Every job running cron is recorded in the/var/log/cron logon file, but the cron log is disabled by Ubuntu by default.
Open the rsyslog service configuration file.
$ sudo vim /etc/rsyslog.d/50-default.conf
Remove the comments from the beginning of the line below
cron.* /var/log/cron.log
Restart rsyslog
$ sudo service rsyslog restart
Now we can see the/var/log/cron file. Viewing this file is an important way to solve timing task errors.
Control users to run scheduled tasks
If you do not want a user to run a scheduled task, directly write his account to the/etc/cron. deny file. Note: One account can be created in one row.
You can also use/etc/cron. the allow file explicitly specifies the account of the user who can run the scheduled task, and/etc/cron. allow ratio/etc/cron. deny has a higher priority.
However, it seems a bit messy. Instead, you should use only one of them.
Crontab command
-U: only root has the permission to execute this task, that is, to help other users create/remove crontab scheduled tasks.
-E: Edit crontab content
-L: displays crontab content.
-R: removes all crontab content. to delete only one item, use-e to edit it.
Time Format
Minute Hour Date month week command
Number range: 0-59 0-23 1-31 1-12 0-7 echo "hello"> abc. log
Meaning of special characters
* (Asterisks) indicates acceptance at any time.
, (Comma) indicates the meaning of the separated time periods.
-(Minus sign) indicates a period of time.
/N (diagonal line) that n represents a number, every n units interval.
Case1: Run once a year at on January 1, May 1.
5 10 1 5 * command
Case2: execute each task at three o'clock every day.
0, 6 * command # note that comma is used here
Case3: execute each operation at, and every day.
20 8-11 *** command # note that the minus sign is used here
Case4: execution every five minutes
*/5 *** command # note that/n is used here.
Case5: Run once every week
* 10 * * 1 command
Configure system-level scheduled tasks
Crontab-e is used to set user-level scheduled tasks. What should I do if I want to set a system-level scheduled task?
The answer is to directly use the root permission to edit the configuration file of the system-level scheduled task:/etc/crontab.
For example, you can directly use vim to open the Editing: