LiangGaRy-Study Notes-Day22

1. shell tool-tput

This is the tput bash tool

The specific operation is as follows:

  • tput clear: Qingping
  • tput cup Y X
    • Row Y, column X position
  • tput bold: bold font
  • tput sgr0 : reset command
  • tput setaf n
    • n: represents the number 0-7
      • 0 black
      • 1 red
      • 2 green
      • 3 yellow
      • 4 blue
      • 5 magenta
      • 6 cyan (blue-green)
      • 7 white
  • tput rew: set subtitle

Function: Through the Terminfo database, you can initialize a terminal session or change terminal functions, such as moving or changing the cursor, changing text attributes, and clearing specific areas of the terminal screen.

Syntax: tput+options

1.1, shell script-LAMP+Wordpress

  • install httpd
  • Install MySQL
  • Install PHP
  • install wordpress
#编写脚本
[root@Node1 sh]# vim lamp.sh
#!/bin/bash
#Time:2023年6月12日
#Author By LiangGaRy
#Usage:结合tput简单部署lamp脚本
#############################
        #设定函数
clean_OS(){
    
    
        #卸载与挂载光盘
        umount /dev/cdrom /mnt/cdrom &>> /dev/null
        mount /dev/cdrom /mnt/cdrom &>> /dev/null
        #删除原来的yum的进程pid
        rm -rf /var/run/yum.pid &>> /dev/mull
        #请空缓存与重新生成yum 缓存
        yum clean all &>> /dev/null
        yum makecache fast &>/dev/null
}
http_install(){
    
    
        yum -y install httpd &>> /dev/null
        systemctl restart httpd &>> /dev/null
        if [ $? -eq 0 ];then
                echo "当前httpd 安装成功;"
        else
                echo "httpd安装失败,请手工排查问题 "
        fi
}
mysql_install(){
    
    
        yum -y install mariadb mariadb-server &>> /dev/null
        systemctl restart mariadb &>> /dev/null
        if [ $? -eq 0 ];then
                echo "当前mariadb 安装成功;"
        else
                echo "mariadb安装失败,请手工排查问题 "
        fi
}
        #实现清屏功能
tput clear
        #定位光标的位置
tput cup 3 15
        #设置颜色
tput setaf 3
echo “LAMP安装面板”
        #重置命令
tput sgr0
        #定位光标的位置
tput cup 5 17
        #设置一个小标题
tput    rev
echo "LAMP 管理面板"
        #定位光标的位置
tput cup 7 15
echo "1.安装httpd"
tput cup 8 15
echo "2.安装MySQL"
tput cup 9 15
echo "3.安装PHP"
tput cup 10 15
echo "4.安装WORDPRESS"
        #设置字体加粗
tput bold
tput cup 12 15
read -p "请输入你的选择【1-4】:" choice
case $choice in
1)
        echo "httpd 开始安装中........"
        #然后开始调用函数
        clean_OS
        http_install
;;
2)
        clean_OS
        echo "MySQL 开始安装中........"
        mysql_install
;;
3)
        clean_OS
        echo "PHP 开始安装中........"
        php_install
;;
4)
        clean_OS
        echo "Wordpress 开始安装中........"
        wordpress_install
;;
*)
        echo "请输出数字1-4!!"
;;
esac

Check out the function

2. Planning tasks

Scheduled tasks are to let the system execute certain tasks (programs) at a specified time in the future; tasks can be performed periodically or only once

  • For example: 6:30 in the morning –> perform inspection tasks;
  • Precautions:
    • To enable scheduled tasks to run automatically at a specified time, the server must be powered on

2.1, at service explanation

Definition: Specify a certain time to execute a task at one time; rely on the system background process atd process

  • Automatically execute some pre-set command operations at the specified date and time point, which is a one-time scheduled task
  • The name of the system service: /etc/init.d/atd
  • Setting format: at [HH:MM] [yyyy-mm-dd]
  • atq command: query the currently set at task list same as at -l
  • atrm command: delete the at task with the specified task number atrm number is the same as at -d
  • at -c task number View the specific content of the scheduled task (same content as the at scheduled task file /var/spool/at/file starting with a)

Two files:

  • /etc/at.deny: only people in the list can not use
  • /etc/at.allow: Only people in this list can use it (this file generally does not exist, if necessary, create it yourself)

Case description:

#启动atd进程
[root@Node1 ~]# systemctl start atd
	#查看服务状态
[root@Node1 ~]# systemctl status atd
	#查看是否开机自启
[root@Node1 ~]# systemctl is-active atd
active
	#确保时间是准确的
[root@Node1 ~]# date
Wed Jul 12 15:23:53 CST 2023

#添加一次性计划任务-->添加ctrl+d结束
[root@Node1 ~]# at 15:30
at> echo "hello the word"
at> <EOT>
job 1 at Wed Jul 12 15:30:00 2023

#查看一次性任务列表
[root@Node1 ~]# at -l
1	Wed Jul 12 15:30:00 2023 a root
	#输出的信息说明
		#1:任务编号:
		#Wed Jul 12 15:30:00 2023:执行的时间	
		#a:队列
		#root:执行者root

#在添加一次性任务
at> echo "hello liangjiawei"   
at> echo "welcome to Linux"
at> <EOT>
job 2 at Wed Jul 12 16:00:00 2023

#指定一个时间
[root@Node1 ~]# at 11:00 2023-10-31
at> echo lalal
at> <EOT>
job 3 at Tue Oct 31 11:00:00 2023
You have mail in /var/spool/mail/root

#指定这个时间后的30分钟执行
[root@Node1 ~]# at now +30min
at> echo aaa
at> <EOT>
job 4 at Wed Jul 12 16:04:00 2023

#指定三天后的计划任务
[root@Node1 ~]# at 18:00 +3days
at> echo bbb
at> <EOT>
job 5 at Sat Jul 15 18:00:00 2023

#查看计划任务
[root@Node1 ~]# at -l
2	Wed Jul 12 16:00:00 2023 a root
3	Tue Oct 31 11:00:00 2023 a root
4	Wed Jul 12 16:04:00 2023 a root
5	Sat Jul 15 18:00:00 2023 a root

#删除计划任务
[root@Node1 ~]# at -d 3
[root@Node1 ~]# at -l
2	Wed Jul 12 16:00:00 2023 a root
4	Wed Jul 12 16:04:00 2023 a root
5	Sat Jul 15 18:00:00 2023 a root

#查看计划任务的内容
[root@Node1 ~]# at -c 2
#!/bin/sh
# atrun uid=0 gid=0
.........
echo "hello liangjiawei"
echo "welcome to Linux"

2.2. Backup instructions

Full backup:

  • The so-called full backup is to use a tape to fully back up the entire system, including the system and data.
  • Features:
    • The most common backup type. Backup complete data, easy to restore
    • Backup takes a long time, and backups are repeated, taking up a lot of storage space

Incremental backup:

  • That is, the data backed up each time is only equivalent to the added and modified data after the last backup.
  • Features:
    • The amount of backup data is small and the backup speed is fast.
    • Relatively speaking, the required recovery time is longer than that required for full or differential backups.

differential backup

  • That is, the data backed up each time is the newly added and modified data relative to the last full backup.
  • Features:
    • The amount of backup data is small, and the backup speed is faster than full backup.
    • Relatively speaking, it takes longer to restore data than a full backup. Differential backups take longer than incremental backups if a lot of data changes

Things backup :

  • Backup the transaction log in the database. A transaction log is a series of records of all modifications that have occurred in a database and the transactions that performed each modification.
  • Features:
    • Using transaction log backups, data can be recovered to the precise point of failure.

forever incremental backup

  • Incremental-forever backups are often used by disk-to-disk-tape backup systems. Its basic method is similar to incremental backup. Permanent incremental backup starts with a full backup of all data, and only incremental backups are performed later.
  • Features:
    • The amount of backup data is small, and the backup speed is faster than incremental backup.
    • Relatively speaking, it is suitable for application scenarios where a single application has a large amount of data and it is time-consuming and laborious to perform a full backup

2.3, crond service

Role: Execute periodic scheduled tasks

  • Repeat the command operation performed by the user according to the preset time period (minutes, hours, days...), which belongs to the periodic planner task
  • Service name: /etc/init.d/crond
  • Main settings file: user-defined settings, located in the file /etc/spool/cron/username
  • cron service configuration file: /etc/crontab (contains three parts: comments, environment variables, cron command)
  • Cron service log file: /var/log/cron
  • Manage cron scheduled tasks:
    • Edit crontab: crontab -e [-u username]
    • View scheduled tasks: crontab -l [-u username]
    • Delete a scheduled task: crontab -r [-u username ]
  • The root user can manage the scheduled user tasks, and other users can only manage their own scheduled tasks
  • grammar:
    • Time-sharing day-month-week command
  • Representation of time:
    • Model*: Indicates any time within the range
    • Comma, : Indicates multiple discontinuous time points in the interval
    • Bar -: Indicates a continuous time range
    • /n: Indicates the time frequency of the specified interval
0  17  *  *  1-5		  周一到周五每天17:00
30  8  *  *  1,3,5	      每周一、三、五的8点30分
0  8-18  *  *  *          8点到18点整
0  12  */3  *  *		  每隔3天的12点整

crontab command

  • Function: set and view periodic scheduled tasks
  • Syntax: crontab+option+time
  • options:
    • -u: specify the user
    • -l: list detailed tasks
    • -r: delete scheduled tasks
    • -e: edit scheduled tasks
#启动crond服务
[root@Node1 ~]# systemctl  start crond
[root@Node1 ~]# systemctl enable crond

#编辑任务
[root@Node1 ~]# crontab -uroot -e
no crontab for root - using an empty one
1 * 5 * * echo aaa

#查看计划任务
[root@Node1 ~]# crontab  -l
1 * 5 * * echo aaa

#指定liangjiawei用户进行编辑
[root@Node1 ~]# crontab  -uliangjiawei -e
no crontab for liangjiawei - using an empty one
1 2 4 3 * echo lll

#再次查看
[root@Node1 ~]# crontab  -uliangjiawei -l
1 2 4 3 * echo lll

#每天晚上23:00重启httpd
0 23 * * * /etc/init.d/httpd restart

#每隔五分钟执行
*/5 * * * * echo aaa >> a.txt

#每周的135的12点和18点重启httpd
0 12,18 * * 1,3,5 /etc/init.d/httpd restart

#每天都饿12点到18点重启apache
0 12-18 * * * /etc/init.d/httpd restart

System-level scheduled tasks

  • The system will clear the system cache
  • The system will temporarily clean up the files
  • The system collects system information
#查看系统级别的定时任务
[root@Node1 ~]# ls /etc/cron
cron.d/       cron.deny     cron.monthly/ cron.weekly/  
cron.daily/   cron.hourly/  crontab

#文件说明如下:
cron.deny 		#控制用户是否能做计划任务的文件;
cron.monthly/ 	#每月执行的脚本;
cron.weekly/ 	#每周执行的脚本;
cron.daily/ 	#每天执行的脚本;
cron.hourly/ 	#每小时执行的脚本;
crontab 		#主配置文件 也可添加任务;

Reasons for scheduled tasks not executing

  • The script was written incorrectly and there is no way to execute it
  • Wrong execution environment; no execution permission
  • The system time is incorrect, resulting in non-execution at the specified time
  • The crontab daemon process is down and does not execute

log :

/var/log/message 		#系统默认的日志信息
/var/log/secure 		#ssh登录服务
/var/log/maillog 		#邮件相关
/var/log/cron 			#定期执行任务相关的日志
/var/log/boot.log
/var/log/dmesg

2.4, watch command

Function: The output of the command can be output to the standard output device, which is mostly used for periodic execution of commands/timing execution of commands

Syntax: watch + option + command

options:

  • -n: Specify the interval time, the default is 2 seconds
  • -c: Clear the screen to display the results
  • –color: display color
#每隔一秒高亮显示网络链接数的变化情况
[root@Node1 sh]# watch -n 1 -d netstat -ant
Every 1.0s: netstat -ant                                Wed Jul 12 16:31:04 2023
............

3. Script exercise

3.1. Check the total number of users in the system

Troubleshoot scheduled tasks for all users

#编写脚本
[root@Node1 sh]# vim user_count.sh
#!/bin/bash
#Time:2023年6月12日
#Author By LiangGaRy
#Userage:用于统计用户以及计划任务
#############################
name_total=$(wc -l < /etc/passwd)
names=$(cat /etc/passwd | awk -F : '{print $1}')
echo "当前系统中一共有$name_total个用户"
for i in $names
do
        crontab -u$(name) -l
        if [ $? -eq 0 ];then
        echo "$i 有计划任务;"
        fi
done

#执行查看
[root@Node1 sh]# bash user_count.sh 

3.2. Scheduled task script

Automatically enter time and generate scheduled tasks

[root@Node1 sh]# vim auto_cron.sh
#!/bin/bash
#Time:2023年6月12日
#Usage:用户自己输入自己想要的计划任务
########################
read -p "请输入分钟信息(00-59):" min
read -p "请输入小时信息(00-24):" hour
read -p "请输入日期信息)01-31):" date
read -p "请输入月份信息(01-12):" month
read -p "请输入信息信息(00-06):" week
read -p "请输入计划任务需要执行的命令或者脚本:" program
echo "$min $hour $date $month $week $grogram" >> /etc/crontab

Guess you like

Origin blog.csdn.net/Liang_GaRy/article/details/131175001