无法使程序作为守护程序运行

时间:2014-03-09 17:43:08

标签: bash solr daemon init.d

我正在尝试编写init.d脚本来运行solr作为守护程序,但不幸的是ps aux表明没有这样的进程。

以下是代码:

#!/bin/sh

start_path=/opt/solr/example/start.jar
JAVA_PATH=/usr/bin/java
PID=/tmp/.solr/pid
ARGS="-jar $start_path"

if [ ! -d /tmp/.solr ]
    then
        mkdir /tmp/.solr
fi

start(){
    echo -n "Starting solr..."
    start-stop-daemon --start --background --name "solr" --make-pidfile --pidfile $PID --exec ${JAVA_PATH} -- ${ARGS} 
    RETVAL="$?"
    if [ "$RETVAL" = 0 ] 
    then
        echo "done."
    else
        echo "failed. See error code for more information."
    fi  
    return $RETVAL
}

case "$1" in
    start)
        start
    ;;  
    *)  
        echo $"Usage: solr {start}"
        exit 3
    ;;  
esac

exit $RETVAL

2 个答案:

答案 0 :(得分:1)

如何使用命令

java jar start.jar & disown

这将启动该过程并将其移至后台......

希望我帮忙!

答案 1 :(得分:0)

好吧,感谢this博文,我终于成功了。 这是它的外观:

#!/bin/sh

PIDFILE=/tmp/.solr/pid

if [ ! -d /tmp/.solr ]
    then
        mkdir /tmp/.solr
fi

cd /opt/solr/example

start(){
    if [ ! -f $PIDFILE ]
        then
            echo "Starting solr..."
            nohup java -jar start.jar &
            echo $! > $PIDFILE
            RETVAL=$?
            if [ "$RETVAL" = 0 ]
                then
                    echo "Done."
                else
                    echo "Failed. See error code for more information."
            fi
            return $RETVAL
            echo $! > $PIDFILE
        else
            echo "Solr is already running"
            return 1
    fi
}

stop(){
    if [ ! -f $PIDFILE ]
        then
            echo "Solr is not running"
            return 1
        else
            PID=$(cat $PIDFILE)
            echo "Stopping solr..."
            kill $PID
            RETVAL=$?
            if [ "$RETVAL" = 0 ]
                then
                    echo "Solr stopped."
                    rm $PIDFILE
                else
                    echo "Can't stop Solr."
            fi
            return $RETVAL
    fi
}

restart(){
    stop
    start
}

case "$1" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    restart)
        restart
    ;;
    *)
        echo $"Usage: solr {start|stop|restart}"
        exit 3
    ;;
esac

exit $RETVAL