Pergunta

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!

Foi útil?

Solução

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.

Outras dicas

May be a little awkward, but what about this:

$PIDCMDLINE = ps -p $PID -o user,command | grep `whoami` | awk '{ print $2 }'
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top