Linux system environment crontab scheduled task setting parameter examples and common techniques

888u

Last update at :2024-07-09,Edit by888u

Under normal circumstances, we may use the configuration of crontab scheduled tasks when operating and maintaining VPS and servers. For example, scheduled server restarts, scheduled data backups and other operations are often used. The task configuration using the crontab command is inseparable here. In this article, Snail will sort out a relatively complete method of using the crontab command and common example operations.

From the picture above, we can actually see the specific usage of crontab. There are a total of 6 paragraphs. The first 5 mark the scheduled time setting, and the last one marks the file path for scheduled execution.

First, crontab installation

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

1. Centos installation crontab

#Install Crontabyum install vixie-cron crontabs#Set up Crontabchkconfig crond on#Start Crontabservice crond start

2. Debian installation crontab

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

Ensure that the system is installed and running before making relevant settings.

Second, scheduled task case

1, 35 * * * * updatedb

The database is updated every hour and 35 minutes.

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

Run diskusage.sh file every 5 minutes.

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

Execute file every Tuesday at 1:50AM

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

Executed at 2 pm on the tenth day of March, June, September and December every year.

5, 00 21 * * Mon,Wed,Fri /usr/local/bin/diskusage.sh [email protected]

Execute the script every Monday, Wednesday and Friday at 9pm

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

Executes every five minutes from Monday to Friday.

Third, system scheduling

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

If we are executing the backup.sh backup command in our root directory, we can use the above script to directly replace it to the /etc/cron.weekly directory.

Fourth, level setting of scheduled plans

When we are operating, each user can edit, view, and delete the crontab timing configuration file. If the root user needs to change the crontab file, the \\’-u\\’ option must be added to specify the user name.

1. When editing the crontab file as user laozuo, we can use the command:

crontab -u laozuo -e

2. Remove the timing settings of laozuo user

crontab -u laozuo -r

3. View laozuo scheduled task setting file

crontab -u laozuo -l

Fourth, load crontab from file

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

cat /root/mycronjobs.txt53 00 * * 7 /bin/sh /root/server-backup.sh01 00 * * * /bin/sh /root/check-user-quota.sh

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

crontab /root/mycronjobs.txt

Check validation with this command.

crontab -l53 00 * * 7 /bin/sh /root/server-backup.sh01 00 * * * /bin/sh /root/check-user-quota.sh

Here we can check whether the scheduled file exists.

Fifth, add SELinux security options

crontab -s /root/mycronjobs/my.txtSELINUX_ROLE_TYPE=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c102353 00 * * 7 /bin/sh /root/server-backup.sh01 00 * * * /bin /sh /root/check-user-quota.sh

Sixth, delete scheduled tasks

[[email protected] ~]# crontab -l53 00 * * 7 /bin/sh /root/server-backup.sh01 00 * * * /bin/sh /root/check-user-quota.sh

[[email protected] ~]# crontab -r

[[email protected] ~]# crontab -lno 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 summarize, the above 6 steps and methods are commonly used for crontab timing file configuration. Generally, we will use backup and scheduled startup, etc., and then adjust the time according to our actual needs.

Recommended site search: dynamic ip server, permanent free linux server, wordpress host, Japanese server free, registration inquiry, server hosting, how much does it cost to rent a server for a year, vps dynamic ip server, which Hong Kong server is the fastest, change IP in seconds Server,

Linux system environment crontab scheduled task setting parameter examples and common techniques

All copyrights belong to 888u unless special state
取消
微信二维码
微信二维码
支付宝二维码