THESorcerer
THESorcerer

Reputation: 1065

is there any way to know the pid of a launched program?

if I launch a bash script as a child, I can pass its own pid to the parent by using $$.

Is there any way to find the pid of a program that I launch from a script in background like:

ping x.x.x.x &

what's the pid of that ping ?

(I just hope I expressed my self correctly ... my English is not the best)

PS. I'm looking for a simple and clean solution, I can imagine something like:

ping -t10000 -W10 x.x.x.x &
then
ps ax | grep 'ping -t10000 -W10 x.x.x.x'$

but is too complicated, also even that I used switches to personalize it is not clean, it may catch another processes in the system

Upvotes: 13

Views: 3652

Answers (2)

0xC0000022L
0xC0000022L

Reputation: 21319

Use this: $! right after executing the command whose PID you want. It means, though that you need to use an ampersand (&) after the command, to start it in background. E.g.:

my_command & CMDPID=$!
echo "$CMDPID"

Upvotes: 9

Alex L
Alex L

Reputation: 8935

The variable $! has the PID of the last background process you started.

Upvotes: 26

Related Questions