centos6 python脚本service服务

参考引用:https://www.jianshu.com/p/a34836a7811c  作者:不开心找小司

python脚本:/home/ftpuser/io_mq_service/publisher.py

编写服务脚本:/etc/init.d/io_mq_service

nano /etc/init.d/io_mq_service

#!/bin/sh

#chkconfig: 123456 90 10

daemon_start() {

    python /home/ftpuser/io_mq_service/publisher.py &

    echo "Server started."

}

daemon_stop() {

    pid=`ps -ef | grep '[p]ython /home/ftpuser/io_mq_service/publisher.py' | awk '{ print $2 }'`

    echo $pid

    kill $pid

    sleep 2

    echo "Server killed."

}

case "$1" in

  start)

    daemon_start

    ;;

  stop)

    daemon_stop

    ;;

  restart)

    daemon_stop

    daemon_start

    ;;

  *)

    echo "Usage: /home/ftpuser/io_mq_service/publisher {start|stop|restart}"

    exit 1

esac

exit 0

第一行:#!/bin/sh 是指此脚本使用 /bin/sh 来解释执行,#! 是特殊的表示符,其后面根的是解释此脚本的 shell 的路径。

第二行:比较特殊,看起来像是注释,但 chkconfig 命令需要用到,必须存在。定义了在运行级别 1、2、3、4、5、6 中,服务将被激活(状态为:on),90 代表 Start 的顺序,10 代表 Kill(Stop)的顺序。

标记服务脚本为可执行:

chmod +x /etc/init.d/io_mq_service

自启动:

chkconfig io_mq_service

启动/停止服务:

service io_mq_service  start  # 启动服务  

service io_mq_service  restart  # 重启服务

你可能感兴趣的:(centos6 python脚本service服务)