Ubuntu uses cron to implement scheduled tasks

Windows comes with a tool to execute tasks regularly called "Scheduled Tasks". Under Linux, we use Cron to achieve this function.

Install cron service##

Start the cron service##

Edit plan file##

crontab -e

To open the crontab file that your user belongs to. The first time you use this command, you will be asked to choose a text editor. I chose vim. The selected editor is also available

You can also change select-editor by yourself every time

Restart cron service##

After each crontab is saved, we also need to restart cron to apply this scheduled task. Use the command: sudo service cron restart

Management Task Schedule File##

All cron task plans are recorded in the crontab task plan file, and the task file is managed through the crontab command.

$ crontab -u root -e     #Edit the scheduled task file of user root

$ crontab -e            #Edit the current user's scheduled task file

$ crontab -u root -l     #Display the scheduled task file of user root

$ crontab -l            #Display the current user's scheduled task file

$ crontab -r            #Delete the current user's scheduled task file

cron syntax format##

m h dom mon dow   command
0- 590- 231- 311- 120- 7 command

In addition, you need to use some special symbols to achieve flexible configuration:

Task example##

## Specify specific execution time
2**** ls    #Execute the ls command once every second minute of the hour
307*** ls    #Execute the ls command once every day at 7:30
3020**2 ls    #Every Tuesday, execute the ls command at 20:30 (0 and 7 indicate Sunday)

## Specify interval time
* /2**** ls    #Execute the ls command every 2 minutes

## Specify time period
3073- 6** ls    #Execute the ls command at 7:30 on the 3, 4, 5, and 6th of each month

## Specify multiple times
3073,6** ls    #Execute the ls command at 7:30 on the 3rd and 6th of each month

In addition, use run-parts to run all scripts in the specified directory (note that the script must be added "#!/bin/bash", otherwise run-parts will fail to call)

307*** run-parts /home   #Run every day at 7:30/All scripts in the home directory

Recommended Posts

Ubuntu uses cron to implement scheduled tasks
Ubuntu uses Docker to install Gitlab
Ubuntu16.04 uses composer to install laravel framework
The use of crontab for ubuntu scheduled tasks
How to upgrade to Ubuntu 20.04
Ubuntu uses PlayOnLinux notes
Remote connection to Ubuntu 19.1
How to upgrade to Ubuntu 20.04
Mac uses vnc to remotely log in to the ubuntu16.04 desktop
Ubuntu 18.04 uses elastic network card to configure multiple external network IP