ubuntu开机自启动并且崩溃自启动程序/服务

ubuntu开机自启动并且崩溃自启动程序/服务

1 开机自启动

参考ubuntu程序自启动:https://blog.csdn.net/u013894391/article/details/89405738

2 崩溃自启动

2.1 崩溃自启动思路

启动脚本中加入检测程序是否运行的程序。当程序运行时 使用

ps -aux | grep your_bin | grep -v grep

通过运行的二进制程序名称,查看系统运行进程中是否有该进程。其中 your_bin是运行的二进制名词。若存进程存在则睡眠10S,若进程不存在,则启动程序,再睡眠十秒。

2.2 崩溃自启动脚本代码

yourbash.sh

#!/bin/sh
### BEGIN INIT INFO
# Provides:          HMI.sh
# Required-start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the HMI.sh daemon
# Description:       starts HMI.sh using start-stop-daemon
### END INIT INFO

#set -e
sleep 5    #启动前的睡眠时间

proc_name="your_bin_name"     #你的要崩溃自启的程序名词

proc_num()
{
	#通过名称查询系统的线程
	num=`ps -ef | grep "your_bin_name" | grep -v grep | wc -l`
	return $num
}
while true
do
	
	proc_num  #运行proc_num函数 输出
	number=$?	#从输出端获取数据
	if [ $number -eq 0 ]	#若线程数为0,即崩溃。
	then
		cd /your_bin_catalog  #重启
		setsid ./your_bin &
		echo "process has been restarted!"
	else
		echo "process already started!"
	fi
	sleep 10   #睡眠时间
done

2.3崩溃自启动脚本

新建一个setup.sh脚本文件,并将setup.shyourbash.sh 移动到/etc/init.d目录下。

#!/bin/sh
### BEGIN INIT INFO
# Provides:          HMI.sh
# Required-start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the HMI.sh daemon
# Description:       starts HMI.sh using start-stop-daemon
### END INIT INFO
cd /etc/init.d
setsid sh yourbash.sh &
exit 0

2.4将setup.sh添加到系统自启动中

参考ubuntu程序自启动:https://blog.csdn.net/u013894391/article/details/89405738

猜你喜欢

转载自blog.csdn.net/u013894391/article/details/89406277