![在SLES/SUSE 11 SP3中创建服务](https://linux22.com/image/67569/%E5%9C%A8SLES%2FSUSE%2011%20SP3%E4%B8%AD%E5%88%9B%E5%BB%BA%E6%9C%8D%E5%8A%A1.png)
我有一个 python 脚本,可以很好地从(远程)控制台执行,如下所示:
sudo /srv/web-asset-server-master/python server.py
问题是,它只有在控制台窗口打开时才存在。所以我想将它作为服务来运行,但这似乎比我想象的更棘手。已遵循此食谱->https://www.novell.com/coolsolutions/feature/15380.html(并阅读有关 Unix 和 Linux 的类似问题,例如SuSE 启动完成后如何运行我的脚本?
遵循上述所有基本原则创建自定义初始化脚本我做了这个:
#! /bin/sh
# Copyright (c) 2015 NHMD / specify
# All rights reserved.
#
# Author: Ben Anhalt, 2015
#
# /etc/init.d/attachment-server
# and its symbolic link
# /usr/sbin/attachment-server
### BEGIN INIT INFO
# Provides: attachment-server
# Required-Start: $network
# Required-Stop:
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Short-Description: Specify attachment server
# Description: The attachment server is storage medie for Specify attachments
# service. We want it to be active in runlevels 3
# and 5, as these are the runlevels with the network
# available.
### END INIT INFO
# Check for missing binaries
ATT_BIN=/srv/web-asset-server-master/python server.py
test -x $ATT_BIN || { echo "$ATT_BIN not installed";
if [ "$1" = "stop" ]; then exit 0;
else exit 5; fi; }
# Load the rc.status script for this service.
. /etc/rc.status
# Reset status of this service
rc_reset
case "$1" in
start)
echo -n "Starting attachment server "
## Start daemon with startproc(8). If this fails
## the return value is set appropriately by startproc.
startproc $ATT_BIN
# Remember status and be verbose
rc_status -v
;;
stop)
echo -n "Shutting down attachment server "
## Stop daemon with killproc(8) and if this fails
## killproc sets the return value according to LSB.
killproc -TERM $ATT_BIN
# Remember status and be verbose
rc_status -v
;;
restart)
## Stop the service and regardless of whether it was
## running or not, start it again.
$0 stop
$0 start
# Remember status and be quiet
rc_status
;;
reload)
# If it supports signaling:
echo -n "Reload service attachment server "
killproc -HUP $ATT_BIN
#touch /var/run/BAR.pid
rc_status -v
## Otherwise if it does not support reload:
#rc_failed 3
#rc_status -v
;;
status)
echo -n "Checking for service attachment-server "
## Check status with checkproc(8), if process is running
## checkproc will return with exit status 0.
# Return value is slightly different for the status command:
# 0 - service up and running
# 1 - service dead, but /var/run/ pid file exists
# 2 - service dead, but /var/lock/ lock file exists
# 3 - service not running (unused)
# 4 - service status unknown :-(
# 5--199 reserved (5--99 LSB, 100--149 distro, 150--199 appl.)
# NOTE: checkproc returns LSB compliant status values.
checkproc $ATT_BIN
# NOTE: rc_status knows that we called this init script with
# "status" option and adapts its messages accordingly.
rc_status -v
;;
*)
## If no parameters are given, print which are avaiable.
echo "Usage: $0 {start|stop|status|restart|reload}"
exit 1
;;
esac
rc_exit
这是最简单的示例,更改了服务名称和脚本路径。和“附件服务器”是现在实际上由 YAST 在系统 -> 系统服务中注册,但是当我尝试启用它时,出现错误
/etc/init.d/attachment-server start 返回 2(参数无效或过多)
有什么可能是错误的?我无法弄清楚这个脚本哪里错了。它基本上只需要执行一个脚本,就是这样。
必须说我对 SLES 完全是新手。
答案1
你的线路
ATT_BIN=/srv/web-asset-server-master/python server.py
正在设置变量 ATT_BIN 然后执行 server.py。通常,ATT_BIN 应该是要执行的单个文件的完整路径名,例如/home/me/server.py
,如果这是您的 python 脚本所在的位置。
通常,server.py 会被更改,以便第一行在您的情况下是:
#!/srv/web-asset-server-master/python
那么运行时就不需要指定解释器了,直接设置即可
ATT_BIN=/home/me/server.py
确保您设置了 python 文件的可执行权限,例如:
chmod a+rx /home/me/server.py