0

我有一个脚本可以在 CentOs 6 上将 java 应用程序作为服务启动。这里是:

#!/bin/sh
# chkconfig: - 80 20

SERVICE_NAME=cn4server
PATH_TO_JAR=/usr/local/share/myserver/cn4server.jar
PID_PATH_NAME=/usr/local/share/myserver/cn4server-pid
case $1 in
    start)
        echo "Starting $SERVICE_NAME ..."
        if [ ! -f $PID_PATH_NAME ]; then
            nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
                        echo $! > $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is already running ..."
        fi
    ;;
    stop)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stoping ..."
            kill $PID;
            echo "$SERVICE_NAME stopped ..."
            rm $PID_PATH_NAME
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
    restart)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stopping ...";
            kill $PID;
            echo "$SERVICE_NAME stopped ...";
            rm $PID_PATH_NAME
            echo "$SERVICE_NAME starting ..."
            nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
                        echo $! > $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
esac 

它运行良好,但有一个问题:它以 root 身份运行。如何更改脚本以代表另一个用户运行 java 应用程序?

4

1 回答 1

0

对我来说,解决方案是:非root用户的crontab,使用@reboot /usr/local/share/myserver/cn4server.sh restart

它工作正常,尽管它是一种解决方法。

于 2014-07-11T06:38:04.667 回答