Planning tasks in linux

Posted by simplyi on Tue, 28 Dec 2021 19:09:45 +0100

1, Custom scheduled tasks

1. atd service (one time)

1. Command corresponding to atd service

Installation is required before using the at command

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

Then the atd service must be started, otherwise it will not perform tasks regularly

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

The cases are as follows:

[root@localhost lianxi]# at 11:00 #Create a one-time scheduled task
at> bash /root/wang.sh
at> <EOT>     # ctrl+d 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 planned task number 3
[root@localhost lianxi]# at -l  #View the list of scheduled tasks
5	Wed Dec 22 11:00:00 2021 a root
[root@localhost lianxi]# 

2. Directory where one-time scheduled tasks are stored: / var/spool/at

The batch command is also equivalent to the at command, except that it runs the scheduled task when the system load is low

uptime command:

[root@localhost lianxi]# uptime  #Check how long the system has been powered on 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 after startup

1. The command corresponding to the crond Service -- "crontab" is the command to create and manage periodic planned tasks
- e , means to create a scheduled task (edit)
- l) view the list of scheduled tasks

2. Directory where periodic scheduled tasks are stored: / var/spool/cron

Configuration file of cron service: / etc/crontab

Log file of cron service: / var/log/cron (you can know whether a planned task is executed (CMD))

The format is shown in the figure:

Example: Example 1 (root user)
The sshd service is automatically started at 7:50 every morning and closed at 22:50
Clear the public directory Ivar/ftp/pub of FTP server every 5 days at 12:00 sharp
Restart the httpd service at 7:30 every Saturday
Pack and back up the / etc/httpd directory at 17:30 every Monday, Wednesday and Friday

[root@localhost lianxi]# crontab -e
crontab: installing 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. Meaning behind atd service and crond service

d -------- "daemon": a process that runs in memory until we stop it artificially. Otherwise, it runs in memory. Because it runs in memory, our users can access it at any time, so it always guards you and waits for your arrival.

2, Synchronization time

In CentOS 7: 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"  #Modification time
2021 Wednesday, 22 December 2015: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
2021 Wednesday, 22 December 2011 11:59:42 CST

In CentOS 8 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"
2021 Wednesday, 22 December 2015:06:12 CST

[root@localhost lianxi]# date
2021 Wednesday, 22 December:02:38 CST

3, Practice

  1. Write a script / backup/backup_log.sh backs up all files in the / var/log directory to the / backup directory. The file name is required to include the date of the day, accurate to seconds. For example, 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 the backup files in the last seven days are retained

  2. Execute the task as root user. The requirement for the planned task is to execute the above script / backup / backup at 4:30 every day_ log. sh

[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 if there are illegal planned tasks?

Where to see it?

Where does the crond process go to find the script to be executed?

1./var/spool/cron -- the place where user-defined scheduled tasks are stored

2. Planned tasks of the operating system itself -- (free ride)

[root@localhost lianxi2]# ls /etc/cron.*
/etc/cron.deny

/etc/cron.d:
0hourly

/etc/cron.daily:  Scripts to be executed for each machine
logrotate  man-db.cron

/etc/cron.hourly:  Scripts to be executed every hour
0anacron

/etc/cron.monthly:  Scripts to be executed every month

/etc/cron.weekly: Scripts executed weekly



[root@localhost lianxi2]# cat /etc/cron.deny users who disable the crond service can write to it

2. Hitchhiking cases

Examples of hitchhiking:
[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 

 

Topics: Linux Operation & Maintenance server