2015-05-25 35 views
2

我試圖更新我的應用程序以將其與systemd結合使用。 當我用暴發戶,我剛剛創建/etc/init.d/myService腳本:systemd在ExecStop完成之前殺死我的進程

#!/bin/bash 
#chkconfig: 2345 90 10 
#description: myDescription 

### BEGIN INIT INFO 
# Provides: myService 
# Required-Start: sshd 
# Required-Stop: sshd 
# Default-Start: 2 3 4 5 
# Default-Stop: 0 1 6 
# Short-Description: start myService 
# Description: 
### END INIT INFO 

SCRIPT=$(readlink -f $0) 
lockfile="/var/lock/subsys/myService" 

do_start() { 
    if [ -d "/var/lock/subsys" ]; then 
     touch $lockfile 
    fi 
    ... 
} 

do_stop() { 
    ... 
    if [ -d "/var/lock/subsys" ]; then 
     if [ -f "$lockfile" ]; then 
      rm -f $lockfile 
     fi 
    fi 
} 

do_status() { 
    ... 
} 


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

和所有被罰款。

通知,該腳本生成一些將在後臺執行的子進程。 要使用systemd使用它,我做了後續的服務文件(myService.service):

[Unit] 
Description=My Description 
Requires=sshd.service 
After=sshd.service 
Before=shutdown.target reboot.target halt.target 

[Service] 
Type=oneshot 
ExecStart=/etc/init.d/myService start 
ExecStop=/etc/init.d/myService stop 
RemainAfterExit=yes 
KillMode=none 

[Install] 
WantedBy=multi-user.target 

如果我運行

systemctl stop myService.service 

所有做工精細。我的應用程序通過/etc/init.d/myService stop命令成功停止。

但我已經得到了如下問題: 當我重新啓動系統,並/etc/init.d/myService停止正在執行的過程,我應該停止爲myService已經殺腳本。我應該控制許多流程(大約7個流程),而系統本身不應該終止它。

我試過使用Type = forking,並將PIDFile指定爲進程的pidfile,它具有最長的生命週期(應該先開始第一個結束),但是我的所有進程都被終止了。

是否有任何簡單的方法來避免殺死我的子進程?

+0

您可以從非編程問題(例如Unix和Linux)中選擇一個更好的StackExchange WWW站點開始:http://unix.stackexchange.com/questions/204922/ – JdeBP

+0

謝謝,我會試一試。 – Sild

+0

這不是創建systemd單元的正確方法。你並不是想要調用你舊式的init/upstart腳本。做得對,你不會有任何問題。 –

回答

0

找到解決方法。

我運行hadoop & hbase,它們的一些組件是由ssh-connection啓動到本地主機,並且以這種方式啓動的進程無法由systemd控制。 這是分佈式系統的設計,但在我的情況下,工作是在一臺機器上進行。所以,我在的Hadoop /斌/ slaves.sh

for slave in `cat "$HOSTLIST"|sed "s/#.*$//;/^$/d"`; do 
ssh $HADOOP_SSH_OPTS $slave $"${@// /\\ }" \ 
    2>&1 | sed "s/^/$slave: /" & 
if [ "$HADOOP_SLAVE_SLEEP" != "" ]; then 
    sleep $HADOOP_SLAVE_SLEEP 
fi 
done 

已經取代以

for slave in `cat "$HOSTLIST"|sed "s/#.*$//;/^$/d"`; do 
eval "[email protected]" 
if [ "$HADOOP_SLAVE_SLEEP" != "" ]; then 
    sleep $HADOOP_SLAVE_SLEEP 
fi 
done 

的問題得到了解決,現在進程顯示服務進程樹。 Hbase可能具有相同的解決方案,但現在它以distributed = false開頭,並且不會通過ssh啓動任何進程。