Clearly I'm a beginner when it comes to setting up a script. I'm trying to check if a process is alive and running, and if so, switch on an LED.
I need help with the following:
Check if a process is running and not have crashed or the hardware has not been unplugged etc.
So far I (not really me, I have borrowed it) , have come up with the following script.
#!/bin/bash
##Function to check if a process is alive and running and turning on LED in an infinite loop.
echo "18" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio18/direction
while :
do
_isRunning() {
ps -o comm= -C "$1" 2>/dev/null | grep -x "$1" >/dev/null 2>&1
}
if _isRunning airodump-ng; then
echo 1 > /sys/class/gpio/gpio18/value #turn on LED
else
echo 0 > /sys/class/gpio/gpio18/value #turn off LED
fi
done
The script works and switches on the LED if the process is running. However, if for example, the wifi adapter is unplugged, the LED is still on. How can I get around this? Is there anyway, for example, to use the output of the airodump-ng to see if there are actually any data coming in and use that as an "alive" trigger? I guess you can read from an airodump file and search for a certain keyword in that file and use that as an trigger, but if there are no networks around me to scan, the LED would be off even if the process is alive so I guess that wouldn´t work either?
I would also like to mention that I tried using the
ps aux | grep airodump-ng | grep -v grep command instead of
_isRunning() { ps -o comm= -C "$1" 2>/dev/null | grep -x "$1" >/dev/null 2>&1 } However, in the grep output, "airodump" was still visible even if the process was killed with the killall -9 aidodump-ngcommand with the grep command. How is that possible?
So to boil all the rambling down to a question; How do you in a reliable way check that a process is alive and running and switch on an LED as a status indicator?