Linux – How to Get PID of Just Started Process

bashlinuxpidprocessunix

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

myCommand
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.

Best Answer

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

myCommand & echo $!