Operating System - HP-UX
1846654 Members
2936 Online
110256 Solutions
New Discussion

capture pid from process started by su command

 
SOLVED
Go to solution
Scott Williams_5
Frequent Advisor

capture pid from process started by su command

Hi all,

I have a script that starts custom applications as a particular user. Is it possible to start a program via su and capture the pid of program just started?

Example:
su someuser -c "proga.exe &"

Cheers,

Scott
6 REPLIES 6

Re: capture pid from process started by su command

well $! gives you the pid of the last process started in the background, so as long as you redirect all other output, you could do domthing like this

pid=$(su someuser -c "proga.exe >/tmp/proga.output.log 2>&1 & ; echo $?" )

HTH

Duncan

I am an HPE Employee
Accept or Kudo

Re: capture pid from process started by su command

oops, of course that should have been:

pid=$(su someuser -c "proga.exe >/tmp/proga.output.log 2>&1 & ; echo $!" )

HTH

Duncan

I am an HPE Employee
Accept or Kudo
Renda Skandier
Frequent Advisor

Re: capture pid from process started by su command

you can grep for program name, excluding the grep command

progname=$1
PID=`ps -ef | grep $progname | grep -v grep | awk '{print $2}'`
Scott Williams_5
Frequent Advisor

Re: capture pid from process started by su command

Thank you for your responses. I'm sorry I've taken so long to reply...I have tried your suggestion, and am running into a bit of a snag. The "echo $!" is not returning a value. I've attached a snippit of code and the output I get back..

Appreciate your help.

Scott
Jeff_Traigle
Honored Contributor
Solution

Re: capture pid from process started by su command

The only way I see to make this work in this manner is to put your commands in a wrapper script that would be called from su. I haven't found a syntax that will allow running multiple commands in a single line from su with the semicolon.
--
Jeff Traigle
Scott Williams_5
Frequent Advisor

Re: capture pid from process started by su command

Jeff, Duncan, Renda: Thanks for your repsones. Given my trials, I suspected I'd have to something similar to a separate script. I'm movin' on...

thanks,

Scott