Sirish Kumar Bethala
Sirish Kumar Bethala

Reputation: 9269

Bash script runs with two pids

When I run bash script I am getting two entries in ps list one being child of other . My script contains just one command

test.sh
   sleep 20

pidof test.sh
2494 2493

And how can I get the parent PID

Upvotes: 1

Views: 1071

Answers (2)

JRFerguson
JRFerguson

Reputation: 7526

On a Centos system 'pidof' returned only the parent process. To obtain the child(ren) you could use 'pstree':

$ pidof test.sh
22220
$ pstree -p 22220
mytest(22220)---sleep(22223)

Upvotes: 1

Dennis Bliefernicht
Dennis Bliefernicht

Reputation: 5157

When you run that script, two processes are being created. The first one is the bash interpreter running your script. sleep on the other hand is another binary (often in /bin) and thus requires launching of a new process. (although the process naming seems to differ on different systems; when running on my test system neither process was named by test.sh, just bash and sleep).

To get the parent process ID for one or more processes (by ID or name) you might use ps:

$ ps -p 6194 -o ppid=
 6187

$ ps -p 6194,6748 -o ppid= 
 6187
 6747

$ ps -C bash -o ppid=
 6187
 6747
 6782

Upvotes: 2

Related Questions