debian 添加service服务

家住魔仙堡 提交于 2019-12-07 17:43:06

fedora中添加service用的是chkconfig --add  网上的资料按照操作,成功率很高,在debian中 ,添加service用到的是update-rc.d按照网上的教程添加,虽然教程写的没有问题,但是在操作的时候还是碰到不少问题,本文介绍如何在debian中添加service,并记录自己在添加的时候碰到的一些问题,希望朋友们碰到其他问题的可以留下言,供大伙参考。

1.首先编写一个service脚本(建议需要复制的同学将汉字备注部分去掉):

app_run.sh  :

#!/bin/sh
### BEGIN INIT INFO
# Provides:          app_run
# 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: app_run service
# Description:       Start the app_run service and associated helpers
### END INIT INFO
do_start()
{
	nohup python /var/www/python/run.py > /tmp/app_run.log 2>&1 &  #这里是你要在服务启动时执行的动作
}

do_stop()
{
	pkill -f /var/www/python/run.py   #这里是你要在关闭服务时执行的动作

}


#
# Function that sends a SIGHUP to the daemon/service
#
do_restart() {
	do_stop
	sleep 1
	do_start
}

case "$1" in
  start)
	do_start
	;;
  stop)
	do_stop
	;;
  status)
	exit $?
	;;
  reload)
	echo "reload"
	;;
  restart)
	do_restart
	;;
  *)
	echo "Usage: {start|stop|restart|reload}" >&2
	exit 3
	;;
esac

exit 0

2.将代码放在一个临时的位置 检查代码是否正常工作  如存放在/home/app_run.sh 给脚本授权  chmod +x /home/app_run.sh

3.验证脚本:/home/app_run.sh start  如果可以正常运行  则将脚本移入文件夹 /etc/init.d

4.运行update-rc.d app_run defaults 注册服务

5.运行service app_run start启动脚本 然后运行service app_run status 查看服务是否正常启动

6.若想删除服务,运行 update-rc.d -f app_run remove , 若更改了/etc/init.d/app_run的内容,可以运行 systemctl daemon-reload  这个命令会重新装载所有守护进程的unit文件,然后重新生成依赖关系树 。

配置过程中遇到的问题:

因脚本是用notepad++在win下面写然后通过winScp上传到linux的所以出现shell脚本无法执行的问题 错误信息大概是 **** /bin/sh^M: bad interpreter: *** 通过baidu 了解到这是因为该文件的fileformat是dos类型 文件中会有linux不认的特殊符号,所以要用vim转换下:

vim /home/app_run 然后输入:set ff 回车  显示fileformat=dos  

这时需要把文件的fileformat修改成unix: 

在vim打开状态下输入  :set ff=unix 回车  然后输入:wq 回车 保存

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!