help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] get pid of exited process


From: Eduardo A . Bustamante López
Subject: Re: [Help-bash] get pid of exited process
Date: Sun, 3 Jan 2016 01:32:19 -0600
User-agent: Mutt/1.5.23 (2014-03-12)

If the process died already, then the pid you're getting is no longer a valid
pid. Also, it could be the pid of a new and very different process.

If even with that in mind you want to proceed, you can do this:

  address@hidden:~$ date -u; (echo "The pid of sleep is $BASHPID"; exec sleep 
10); date -u
  Sun Jan  3 07:30:48 UTC 2016
  The pid of sleep is 19579
  Sun Jan  3 07:30:58 UTC 2016

And I ran 'ps' in a different terminal:

  address@hidden ~ % ps -C sleep
     PID TTY          TIME CMD
   19579 pts/1    00:00:00 sleep

What happens here is that you create a subshell with (...) and get its pid with
the special BASHPID variable. Then, you replace the subshell's process using 
'exec',
which just loads the new program, but keeps the process ID.

This could also work in POSIX sh with $$, but not using a subshell (i.e. (...) 
), but
replacing the main shell process (or doing something like: sh -c 'echo $$; exec 
...').

-- 
Eduardo Bustamante
https://dualbus.me/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]