Shell-Script
在 SLES / SUSE 11 SP3 中創建服務
我有一個 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 在 System -> System Services 中註冊的,但是當我嘗試啟用它時,我得到了錯誤
/etc/init.d/attachment-server start 返回 2(無效或多餘的參數)
有什麼可能是錯的?我無法弄清楚這個腳本應該在哪裡出錯。它基本上只需要執行一個腳本,就是這樣。
必須說我在 SLES 上完全是新手。
你的線
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