How to get the pid of the last executed command in shell script?

407,656

Solution 1

The PID of the last executed command is in the $! shell variable:

my-app &
echo $!

Solution 2

Get PID:

#!/bin/bash
my-app &
echo $!

Save PID in variable:

#!/bin/bash
my-app &
export APP_PID=$!

Save all instances PID in text file:

#!/bin/bash
my-app &
echo $! >>/tmp/my-app.pid

Save output, errors and PID in separated files:

#!/bin/bash
my-app >/tmp/my-app.log 2>/tmp/my-app.error.log &
echo $! >>/tmp/my-app.pid

echo "my-app PID's: $(cat /tmp/my-app.pid)"

Solution 3

Try something like

pidof my_app >> /tmp/my_app.pid
Share:
407,656

Related videos on Youtube

javment
Author by

javment

Senior software engineer with solid knowledge in data structure and algorithm. Familiar with Cloud-Native architecture. Proficient in developing using a wide variety of tools. Eager to learn new technologies.

Updated on September 18, 2022

Comments

  • javment
    javment over 1 year

    I want to have a shell script like this:

    my-app &
    echo $my-app-pid
    

    But I do not know how the get the pid of the just executed command.

    I know I can just use the jobs -p my-app command to grep the pid. But if I want to execute the shell multiple times, this method will not work. Because the jobspec is ambiguous.

    • Arsen Karapetjan
      Arsen Karapetjan about 3 years
      did you try echo $!? right after the command, you ran?
  • Mat
    Mat over 12 years
    That's very fragile at best and doesn't work if my-app is executed more than once - davidshen84 specifically worries about that cas.
  • user3153014
    user3153014 over 9 years
    It is printing pid as for eg. [1] 893 . I want only number.
  • ramrunner
    ramrunner over 9 years
    It should be noted that this stands for programs started in the background. If no background processes have been started the parameter is not set.
  • Willem
    Willem over 9 years
    If you even go this route, you should use pgrep instead.
  • imz -- Ivan Zakharyaschev
    imz -- Ivan Zakharyaschev almost 9 years
    Another worthy solution is suggested in (a comment to an answer to) How to get pid of just started process: oh, and the "oneliner": /bin/sh -c 'echo $$>/tmp/my.pid && exec program args' & – sysfault Nov 24 '10 at 14:28
  • S.D
    S.D almost 9 years
    @user3153014 String s like "[2] 2625" are printed by shell after starting background task. This is not related to output of echo $!
  • Eric Renouf
    Eric Renouf over 8 years
    The question didn't ask about redirection, then your answer is the mostly the same as the accepted one except that if an instance of my-app finishes, and perhaps even worse its PID gets reused later, you'll have bad information in your file. I don't think this answer adds much of value to the existing accepted answer really
  • Eduardo Cuomo
    Eduardo Cuomo over 8 years
    @EricRenouf post updated!
  • mlathe
    mlathe about 6 years
    another derivative that might be useful. This gets the PID and at the same time treats it as a (mostly) foreground process: sleep 4 | grep INVALID & export MY_PID=$!; fg; echo $MY_PID returned with $? `
  • MrMesees
    MrMesees over 5 years
    isnt APP=main & a way to grab PID?
  • papo
    papo over 5 years
    external command, and slow, if system can give PID directly why searching through all processes to find just that? and also, can't be certain you'll get correct value
  • Vrata Blazek
    Vrata Blazek about 5 years
    I usually use such code in a script when I need to wait until the process ends. my-app & myVar=$! ; fg. fg brings the process to foreground again. I can then print echo $myVar later and I'm pretty sure that the my-app has already finished.
  • Michele Piccolini
    Michele Piccolini about 4 years
    If you are using that inside su {user} -c {command} as command, be sure to escape the $! (use \$!). See unix.stackexchange.com/questions/147916/…
  • Michael
    Michael about 4 years
    @VrataBlazek fg: no job control
  • Michael
    Michael about 4 years
    @imz--IvanZakharyaschev Why is it necessary to pipe the PID to a file? If I try to wrap the shell call with $() it hangs at that point... why can't I get it into a shell variable directly? When I try to pipe it to a file I have another problem - now the PID of my command isn't the same as reported, whereas if I don't pipe it's the same!
  • imz -- Ivan Zakharyaschev
    imz -- Ivan Zakharyaschev about 4 years
    @Michael Hiw do you want to get it into a shell variable directly? If you suggest a method, I could try to say whether it would work or not. As for $() wrapping, I can't try to explain the behavior without seeing the exact code.