Search code examples
perlbashprocesspidps

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. 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. If so, the application could be killed.

The following blues script find out the 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.

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)

Thank you!


Solution

  • You can use the following to check both command and user for the certain PID:

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

    Adding a 'grep' according to the comment.