Focus on cloud service provider activities
Notes on website operation and maintenance

Example and common skills of setting parameters for crontab scheduled task in Linux system environment

Generally, we may use it when operating and maintaining VPS and servers crontab The configuration of scheduled tasks, such as the scheduled restart of the server and the scheduled backup of data, is still used more frequently. This is inseparable from the task configuration of using the crontab command. In this article, Lao Zuo will sort out more complete methods of using the crontab command and commonly used instance operations.

 Example and common skills of setting parameters for crontab scheduled task in Linux system environment

From the above figure, we can actually see the specific usage of crontab. There are six paragraphs in total. The first five indicate the timing settings, and the next one indicates the path of the file to be executed regularly.

First, crontab installation

Generally, the centos or debian distribution we use comes with crontab timing software support. If the system does not support it, we only need to install it through the following corresponding installation.

1. Centos installation crontab

#Install Crontab
yum install vixie-cron crontabs
#Set the startup of Crontab
chkconfig crond on
#Start Crontab
service crond start

2. Debian Install crontab

#Install Crontab
apt-get install cron
#Restart Crontab
/etc/init.d/cron restart

Make sure the system is installed and running effectively before setting.

Second, timed task cases

1、35 * * * * updatedb

The database is updated every hour and 35 minutes.

2、*/5 * * * * /usr/local/bin/diskusage.sh

Run the diskusage.sh file every 5 minutes.

3、25,50 1 15 * 2 /usr/local/bin/diskusage.sh

Execute documents at 1:50AM every Tuesday

4、00 14 10 3,6,9,12 * /usr/local/bin/diskusage.sh

It is implemented at 2:00 p.m. on the tenth day of March, June, September and December every year.

5、00 21 * * Mon,Wed,Fri /usr/local/bin/diskusage.sh admin@laozuo.org

Execute script every Monday, Wednesday and Friday at 9:00 p.m

6、*/5 * * * 1-5 /usr/local/bin/diskusage.sh

Monday to Friday, every five minutes.

Third, system scheduling

/etc/cron.d /etc/cron.daily /etc/cron.hourly /etc/cron.monthly /etc/cron.weekly

If our root directory is executing the backup.sh backup command, the above script can be directly replaced to the/etc/cron.weekly directory.

Fourth, level setting of timing plan

When we operate, each user can edit, view, and delete the crontab timing profile. If the root user needs to change the crontab file, you must add the '- u' option to specify the user name.

1. When laozuo, the user who edits the crontab file, we can use the command:

crontab -u laozuo -e

2. Remove the timing settings of laozuo users

crontab -u laozuo -r

3. View the laozuo scheduled task settings file

crontab -u laozuo -l

Fourth, load crontab from the file

In addition to manually editing the timing file, we can also load the file into the timing file.

cat /root/mycronjobs.txt
53 00 * * 7 /bin/sh /root/server-backup.sh
01 00 * * * /bin/sh /root/check-user-quota.sh

We need to upload the edited mycronjobs.txt scheduled task file to the directory before execution.

crontab /root/mycronjobs.txt

Check the verification through this command.

crontab -l
53 00 * * 7 /bin/sh /root/server-backup.sh
01 00 * * * /bin/sh /root/check-user-quota.sh

Here we can check whether the timing file exists.

Fifth, add SELinux security option

crontab -s /root/mycronjobs/my.txt
SELINUX_ROLE_TYPE=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
53 00 * * 7 /bin/sh /root/server-backup.sh
01 00 * * * /bin/sh /root/check-user-quota.sh

VI. Deleting a Scheduled Task

[ root@linux ~]# crontab -l
53 00 * * 7 /bin/sh /root/server-backup.sh
01 00 * * * /bin/sh /root/check-user-quota.sh [ root@linux ~]# crontab -r [ root@linux ~]# crontab -l
no crontab for root

We first use - l to view the current scheduled task, then use - r to delete it, and then check that there is no task option.

To sum up, the above six steps and methods are commonly used to configure crontab timing files. Generally, we use backup and scheduled startup, and adjust the time according to our actual needs.

Domain name host preferential information push QQ group: six hundred and twenty-seven million seven hundred and seventy-five thousand four hundred and seventy-seven Get preferential promotion from merchants.
Like( zero )
Do not reprint without permission: Lao Zuo's Notes » Example and common skills of setting parameters for crontab scheduled task in Linux system environment


Scan the code to follow the official account

Get more news about webmaster circle!
Entrepreneurship, operation and new knowledge