本文主要介绍Linux系统下,一个实现开机启动并将进程加入监控的简单方案。
主要完成两个shell脚本,会涉及到crontab的使用。
crontab的相关知识可以参考这篇文章:crontab基本用法及常见问题定位
方案介绍
简单方案就是使用两个shell脚本,一个脚本实现开机启动功能,另外一个脚本实现加入crontab监控的功能。
废话不多说,直接上代码
实现开机启动功能的脚本
Linux下实现开机启动的方法很多,本人采用将脚本放入/etc/init.d/目录下的办法。
脚本如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38!/bin/bash
description: add xxx to crontab and start process along with power on
load system functions
source /etc/init.d/functions
your application name
appname=cmd_python.py
your application path
apphome=/opt/
your log path
logpath=/var/log/task_test.log
search process.
pid=$( ps -ef | grep ${appname} | grep -v grep | grep -v vi | grep -v dbx | grep -v tail | grep -v start | grep -v stop| sed -n 1p | awk '{print $2}' )
if [ "a${pid}b" != "ab" ]; then
exist process pid
echo "servcie ${appname} was started."
exit 0
else
start your services or missions
cd ${apphome}
start python task, you can use your app task here!!!
python ./${appname}${appcomm} &
echo "servcie ${appname} was started." >> ${logpath}
fi
crontab -l | grep "${appname}"
if [ $? -eq 0 ]; then
exit 0
else
add to crontab
sh monitor.sh start
curTime=`date`
echo "${curTime}:add ${appname} to crontab." >> ${logpath}
fi
实现加入crontab监控的脚本
监控脚本:monitor.sh
1 | 监控任务: |