Use crontab to implement scheduled tasks (scheduled execution of scripts) on Linux (Centos)

Scenes

In Windows, database backup is implemented through bat scheduled execution commands and mysqldump:

Database backup is implemented in Windows through bat scheduled execution commands and mysqldump_mysqldump bat-CSDN blog

The above describes how to use bat to implement scheduled tasks in Windows. If it is on Linux, it can be implemented through crontab.

cron is the service name. crond is a background process, used to execute scripts, and will always run in the system.

crontab is a tool used to manage scheduled task lists. Customized scheduled tasks require starting the crond service (installed by default on the system).

The crond service is implemented through the crontab command.

Note:

Blog:
Domineering hooligan temperament_C#, architecture road, SpringBoot-CSDN blog

accomplish

1. First create a new sh script to clean the file contents under the specified folder.

Create a new clean.sh in the var/test directory

touch clean.sh

And modify the content as follows

#!/bin/bash

cd /var/test

>logs.log

exit

This script will clean the contents of the logs.log file in the test directory.

Create a new logs.log file and add content as you like.

2. Then add a scheduled task

Set a scheduled task and enter editing mode, the same as vim operation

crontab -e

Modify the content as follows

* * * * * /var/test/clean.sh

What follows is a cron expression, which means it will be executed once a minute. What follows is the command to execute.

For other crontab execution expressions, please refer to

crontab execution time calculation - online tool

3. After saving and exiting, you can add content to logs.log and verify the effect after one minute.

4. If you want to view the log, you can

tail -f /var/log/cron

Guess you like

Origin blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/134576630
Recommended