检测并重启挂掉的服务
- crontab
- crontab 日志
- 检测服务是否存在
crontab
- Linux crontab是用来定期执行程序的命令
- /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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
/etc/crontab 文件可以添加在某一时刻去执行相应的命令
- 常用的定时任务时刻
*/1 * * * * root echo >> /home/ # 每分钟执行
0 */12 * * * root echo >> /home/ # 每12小时执行
0 0 * * * root echo >> /home/ # 每天0点执行
0 12 1 * * root echo >> /home/ # 每月1号的12点执行
0 0 1,15 * * echo >> /home/ 每月1号和15号执行
- 1
- 2
- 3
- 4
- 5
可以在/crontab去计算cron表达式的执行时间
crontab 日志
- /var/log/cron 文件可以查看crontab的日志
tail -f /var/local/cron
- 1
检测服务是否存在
新建一个sh文件在任意位置,例如 /home/
#!/bin/sh
. /etc/profile
count=`ps -ef |grep "node /home/projects/node-test/node_modules/.bin/strapi start" |grep -v "grep" |wc -l`
#echo $count
if [ 0 == $count ];then
cd /home/projects/node-test
nohup npm run strapi start > /dev/null 2>&1 &
fi
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
-
上面的
. /etc/profile
可以结局shell脚本中无法获取环境变量的问题 -
设置 /etc/crontan 每分钟检测一次
*/1 * * * * root /bin/bash/home/
- 1
这样就能当没有找到这个服务的时候就重新启动服务的功能