在Oracle linux中为nodejs app创建服务

时间:2017-03-14 20:28:30

标签: node.js linux service

我有一个nodejs应用程序,我想将其作为oracle linux服务器中的服务运行。 我已经创建了服务文件/etc/init.d/wsdlsrv,之后我运行了这个命令:

cd /etc/init.d
chmod +x wsdlsrv
/sbin/chkconfig wsdlsrv on
systemctl start wsdlsrv

wsdlsrv代码如下:

user="root"
. /etc/init.d/functions
export PATH="/usr/local/bin/:/usr/bin/:$PATH"  
name='wsdlsrv'
pid_file="/var/run/$name.pid" 
stdout_log="/var/log/$name.log"
stderr_log="/var/log/$name.err"
get_pid() {
    cat "$pid_file"
}
is_running() {
    [ -f "$pid_file" ] && ps `get_pid` > /dev/null 2>&1
}
case "$1" in
    start)
    if is_running; then
        echo "Already started"
    else
        echo "Starting $name"
        cd "/home/wsdlsrv"
            su $user -c "node app" >> "$stdout_log" 2>> "$stderr_log" &

        echo $! > "$pid_file"
        if ! is_running; then
            echo "Unable to start, see $stdout_log and $stderr_log"
            exit 1
        fi
    fi
    ;;
    stop)
    if is_running; then
        echo -n "Stopping $name.."
        kill `get_pid`
        for i in {1..10}
        do
            if ! is_running; then
                break
            fi

            echo -n "."
            sleep 1
        done
        echo

        if is_running; then
            echo "Not stopped; may still be shutting down or shutdown may have failed"
            exit 1
        else
            echo "Stopped"
                if [ -f "$pid_file" ]; then
                rm "$pid_file"
            fi
        fi
    else
        echo "Not running"
    fi
    ;;
    restart)
    $0 stop
    if is_running; then
        echo "Unable to stop, will not attempt to start"
        exit 1
    fi
    $0 start
    ;;
    status)
    if is_running; then
        echo "Running"
    else
        echo "Stopped"
        exit 1
    fi
    ;;
    *)
    echo "Usage: $0 {start|stop|restart|status}"
    exit 1
    ;;
esac

exit 0

但是当我尝试使用systemclt启动服务时,它会引发错误:wsdlsrv.service的作业失败,因为控制进程退出并显示错误代码。请参阅" systemctl status wsdlsrv.service"和" journalctl -xe"有关详细信息。对于wsdlsrv.service的作业失败,因为控制进程退出并显示错误代码。请参阅" systemctl status wsdlsrv.service"和" journalctl -xe"详情。

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用service wsdlsrv start

确保在/ etc / sysconfig / wsdlsrv中有至少空文件条目

[root@centos ~]# cat /etc/sysconfig/wsdlsrv 
# TODO: add relevant configuration stuff here.

然后,您需要通过执行systemctl daemon-reload重新加载systemctl守护程序。 在此之后,您可以使用systemctl status wsdlsrvsystemctl start wsdlsrv

前:

[root@centos init.d]# systemctl status wsdlsrv
● wsdlsrv.service - (null)
   Loaded: loaded (/etc/rc.d/init.d/wsdlsrv; bad; vendor preset: disabled)
   Active: failed (Result: exit-code) since Tue 2017-03-14 17:12:47 EDT; 7min ago
     Docs: man:systemd-sysv-generator(8)

Mar 14 17:12:47 centos systemd[1]: Starting (null)...
Mar 14 17:12:47 centos systemd[1]: wsdlsrv.service: control process exited, code=exited status=203
Mar 14 17:12:47 centos systemd[1]: Failed to start (null).
Mar 14 17:12:47 centos systemd[1]: Unit wsdlsrv.service entered failed state.
Mar 14 17:12:47 centos systemd[1]: wsdlsrv.service failed.

您可以使用journalctl -xe查看来自systemd的错误消息