I have a script (run.sh) which calls my application script. At a given time, I may have multiple run.sh running.

The general format of the run.sh script is,

# Running DALTON JOB: Helix
    echo "-----------------------------------------------"
   export DALTON_TMPDIR=/mnt/raid0/scratch
    export OMP_NUM_THREADS=6
    source /opt/intel/compilers_and_libraries_2017.0.098/linux/bin/compilervars.sh intel64
    source /opt/intel/mkl/bin/mklvars.sh intel64
    echo "//-------process started----------------------------//"

./application.sh  -mb 14550 input.mol output.out

echo "//-------process finished----------------------------//"

Is it possible to get the PID of the application.sh inside the run.sh script. (I found that $$ gives the PID of the script itself.)

Also, I noticed that the PID of the application is always larger numerically than parent script but maybe its coincidence.

  • 1
    does ./application.sh get run in the background? otherwise, it will have exited by the time run.sh could do anything with the PID.
    – Jeff Schaller
    Jun 8, 2017 at 14:14
  • application.sh runs in the background ( for ~3-4 hours) as a host process of run.sh.
    – ankit7540
    Jun 8, 2017 at 14:15
  • 1
    Aside: the PID is assigned in increasing numerical order, so in general parents have a lower PID number than children. However, there is a point where the PID wrap. E.g., the maximum PID is 32768, so it is possible for a child process to have a lower PID number than a parent. (note: 32768 is an example and not necessarily the max value on a given system).
    – KevinO
    Jun 8, 2017 at 14:22
  • 1
    eliding the actual call to application.sh with "parameters" muddies the waters. show that it goes into the background, then I think this Q has a usable answer.
    – Jeff Schaller
    Jun 8, 2017 at 14:25
  • 1
    I'm confused. You do not put it in the background; it runs for 3-4 hours, and then you get the "echo process finished"? The application.sh has exited by that point. What PID do you want, and where?
    – Jeff Schaller
    Jun 8, 2017 at 14:33

3 Answers 3


If you want to see the PID of application.sh while it is running, then I would suggest explicitly putting it into the background, capturing the PID, then waiting for it to exit:

# ...
./application.sh  -mb 14550 input.mol output.out &
echo "The application pid is: $app_pid"
wait "$app_pid"
# ...
  • I would second this. '$!' captures the pid of the last executed (backgrounded) command which is the command in question.
    – LJKims
    Jun 8, 2017 at 14:52
  • I need a clarification. wait "$app_pid" keeps the run.sh waiting till application.sh finishes right ??
    – ankit7540
    Jun 8, 2017 at 14:52
  • 2
    That's correct.
    – LJKims
    Jun 8, 2017 at 14:53

I believe you may want a combination of something like pstree -p and ps axjf with some additional parsing.

Notice how the PID of httpd is 30469, and every process that is a child of httpd has a PPID (parent process ID) of 30469. The grandchildren of httpd will have the PPID of their parent process, which will have the PPID of the httpd process.

I didn't post the full output of either as they are rather large. Here's an example output from each:

user@host$ ps -axjf
1     30469 30469 30469 ?           -1 Ss       0   0:46 /usr/sbin/httpd
30469 22410 22410 22410 ?           -1 Ssl      0   0:00  \_ PassengerWatchdog
22410 22413 22413 22410 ?           -1 Sl       0  23:06  |   \_ PassengerHelperAgent
22410 22418 22418 22410 ?           -1 Sl      99   0:01  |   \_ PassengerLoggingAgent
30469 22442 30469 30469 ?           -1 Sl      48   7:55  \_ (wsgi:pulp)
30469 22443 30469 30469 ?           -1 S       48   1:48  \_ /usr/sbin/httpd
30469 22445 30469 30469 ?           -1 S       48   1:55  \_ /usr/sbin/httpd
30469 22447 30469 30469 ?           -1 S       48   1:54  \_ /usr/sbin/httpd

user@host$ pstree -p
        │              │                        │                        ├─{PassengerHelpe}(22416)
        │              │                        │                        ├─{PassengerHelpe}(22417)
        │              │                        │                        ├─{PassengerHelpe}(22420)
        │              │                        │                        ├─{PassengerHelpe}(22422)
        │              │                        │                        ├─{PassengerHelpe}(22423)
        │              │                        │                        ├─{PassengerHelpe}(29342)

Note if you know the parent process tree you can run pstree -p <pid>.


To save the process ID of a utility, start it as an asynchronous process and save its PID in a variable, or use $! directly (it's the PID of the most recently started background process):


export DALTON_TMPDIR=/mnt/raid0/scratch
source /opt/intel/compilers_and_libraries_2017.0.098/linux/bin/compilervars.sh intel64
source /opt/intel/mkl/bin/mklvars.sh intel64

printf 'Started at "%s"\n' "$(date)"

./application.sh -mb 14550 input.mol output.out &


printf 'Application PID is %d\n' "$app_pid"

wait "$app_pid"

printf 'Ended at "%s"\n' "$(date)"
printf 'Run time was approximately %d minutes (%d seconds)\n' "$(( SECONDS/60 ))" "$SECONDS"

The PID of the application is $app_pid.

You say you have noticed that the PIDs of new processes are always larger than those of previous processes. This is nothing that you may rely on for two reasons:

  1. There will be a PID allocation wrap-around once the maximum allowed PID has been allocated to a process, and old PIDs will start to be reused. The next PID will be smaller, not larger, than the previous PID when the wrap-around happens (and new PIDs after that will skip numbers that are still in use as PIDs by running processes).
  2. Some systems, like OpenBSD, use randomized PID allocation:

    $ for i in {1..5}; do bash -c 'echo $$'; done

As a side note, you may use $PWD rather than $(pwd) to get the current working directory.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .