开发者

in a sh script, get the pid of a background process

开发者 https://www.devze.com 2023-01-19 10:56 出处:网络
Is it possible to know the pid of the iwevent process in the following bash script: #!/bin/sh ( iwevent | logger -t IWEVENT ) &

Is it possible to know the pid of the iwevent process in the following bash script:

#!/bin/sh
( iwevent | logger -t IWEVENT ) &
echo the pid is: ???

Note that iwevent run until ctrl-c signal.

FYI. I run this script in a /etc/network/interfaces "up"开发者_运维技巧 statement and I want to kill the running iwevent process in the related "down" statement. My aim is to log wireless events.


Something like this should do the trick:

#!/bin/sh
( { iwevent & printf "The pid is %s\n" $! >&3; } | logger -t IWEVENT ) 3>&1 &

If you need it in a variable, read the output of the above.


check "pidof" function
see this http://en.wikipedia.org/wiki/Pidof
and check the man page: man pidof

0

精彩评论

暂无评论...
验证码 换一张
取 消