linux-计划任务

发布于:2025-07-23 ⋅ 阅读:(15) ⋅ 点赞:(0)


一、crontab

linux 程序 crond

[root@hz shell]# ps aux|grep crond
root 794 0.0 0.0 8812 3712 ? Ss 17:50 0:00 /usr/sbin/crond -n
root 1525 0.0 0.0 6632 2432 pts/0 S+ 18:39 0:00 grep --color=auto crond

crond 每间隔1分钟就去检查每个用户的计划任务 --》最短时间间隔是1分钟

1. 配置文件

[root@hz shell]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
 
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr …
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
分 时 日 月 周


2. 编辑/创建一个计划任务

[root@hz shell]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab

3. 查看当前用户有哪些计划任务

[root@hz shell]# crontab -l
30 2 * * * bash /shell/backup_log.sh

脚本 +计划任务 = 解放了劳动力,提升效率 --》实现了自动化操作

示例
1.每隔5分钟执行脚本/shell/backup_log.sh
*/5 * * * * bash/shell/backup_log.sh

2.每间隔2个小时执行脚本/shell/back_log.sh
* */2 * * *

3.一个月的1号,10号,20-30号的2点30分执行脚本/shell/back_log.sh
30 2 1,10,20-30

4.星期1到星期5的2带你30分钟执行脚本/shell/back_log.sh
30 2 * * 1-5

, 表示单个的
- 表示连续的
/ 表示间隔
* 代表任意时间


4. 计划任务存放目录

每个用户会对应一个和用户名同名的计划任务文件

普通用户创建的计划任务也是会执行的,不管用户是否登录

[root@hz shell]# cd /var/spool/cron
[root@hz cron]# ls
root
[root@hz cron]# cat root
30 2 * * * bash /shell/backup_log.sh


二、计划任务的顺风车

linux系统本身也有一些任务需要定时完成的

/etc/下的这些文件夹里可以存放linux系统的计划任务

cron.d/ cron.daily/ cron.deny cron.hourly/ cron.monthly/ crontab cron.weekly/

cron.d/ 存放计划任务的目录
cron.daily/ 存放每天需要执行的任务
cron.hourly/ 存放每小时需要执行的任务
cron.monthly/ 存放每月需要执行的任务
cron.weekly/ 存放每周需要执行的任务
cron.deny 存放拒绝谁执行计划任务

[root@rocky-2 cron.d]# cd /etc/cron.hourly/
[root@rocky-2 cron.hourly]# ls
0anacron

编写关机的脚本

[root@rocky-2 cron.hourly]# vim shutdown.sh
init 0
[root@rocky-2 cron.hourly]# ll
总用量 8
-rwxr-xr-x. 1 root root 610 5月 2 17:15 0anacron
-rw-r–r-- 1 root root 7 7月 1 16:39 shutdown.sh

授予可执行权限

[root@rocky-2 cron.hourly]# chmod +x shutdown.sh
[root@rocky-2 cron.hourly]# ll
总用量 8
-rwxr-xr-x. 1 root root 610 5月 2 17:15 0anacron
-rwxr-xr-x 1 root root 7 7月 1 16:39 shutdown.sh

计划任务的顺风车-案例
新建一个脚本文件logrotate 存放在/etc/cron.daily

每天自动轮转系统日志

[root@rocky-2 cron.daily]# vim logrotate
/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf

授予logrotate脚本可执行权限

[root@rocky-2 cron.daily]# chmod +x logrotate
[root@rocky-2 cron.daily]# ll
total 4
-rwxr-xr-x 1 root root 82 Jul 1 17:03 logrotate


三、supervisor

Supervisor是一个用Python编写的进程控制系统,允许用户监控和控制类UNIX操作系统(linux)上的多个进程

supervisor 监督者

1. 安装Supervisor

首先安装epel源(网站),提供supervisor软件的下载,是官方最核心的软件存放的地方。

[root@rocky-2 ~]# yum install epel-release -y

然后安装supervisor

[root@rocky-2 ~]# yum install supervisor -y


2. 启动Supervisor并设置开机自启

[root@rocky-2 ~]# systemctl enable supervisord
Created symlink /etc/systemd/system/multi-user.target.wants/supervisord.service → /usr/lib/systemd/system/supervisord.service.

启动

[root@rocky-2 ~]# systemctl start supervisord
[root@rocky-2 ~]# ps aux|grep super
root 3554 0.0 0.2 30768 19252 ? Ss 10:14 0:00 /usr/bin/python3 -s /usr/bin/supervisord -c /etc/supervisord.conf
root 3556 0.0 0.0 3332 1664 pts/0 S+ 10:14 0:00 grep --color=auto super


3. 基本配置

  1. 主配置文件位置
    /etc/supervisord.conf

  2. 程序配置文件位置
    建议将各个程序的配置文件放在:/etc/supervisord.d/ 目录下,扩展名为 .ini

开启supervisor的web服务功能,其他配置不修改

[root@rocky-2 ~]# vim  /etc/supervisord.conf 
[inet_http_server]         ; inet (TCP) server disabled by default
port=*:9001        ; (ip_address:port specifier, *:port for all iface)
username=user              ; (default is no username (open server))
password=123               ; (default is no password (open server))

重启supervisord服务

[root@rocky-2 ~]# systemctl restart supervisord

使用浏览器去访问,ip地址是linux服务器的ip地址,输入的用户名是user 密码是123

登录界面

supervisor


4. 案例:监控ssh服务

监控ssh

1.编写监控ssh服务的shell脚本

[root@rocky-2 scripts]# vim ssh_monitor.sh
#!/bin/bash
#monitor ssh service
while true
do
	#check sshd process 统计包含/usr/sbin/sshd字符串的行数
	sshd_num=$( ps aux|grep "/usr/sbin/sshd"|wc -l)

	if (( $sshd_num == 1 ));then
		echo  "$(date) - sshd is down ,attempting to restart ..."   >> /var/log/ssh_monitor.log
		# try to start sshd service 
		systemctl  start sshd
		if (( $? == 0 ));then
			echo  "$(date) - sshd restarted successfully"  >>/var/log/ssh_monitor.log
		else
			echo  "$(date) - Failed to restarted sshd "  >>/var/log/ssh_monitor.log

		fi
	fi
        #stop  5 second
	sleep 5
done

设置脚本可执行权限:

[root@rocky-2 scripts]# chmod +x ssh_monitor.sh
[root@rocky-2 scripts]# ll
total 4
-rwxr-xr-x 1 root root 525 Jul 2 11:25 ssh_monitor.sh


2. 创建Supervisor配置文件

创建/etc/supervisord.d/ssh.ini

[root@rocky-2 scripts]# cd /etc/supervisord.d/
[root@rocky-2 supervisord.d]# vim ssh.ini
[program:ssh_monitor]
command=/bin/bash /opt/scripts/ssh_monitor.sh
process_name=%(program_name)s
autostart=true
autorestart=true
startretries=3
startsecs=5
stderr_logfile=/var/log/supervisor/ssh_monitor_stderr.log
stdout_logfile=/var/log/supervisor/ssh_monitor_stdout.log
user=root
environment=MONITOR_INIT=“supervisor”

下面是对ssh.ini配置文件的解释
ssh.ini


3. 重新加载Supervisor配置

[root@rocky-2 supervisord.d]# supervisorctl reread
[root@rocky-2 supervisord.d]# supervisorctl update
ssh_monitor: available
ssh_monitor: added process group

验证配置
检查Supervisor状态

[root@rocky-2 supervisord.d]# supervisorctl status
ssh_monitor RUNNING pid 5825, uptime 0:01:23
[root@rocky-2 supervisord.d]# ps aux|grep ssh_monitor
root 5825 0.0 0.0 4244 3072 ? S 11:37 0:00 /bin/bash /opt/scripts/ssh_monitor.sh
root 5959 0.0 0.0 3332 1664 pts/0 S+ 11:38 0:00 grep --color=auto ssh_monitor

web界面里查看的效果
在这里插入图片描述


4. 测试ssh故障恢复

[root@rocky-2 supervisor]# systemctl stop sshd

过几秒钟去查看sshd的进程是否有/usr/sbin/sshd进程存在

[root@rocky-2 supervisord.d]# ps aux|grep sshd
root 1337 0.0 0.1 20008 11008 ? Ss 08:45 0:00 sshd: root [priv]
root 1351 0.0 0.0 20008 6560 ? S 08:45 0:01 sshd: root@pts/0
root 4902 0.0 0.1 20012 11008 ? Ss 11:11 0:00 sshd: root [priv]
root 4906 0.0 0.0 20012 6604 ? S 11:11 0:00 sshd: root@pts/1
root 6406 2.0 0.1 16668 9216 ? Ss 11:43 0:00 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
root 6410 0.0 0.0 3332 1664 pts/0 S+ 11:43 0:00 grep --color=auto sshd

也可以查看日志

[root@rocky-2 supervisord.d]# tail -f /var/log/ssh_monitor.log
Wed Jul 2 11:42:00 CST 2025 - sshd is down ,attempting to restart …
Wed Jul 2 11:42:01 CST 2025 - sshd restarted successfully
Wed Jul 2 11:42:21 CST 2025 - sshd is down ,attempting to restart …
Wed Jul 2 11:42:21 CST 2025 - sshd restarted successfully
Wed Jul 2 11:43:36 CST 2025 - sshd is down ,attempting to restart …
Wed Jul 2 11:43:37 CST 2025 - sshd restarted successfully
Wed Jul 2 11:43:42 CST 2025 - sshd is down ,attempting to restart …
Wed Jul 2 11:43:42 CST 2025 - sshd restarted successfully
Wed Jul 2 11:43:47 CST 2025 - sshd is down ,attempting to restart …
Wed Jul 2 11:43:47 CST 2025 - sshd restarted successfully


总结

计划任务解决 “何时执行” 的问题,专注于时间驱动的周期性操作
Supervisor 解决 “持续运行” 的问题,专注于保障进程的稳定性和可用性


网站公告

今日签到

点亮在社区的每一天
去签到