In the sh script, get the pid of the background process

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

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

Note that iwevent works before the ctrl-c signal.

Fyi. I run this script in / etc / network / interfaces "up" and I want to kill the current iwevent process in the corresponding "down" statement. My goal is to record wireless events.

+3
source share
2 answers

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

+1
source

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

+6

Source: https://habr.com/ru/post/1768638/


All Articles