How to: Bash PID of last execute command

Hello Everyone

In shell scripting if you want to get PID of last executed command, then you can use BASH `$!` meta variable.

For example lets say if you have below command


# nohup sha256sum 1g.bin &

If you want to find that job PID which is running in the background, use below command


  # echo $!

For Example


[x:/tmp]# nohup sha256sum 1g.bin &
[1] 14866
[x:/tmp]# nohup: ignoring input and appending output to `nohup.out'

[x:/tmp]# echo $!
14866
[x:/tmp]# pgrep sha256sum
14866

Hope it helps.

Thank you.

0 comments:

Post a Comment