Linux scheduling, load balancing

1. Definition

Complete the task according to the plan and finish the task regularly

  • The user has set a scheduled task, but the user does not log in to the system. Will it still be executed every day?
    Yes, no matter whether the user logs in or not, he will perform the planned task

2. Use

During linux operation and maintenance:
Data backup: cpu, memory, disk and network broadband will be consumed during backup

3. Planned task classification

3.1 at command

Call the atd process and execute it only once. You need to start the atd service
/var/spool/at is the directory where one-time scheduled tasks are stored, and the file starting with a
When the task is completed, the file disappears

[root@lamp-test ~]# at 11:00 create a one-time scheduled task
at> bash /root/wang.sh
at> <EOT>
job 5 at Wed Dec 22 11:00:00 2021
[root@lamp-test ~]# at -l view the list of scheduled tasks
3	Wed Dec 22 10:20:00 2021 a root
5	Wed Dec 22 11:00:00 2021 a root
[root@lamp-test ~]# ATRM 3 deletes the task with planned task number 3

The batch command is the same as the at command. It submits tasks to atd and runs when the system is not busy

3.2 crontab command

Process name: crond (periodic task)
/var/spool/cron stores periodic scheduled tasks created by crontab
crontab service: call the crond process with a fixed cycle

crontab  -e Create scheduled task  
crontab -u liangluyao  -e   Create a scheduled task for the specified user  
crontab  -l  View scheduled tasks
crontab -u liangluyao  -l   View scheduled tasks for the specified user
crontab  -r  Delete scheduled tasks (delete all, dangerous)

The crond process checks the user's scheduled tasks at / var/spool/cron every 1 minute. The crond process runs when it is started. It is a daemon
daemon: a process that runs in memory until it is stopped artificially. Because it runs in memory, users can access it at any time, so it always guards you.

4. cron cycle planning task

Configuration file: / etc/crontab,
Log file: / var/log/cron. CMD flag in the log indicates that the planned task has been executed
Planned task file: / var/spool/cron stores the user's planned tasks

Time adjustment

ntpdate command synchronizes the time (with the time server on the Internet)
For centOS 7

[root@lamp-test log]# Date - s "2021-12-22 15:6:12" modification time
[root@lamp-test log]# yum install ntpdate -y
[root@lamp-test log]# ntpdate time.windows.com synchronization time
22 Dec 11:59:38 ntpdate[13102]: step time server 20.189.79.72 offset -11225.674351 sec

chrony services
For centOS 8

[root@lamp-test log]# rpm -qf /usr/sbin/chronyd query the installed software through the installed command
[root@lamp-test log]# yum install chrony -y
[root@hahah etc]# date -s "2021-12-12 15:15:15"  #adjust date
2021 Sunday, December 12, 2015:15:15 CST
[root@hahah etc]# service chronyd restart  #Synchronization time
Redirecting to /bin/systemctl restart chronyd.service
[root@hahah etc]# date
2021 Wednesday, 22 December:02:44 CST

Exercise 1: writing a planned task

50 7 * * * service sshd start
50 22 * * * service sshd stop
* 12 */5 * * rm -rf /var/ftp/pub/*
30 7 * *  6 service httpd restart
30 17 * *  1,3,5 tar -czf  /backup/httpd$(date +%F).tar.gz /etc/httpd

Exercise 2: script backup files

[root@hahah backup]# cat backup_log.sh
#!/bin/bash
mkdir -p /backup
tar -czf  /backup/$(date +%F_%H_%M_%S)-log.tar.gz   /var/log  # Absolute path should be taken when packing
find /backup -mtime +7 -type f -name  "*-log.tar.gz" -exec rm -rf {} \;

crontab -l View scheduled tasks
30 4 * * * bash /backup/backup_log.sh

6. anacron service

anacron service: it runs automatically when the system is powered on to make up for the problem that the cron service cannot perform scheduled tasks after the system is shut down, but it cannot replace cron
/var/spool/anacrond: it runs automatically at startup. arncron will stop automatically after unexecuted scheduled tasks are executed once

Check the system's outstanding cron tasks by day, week or month / var/spool/anacron
Service Name: / etc / init d/anacrond

[root@hahah backup]# cd /var/spool/anacron
[root@hahah anacron]# ll
 Total consumption 12
-rw-------. 1 root root 9 12 October 22:32 cron.daily
-rw-------. 1 root root 9 12 March 16:08 cron.monthly
-rw-------. 1 root root 9 12 March 20:30 cron.weekly

7. Security issues

How to find out if there are illegal planned tasks?

  • /var/spool/cron user defined scheduled tasks
  • /etc/cron.daily,/etc/cron.hourly,/etc/cron.monthly,/etc/cron. Scheduled tasks for weekly operating system
    The crond process will go to these places.
[root@hahah cron]# ls /etc/cron*
/etc/cron.deny blacklist  /etc/crontab crond Total profile for

/etc/cron.d:   Directory for storing scheduled tasks
0hourly

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

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

/etc/cron.weekly: Scripts to be executed every week

/etc/cron.d/0hourly  See the following figure for the contents of the document
[root@lamp-test cron.d]# cat 0hourly 
# Run the hourly jobs
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
01 * * * * root run-parts /etc/cron.hourly
 Every hour /etc/cron.hourly The script in the folder leads to a free ride
run-parts Is a command that will traverse the scripts in the folder to execute -->for

Free ride

In / etc / cron Write scripts under the hourly directory to give executable permissions. This script will be executed regularly later

[root@lamp-test cron.hourly]# pwd
/etc/cron.hourly
[root@lamp-test cron.hourly]# ls
0anacron  poweroff.sh
[root@lamp-test cron.hourly]# cat poweroff.sh 
init 0
[root@lamp-test cron.hourly]# chmod +x poweroff.sh 

load balancing

System performance: cpu utilization, memory utilization, disk IO, network IO, number of processes

[root@hahah etc]# uptime checks how long the system has been running and the average load of the system
 10:28:58 up 11:24,  4 users,  load average: 0.00, 0.00, 0.00

the system load averages for the past 1, 5, and 15 minutes.
The average load of the system in the past 1 minute, 5 minutes and 15 minutes.

System load averages is the average number of processes that are either in a runnable or uninterruptable state.
The average system load is the average number of processes in a running or non interruptible state.

A process in a runnable state is either using the CPU or waiting to use the CPU. A process in a non interruptible state is waiting for some I/O access, such as waiting for a disk. The average is taken over three time intervals. The average load is not normalized according to the number of CPUs in the system, so an average load of 1 means that a single CPU system is always loaded, while in four CPU systems, it means that 75% of the time is idle.

Keywords: Linux Operation & Maintenance Load Balance

Added by Redlightpacket on Mon, 03 Jan 2022 01:54:12 +0200