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.

##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 :
_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
echo 0 > /sys/class/gpio/gpio18/value  #turn off LED

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?

  • This sounds like a Linux administrative question for the most part. What you seem to want to know is whether the WiFi adapter is plugged in or not? This is a different question than whether the process is running or not. The process may still exist even if the WiFi adapter is removed and that sounds like what is happening. The process runs and if you kill it then Raspbian just restarts it. Feb 4 '18 at 15:55
  • To check whether the wifi interface exists, check the output of ip link. To check whether it is connected, check ip addr.
    – goldilocks
    Feb 4 '18 at 15:57
  • 1
    BTW, whoever you copied that script off of does not know how to code either. It creates a busy loop, meaning it will hog a processor core while running, which is totally unnecessary. Throw a sleep 5 or something into that loop.
    – goldilocks
    Feb 4 '18 at 16:09
  • Ok, thank you both for your quick replies. What I really want to do is have an reliable script that will check that airodump-ng is running as expected. In my world checking that the adapter is plugged in, is one of many things that that go wrong and stop airodump-ng to work as intended. Can you suggest any other solution that would be more reliable solution? Also, just noticed,If the LED has been switched on once, it will not come off even if the process is killed. Any suggestions to this? Sleep command has been implemented. Once again, thanks!
    – Noah Smith
    Feb 4 '18 at 18:24

If you do a

ps -ef | grep blah_blah

the result is:

root     17625 17607  0 19:21 pts/0    00:00:00 grep blah_blah

and therefore, the test will always be true. If you do:

ps -ef | grep blah_bla[h]

the result is false. So, from that, looking to your script, it would be:


echo "18" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio18/direction

while ps -ef | grep airodump-n[g] > /dev/null ; do
    # put on the led repeatedly, because someone may turn it of when we're not looking
    echo 1 > /sys/class/gpio/gpio18/value #turn on LED
    sleep 1
echo 0 > /sys/class/gpio/gpio18/value  #turn off LED

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.