简体   繁体   English

仅当进程来自当前用户时,才如何查找进程的命令行

[英]How to find the command line of a process only if the process is from current user

I have the following situation: A perl script read a file where a application wrote it's pid , and tries to kill it. 我有以下情况:一个perl脚本在应用程序将其写入pid位置读取文件,并尝试将其杀死。 But the problem is that I don't want to kill another process so I check if current process with the recorded PID has the same command line. 但是问题是我不想杀死另一个进程,所以我检查带有记录的PID的当前进程是否具有相同的命令行。 If so, the application could be killed. 如果是这样,该应用程序可能会被杀死。

The following blues script find out the cmdline: 以下蓝调脚本找出cmdline:

$PIDCMDLINE = `ps -p $PID -o cmd`;

The problem is that if another instance for another user is up, maybe on the same sid, it would be killed because it will return a valid command line, and I don't want that behaviour. 问题是,如果另一个用户的另一个实例启动了(也许在同一个sid上),它将被杀死,因为它将返回有效的命令行,并且我不希望这种行为。

How can I restrict ps -p to search only current users processes (no, simple ps doesn't count, because -p nullify the default effect of ps) 如何限制ps -p仅搜索当前用户进程(不,简单的ps不计算在内,因为-p nullify ps的默认效果-p nullify

Thank you! 谢谢!

You can use the following to check both command and user for the certain PID: 您可以使用以下命令检查命令和用户的特定PID:

ps -p <PID> -o user,cmd --columns 1000 | grep `whoami`

Adding a 'grep' according to the comment. 根据评论添加“ grep”。

可能有点尴尬,但是这呢?

$PIDCMDLINE = ps -p $PID -o user,command | grep `whoami` | awk '{ print $2 }'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM