环境:debian8.11.1 64位
方法1:
应用程序:自己编写的C程序
在/etc/init.d目录下增加启动脚本。此处以servmon文件为例,文件内容如下:
#!/bin/sh
### BEGIN INIT INFO
# Provides: servermon
# Required-Start: $local_fs $remote_fs $network $syslog $named
# Required-Stop: $local_fs $remote_fs $network $syslog $named
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the GaoxinGateway applications
# Description: starts GaoxinGateway using start-stop-daemon
### END INIT INFO
APPDIR="/home" #此处为应用锁在目录
NAME="Daemon" #此处为要启动的应用的完整路径
#需要启动多个应用可在此函数中增加
do_start()
{
pid=`ps -ef | grep $NAME | grep -v grep | awk '{print $1}'`
if [ z"$pid" = z ]
then
$APPDIR/$NAME >/dev/null 2>&1 &
fi
}
#如果启动多个应用,则有多个关闭命令
do_stop ()
{
pid=`ps -ef | grep $NAME | grep -v grep | awk '{print $1}'`
if [ z"$pid" = z ]
then
kill $pid
fi
}
#可根据实际情况修改
do_reload ()
{
do_stop
do_start
}
case "$1" in
start)
init_is_upstart > /dev/null 2>&1 && exit 1
log_daemon_msg "Starting $NAME"
do_start
log_end_msg $?
;;
stop)
init_is_upstart > /dev/null 2>&1 && exit 0
log_daemon_msg "Stopping $NAME"
do_stop
log_end_msg $?
;;
restart|force-reload)
init_is_upstart > /dev/null 2>&1 && exit 1
log_daemon_msg "Restarting $NAME"
do_stop
sleep 1
do_start
log_end_msg $?
;;
status)
status_of_proc $NAME
;;
*)
echo "Usage: $0 {start|stop|restart|force-reload|status}" >&2
exit 3
;;
esac
注意:文件中BEGIN INIT INFO 到END INIT INFO必须有。
修改文件权限为755
使用命令添加到启动列表中
cd /etc/init.d
update-rc.d servermon defaults
即添加成功,重启机器检查是否正常启动
update-rc.d -f servermon defaults remove #从系统启动列表中删除该项目
方法2:
程序:python程序
1) 检查/etc/rc.local是否存在,如果不存在则创建rc.local,文件内容如下:
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
exit 0
2)如果刚从创建了rc.local文件,则给次文件增加执行权限,chmod +x rc.local
3) 将需要开机启动的程序命令添加到rc.local文件中exit 0之前即可
4)启动rc-local.service服务,执行命令: systemctl start rc-local.service
5) 检查rc-local.service服务状态,执行 systemctl status rc-local.service
6)重启系统即可