如何在 CentOs 6 上以非 root 用户身份运行 java 服务

如何在 CentOs 6 上以非 root 用户身份运行 java 服务

我有一个在 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 应用程序?

答案1

我的解决方案是:

crontab对于“非 root 用户”,使用以下行:

@reboot /usr/local/share/myserver/cn4server.sh restart

它工作得很好,尽管这是一种解决方法。

相关内容