Introduction to scheduled tasks in Linux system

Introduction to scheduled tasks in Linux system

1. Customize plan tasks

1. ATD service (one-time)

1. Commands corresponding to atd service -----》at

To use the at command, you must first install

[root@localhost lianxi]# yum install at -y

Then you must start the atd service, otherwise the task will not be executed regularly.

[root@localhost lianxi]# service atd start
Redirecting to /bin/systemctl start atd.service

The following is an example:

[root@localhost lianxi]# at 11:00 #Create a one-time scheduled taskat> bash /root/wang.sh
at> <EOT> # ctrl+d to exit job 5 at Wed Dec 22 11:00:00 2021
[root@localhost lianxi]# at -l
3 Wed Dec 22 10:20:00 2021 a root
5 Wed Dec 22 11:00:00 2021 a root
[root@localhost lianxi]#
 
[root@localhost lianxi]# atrm 3 #Delete the task with the scheduled task number 3 [root@localhost lianxi]# at -l #View the scheduled task list 5 Wed Dec 22 11:00:00 2021 a root
[root@localhost lianxi]#

2. Directory for storing one-time scheduled tasks: /var/spool/at

The batch command is equivalent to the at command, except that it runs scheduled tasks when the system load is relatively low.

uptime command:

[root@localhost lianxi]# uptime #Check how long the system has been up and the average load of the system 10:28:33 up 4 days, 16:13, 2 users, load average: 0.00, 0.01, 0.05

2. The crond service (periodic) service is installed by default and starts automatically at boot time

1. Commands corresponding to crond service ------》crontab is a command for creating and managing periodic scheduled tasks
-e means to create a scheduled task (edit)
-l View the scheduled task list

2. Directory for storing periodic scheduled tasks: /var/spool/cron

Configuration file for cron service: /etc/crontab

Cron service log file: /var/log/cron (can know whether a scheduled task is executed (CMD))

The format is as shown:

Example: "Example 1 (root user)"
The sshd service is automatically started at 7:50 am every day and closed at 22:50. The FTP server public directory Ivar/ftp/pub is cleared every 5 days at 12:00.
Restart the httpd service at 7:30 every Saturday. Back up the /etc/httpd directory at 17:30 every Monday, Wednesday, and Friday.

[root@localhost lianxi]# crontab -e
crontab: installing a new crontab
[root@localhost lianxi]# crontab -l
30 3 * * * bash /root/sc.sh
50 7 * * * service sshd start
50 22 * ​​* * service sshd stop
0 12 */5 * * rm -rf /var/ftp/pub/*
30 7 * * 6 service httpd restart
30 17 * * 1,3,5 tar czf /backup/httpd.tar.gz /etc/httpd
30 4 * * * /bin/bash /lianxi/backup/backup_log.sh
[root@localhost lianxi]#

3. The meaning of "d" after atd service and crond service

d------》Daemon: A process that runs in memory all the time until we stop it manually. Otherwise, it keeps running in memory. Because it keeps running in memory, our users can access it at any time, so it keeps guarding you and waiting for your arrival.

2. Synchronize time

In centos7: Use the ntpdate command

#The first step is to install [root@localhost lianxi]# yum install ntpdate -y
 
#Step 2 [root@localhost lianxi]# date -s "2021-12-22 15:6:12" #Modified time Wednesday, December 22, 2021 15:06:12 CST
 
#Step 3 [root@localhost lianxi]# ntpdate time.windows.com
22 Dec 11:59:38 ntpdate[13102]: step time server 20.189.79.72 offset -11225.674351 sec
[root@localhost lianxi]# date
Wednesday, December 22, 2021 11:59:42 CST

In centos8 or 7: Use chrony

[root@localhost lianxi]# yum install chrony -y #Install [root@localhost lianxi]# service chronyd restart #Restart service Redirecting to /bin/systemctl restart chronyd.service
 
[root@localhost lianxi]# date -s "2021-12-22 15:6:12"
Wednesday, December 22, 2021 15:06:12 CST
 
[root@localhost lianxi]# date
Wednesday, December 22, 2021 12:02:38 CST

3. Practice

1. Write a script /backup/backup_log.sh to back up all files in the /var/log directory to the /backup directory. The file name must contain the date of the day, accurate to the second. For example, the file name is: 2016-6-6-2_30_20-log.tar.gz.
At the same time, it is required to delete the backup files in the /backup directory seven days ago, and only keep the backup files in the last 7 days.

2. Execute as root user. The requirement of the scheduled task is to execute the above script /backup/backup_log.sh at 4:30 every day

[root@lamp-test backup]# pwd
/backup
[root@lamp-test backup]# cat backup_log.sh 
#!/bin/bash
mkdir -p /backup
tar -czf /backup/$(date +%F_%H_%M_%S)-log.tar.gz /var/log/*
find /backup -mtime +7 -type f -name "*.tar.gz" -exec rm -rf {} \;
[root@lamp-test backup]# crontab -l
30 4 * * * bash /backup/backup_log.sh

4. Security Issues

1. How to find out illegal scheduled tasks? Where to go to see it? Where does the crond process look for the script to be executed?

1./var/spool/cron --》Where user-defined scheduled tasks are stored

2. The operating system's own scheduled tasks -- (ride-sharing)

[root@localhost lianxi2]# ls /etc/cron.*
/etc/cron.deny
 
/etc/cron.d:
0hourly
 
/etc/cron.daily: The script logrotate man-db.cron that needs to be executed on each machine
 
/etc/cron.hourly: Script 0anacron that needs to be executed every hour
 
/etc/cron.monthly: script to be executed every month /etc/cron.weekly: script to be executed every week [root@localhost lianxi2]# cat /etc/cron.deny Users who disable the crond service can write to it

2. Hitchhiking Case

Hitchhiking example:
[root@lamp-test cron.hourly]# pwd
/etc/cron.hourly
[root@lamp-test cron.hourly]# ls
0anacron poweroff.sh
[root@lamp-test cron.hourly]# chmod +x poweroff.sh 
[root@lamp-test cron.hourly]# cat poweroff.sh 
init 0
 
 
 
[root@lamp-test log]# cd /etc/cron.hourly/
[root@lamp-test cron.hourly]# ls
0anacron poweroff.sh
[root@lamp-test cron.hourly]# rm -rf poweroff.sh

This is the end of this article about scheduled tasks in Linux system. For more relevant Linux scheduled tasks, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of using the at command for one-time scheduled tasks in Linux
  • How to customize at and cron scheduled tasks in Linux
  • Detailed explanation of how to use cron scheduled tasks in Linux
  • Brief discussion: Summary of commonly used symbols for Linux cron scheduled tasks

<<:  Summary of commonly used escape characters in HTML

>>:  MySQL process control IF(), IFNULL(), NULLIF(), ISNULL() functions

Recommend

Notes on matching MySql 8.0 and corresponding driver packages

MySql 8.0 corresponding driver package matching A...

How to output Chinese characters in Linux kernel

You can easily input Chinese and get Chinese outp...

How to set the text in the select drop-down menu to scroll left and right

I want to use the marquee tag to set the font scro...

CSS style solves the problem of displaying ellipsis when the text is too long

1. CSS style solves the problem of displaying ell...

js implements a simple English-Chinese dictionary

This article shares the specific code of js to im...

Things to note when designing web pages for small-screen mobile devices

The reason is that this type of web page originate...

How to solve "Unable to start mysql service error 1069"

Today, when I was on the road, a colleague sent m...

Graphical explanation of the underlying principle of JavaScript scope chain

Table of contents Preface Scope 1. What is scope?...

About the selection of time date type and string type in MySQL

Table of contents 1. Usage of DATETIME and TIMEST...

Docker core and specific use of installation

1. What is Docker? (1) Docker is an open source t...

Using vsftp to build an FTP server under Linux (with parameter description)

introduce This chapter mainly introduces the proc...

Markup Language - Title

Click here to return to the 123WORDPRESS.COM HTML ...

Detailed explanation of MySQL/Java server support for emoji and problem solving

This article describes the support and problem so...

HTML validate HTML validation

HTML validate refers to HTML validation. It is the...