Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to start process (eg. myCommand) and get its pid (to allow to kill it later).

I tried ps and filter by name, but I can not distinguish process by names

ps ux | awk '/<myCommand>/ {print $2}' 

Because processes names are not unique.

I can run process by:

myCommand &

I found that I can get this PID by:

echo $!

Is there any simpler solution?

I would be happy to execute myCommand and get its PID as a result of one line command.

share|improve this question
up vote 36 down vote accepted

What can be simpler than echo $!? As one line:

myCommand & echo $!
share|improve this answer
Thank you merging these commands with "&" helped me a lot. – rafalmag Nov 27 '10 at 12:09
in a bash script, in a loop which starts programs, $! is not accurate. Sometimes it returns pid of the script itself, sometimes of grep or awk run from the script any solution to specifically get the pid of the process just launched in this scenario? something like pid=myprogram would have been awesome – user217730 Apr 26 '14 at 5:59
NB that this requires you to start the command using a & as the previous line, otherwise the echo basically returns blank. – rogerdpack Feb 4 '15 at 18:56
assigning to variable like command & echo $! freezes the execution at this step :( – Shashank Vivek Jul 15 at 9:41

Wrap the command in a small script

yourcommand &
echo $! >/path/to/pid.file
share|improve this answer

I do not know of any simpler solution, but isn't using $! good enough? You can always assign the value to some other variable if you need it later, as said by others.

As a side note, instead of piping from ps you could use pgrep or pidof.

share|improve this answer

use exec from a bash script after registering the pid to a file:


suppose you have a script named "" that you want to run with args p1,p2,p3 sourcecode:


while [ 1 -lt 2 ] ; do
    logger "$0 running with parameters \"$@\""
    sleep 5

create a


echo $$ > /var/run/$
exec "$@"

run through

./ ./ p1 p2 p3 p4 & does nothing more than logging a line to syslog each 5 seconds

you now have the pid in /var/run/

cat /var/run/ 

and is running aok. syslog grep:

Nov 24 16:07:17 pinkpony cia: ./ running with parameters "p1 p2 p3 p4"

you can see it in the process table:

ps axuwww|grep ' p1' |grep -v grep
root      5780  0.0  0.0   4148   624 pts/7    S    16:07   0:00 /bin/sh ./ p1 p2 p3 p4
share|improve this answer
oh, and the "oneliner": /bin/sh -c 'echo $$>/tmp/ && exec program args' & – sysfault Nov 24 '10 at 14:28
To properly preserve internal whitespace in the arguments, you should use exec "$@" instead of exec $*. Technically what you need to preserve is not whitespace but occurrences of the characters in the IFS shell parameter (which defaults to space, tab, and newline). – Chris Johnsen Nov 25 '10 at 3:53
point taken. :) – sysfault Nov 25 '10 at 7:54
Thank you, I did not know $$ parameter. It can be very useful. – rafalmag Nov 27 '10 at 12:12

You can use something like:

$ myCommand ; pid=$!


$ myCommand && pid=$!

The two commands can be joints using ; or &&. In the second case, the pid will be set only if the first command succeeds. You can get the process id from $pid.

share|improve this answer
OP wants to get the PID so he can kill it later. ; and && require the original process to exit before the echo $! is executed. – Iain Nov 24 '10 at 8:58
Yes, you are right. This will give you the pid after myCommand has terminated. – Khaled Nov 24 '10 at 9:02
Referencing $! after && or ; will never give you the PID of the process started for the left-hand side of the command separator. $! is only set for processes launched asynchronously (e.g. usually with & but some shells also have other methods). – Chris Johnsen Nov 24 '10 at 9:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.