1832857 Members
3448 Online
110047 Solutions
New Discussion

Process ID

 
SOLVED
Go to solution
Venky_1
Frequent Advisor

Process ID

Hi ,

How do I find out the process ID of the process running in the background invoked by some script executed by one particular user.

Note :- Some other users may be running the same script which invokes similar background processes. But I would like to know the process ID of the background process invoked by the script executed by me only.

Thanks
Venky
Winners never quit and quitters never win
4 REPLIES 4

Re: Process ID

When a process is kicked off in the background, the variable $! is set the the process ID of that process, so just grab the value of $! immediately after starting your background process:

myscript.sh &
pid_of_myscript=$!

And then I could do these kind of things:

kill $pid_of_myscript

to stop the background process, or

wait $pid_of_myscript

to wait for it to complete.

HTH

Duncan

I am an HPE Employee
Accept or Kudo
Wodisch
Honored Contributor

Re: Process ID

Hi Venky,

in case you want to see somebody else's child processes, you could go for the adittional features of the "ps" command:

UNIX95=x ps -eHo ppid,pid,args |
more

you will see all processes sorted by their parent process's PID now.

HTH,
Wodisch
Rajeev  Shukla
Honored Contributor

Re: Process ID

Other option would be to grep for all the process started by that user with ps -efl|grep username

and grep for "nice process" = 24.
Coz we know all background process are started by a nice process at 24. So a complete command would look like
ps -efl|grep username|grep -e 24

Rajeev
Sridhar Bhaskarla
Honored Contributor
Solution

Re: Process ID

Hi Venky,

Edit the script and place the following in the beginning.

echo "$(date): $0: $(whoami): $$" >> /tmp/$0.pids

Look at /tmp/your_script_name.pids file to look at the pid corresponding to each user.

Basically "$$" does the trick within the script.

If you don't want to modify it, use the variable $! to find out the background process

$./your_script&
$echo $!

$! does the trick outside the script. It will be saved until your logoff or execute another background script.

-Sri

You may be disappointed if you fail, but you are doomed if you don't try