Search code examples
shellgrepps

get process id through Perl script


In my script, I am executing cat, then trying to grep to get the process id.

I used this :

ps | grep -e \'cat$\' | cut -d\' \' -f2 | head -n 1

but it's not returning anything.


Solution

  • When you start a background process in a shell with program & you can access the PID of the child process through $!.

    For example:

    bash-4.2.37$ cat &
    [1] 9664
    bash-4.2.37$ CAT_PID=$!
    ...time passes...
    bash-4.2.37$ echo $CAT_PID
    9664