I wish to ping my router in background process. Also I want to control ping process as a service: start, status, stop. So, I create lock file and store PID in it. But actually PID is changing, and I cannot kill my process. I store PID with variable $$, but it changing, I don’t know why, may be because I run pipe or may be I run process in background? Could you advise me, how to get PID in sh script, where I run pipe in background?
Code of my sh script ping.sh:$ cat ping.sh
#!/bin/sh
option=$1
if [ "$option" = "start" ]; then
if [ -f /tmp/ping.lock ]; then
echo There is another ping process. Exit
exit 1
else
touch /tmp/ping.lock
(ping 192.168.1.1 | ts | tee -a >> ~/1) &
echo $$ > /tmp/ping.lock ##here i store script pid
fi
elif [ "$option" = "status" ]; then
if [ -f /tmp/ping.lock ]; then
echo ping is running. PID:
cat /tmp/ping.lock
else
echo ping is not running
fi
elif [ "$option" = "stop" ]; then
if [ -f /tmp/ping.lock ]; then
echo killing ping process
kill $(cat /tmp/ping.lock)
echo removing lock file
rm /tmp/ping.lock
echo ping stopped.
else
echo ping is not running.
fi
else
echo Usage: $0 [start, status, stop]
fi
Actually line echo $$ > /tmp/ping.lock ##here i store script pid
does not store actual PID, but that pid that is stores disappears. And this call does not kill my script:
$ ./ping.sh
Usage: ./ping.sh [start, status, stop]
$ ./ping.sh start
There is another ping process. Exit
$ rm /tmp/ping.lock
$
$ ./ping.sh start
$ ps -ef | grep ping
y 5271 2221 1 19:36 ? 00:00:10 /usr/lib/chromium/chromium --show-component-extension-options --enable-gpu-rasterization --no-default-browser-check --disable-pings --media-router=0 --enable-remote-extensions --load-extension
y 5993 4075 0 19:44 pts/2 00:00:00 vim ping.sh
y 6089 1 0 19:45 pts/5 00:00:00 /bin/sh ./ping.sh start
y 6090 6089 0 19:45 pts/5 00:00:00 ping 192.168.1.1
y 6095 5241 0 19:45 pts/5 00:00:00 grep ping
$
$ cat /tmp/ping.lock
6087
$
$ ./ping.sh stop
killing ping process
./ping.sh: 27: kill: No such process
removing lock file
ping stopped.
$
$ ps -ef | grep ping
y 5271 2221 1 19:36 ? 00:00:10 /usr/lib/chromium/chromium --show-component-extension-options --enable-gpu-rasterization --no-default-browser-check --disable-pings --media-router=0 --enable-remote-extensions --load-extension
y 5993 4075 0 19:44 pts/2 00:00:00 vim ping.sh
y 6089 1 0 19:45 pts/5 00:00:00 /bin/sh ./ping.sh start
y 6090 6089 0 19:45 pts/5 00:00:00 ping 192.168.1.1
y 6148 5241 0 19:46 pts/5 00:00:00 grep ping
As you can see, after calling ./ping.sh stop
there is still process of my script and its PID is 6089, while in lock file stored PID 6087. How to get PID 6089 stored in lock file? When and how to store it?