赞
踩
这是从代码创建服务的过程:
首先,在your_script.py的上面添加以下shebang:
#!/usr/bin/env python
我正在使用以下指令来创建自己的服务:
假设您的服务名称为“test”,然后创建以下文件:
test.service
[Unit]
SourcePath=/etc/init.d/test
[Service]
ExecStart=/etc/init.d/test start
ExecStop=/etc/init.d/test stop
test.sh
#!/usr/bin/env bash
# Quick start-stop-daemon example, derived from Debian /etc/init.d/ssh
set -e
# Must be a valid filename
NAME=this_is_a_test
PIDFILE=/var/run/$NAME.pid
#This is the command to be run, give the full pathname
DAEMON=/home/Your_User_Name/Your_path/your_script.py
case "$1" in
start)
echo -n "Starting daemon: "$NAME
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS
echo "."
;;
stop)
echo -n "Stopping daemon: "$NAME
start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
echo "."
;;
restart)
echo -n "Restarting daemon: "$NAME
start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile $PIDFILE
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS
echo "."
;;
*)
echo "Usage: "$1" {start|stop|restart}"
exit 1
esac
exit 0
然后我为上面的配置创建一个安装:
install.sh
#!/usr/bin/env bash
echo "create a test service ..."
cp test.sh /etc/init.d/test
cp test.service /etc/systemd/system
chmod +x /etc/init.d/test
# sed -i "s/Your_User_Name/you_path/g" /etc/init.d/test
echo "created the test service"
最后,做:
设置your_script.py文件的访问权限:
$chmod 755
然后安装服务:
$sudo bash ./install.sh
然后使用systemctl触发服务或根据需要重新启动计算机.
然后开始你的服务:
$sudo service test start
你可以查看它的状态:
$sudo service test status
[注意]:
>使用上述脚本中的变量替换测试,Your_User_Name,Your_path和your_script.py静态变量.
> Difference between a service and systemctl
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。