击:使用diferent用户获取PID的过程中 [英] Bash: Get pid of a process using a diferent user

查看:141
本文介绍了击:使用diferent用户获取PID的过程中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在bash脚本启动java程序,我想获得该程序的PID它执行后。该PID将被保存在一个文本文件中。

I have a script in bash to start a java program and I want to get the pid of the program after it executes. The pid will be saved in a text file.

分辩现在我有这样的事情:

Rigth now I have something like this:

if [ "$USER" == "root" ]
then
    su $APP_USER -c "nohup java $JAVA_OPTS  >> $LOG_OUT 2>> $LOG_ERR &"
    su $APP_USER -c "echo $! > $PID_PATH/$CAT.pid"
else
    nohup java $JAVA_OPTS >> $LOG_OUT 2>> $LOG_ERR &
    echo $! > "$PID_PATH/$CAT.pid"
fi

我也tryed喜欢这个,但它不工作也没有。

I also tryed like this but it doesn't work neither.

if [ "$USER" == "root" ]
then
    su $APP_USER -c "(nohup java $JAVA_OPTS  >> $LOG_OUT 2>> $LOG_ERR &); (echo $! > $PID_PATH/$CAT.pid)"
else
    nohup java $JAVA_OPTS >> $LOG_OUT 2>> $LOG_ERR &
    echo $! > "$PID_PATH/$CAT.pid"
fi

当我为我的APP_USER运行所有的伟大工程,当我以root身份运行我的java程序启动,但pid文件创建空的。

when I run as my APP_USER all works great, when I run as root my java program starts but the pid file is created empty.

推荐答案

尝试

su $APP_USER -c "nohup java $JAVA_OPTS  >> $LOG_OUT 2>> $LOG_ERR & echo \$! > $PID_PATH/$CAT.pid"

\\ 背后 $! prevents变量的扩展,传递给su命令之前。

\ behind $! prevents expansion of the variable, before passing to the su command.

这篇关于击:使用diferent用户获取PID的过程中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆