roe1974
Posts: 18
Joined: Wed Jul 18, 2018 11:18 am

Changing LED (ex. Heartbeat) when service is active/running

Thu Jul 26, 2018 6:55 am

I want to change die green LED to heartbeat when a certain service (homebridge) is up and running and turn it of when homebridge is stopped ....
Is this possible ?

Richard

User avatar
lmarmisa
Posts: 1260
Joined: Thu Feb 14, 2013 2:22 am
Location: Jávea, Spain

Re: Changing LED (ex. Heartbeat) when service is active/running

Thu Jul 26, 2018 1:27 pm

You will need to develop a script. This command will help:

Code: Select all

service --status-all | grep homebridge
These other commands could be more useful:

Code: Select all

service --status-all | grep homebridge | cut -d' ' -f3
status=$(service --status-all | grep homebridge | cut -d' ' -f3)  #the variable status will contain a char "+" if the service homebridge is running 
https://askubuntu.com/questions/407075/ ... ll-results

roe1974
Posts: 18
Joined: Wed Jul 18, 2018 11:18 am

Re: Changing LED (ex. Heartbeat) when service is active/running

Thu Jul 26, 2018 2:06 pm

its a systemd service an is not listed @

sudo service --status-all

???!?!?!?

and i am not able to write a "script " :-(

any helo out there ?

Richard

User avatar
lmarmisa
Posts: 1260
Joined: Thu Feb 14, 2013 2:22 am
Location: Jávea, Spain

Re: Changing LED (ex. Heartbeat) when service is active/running

Thu Jul 26, 2018 2:09 pm

roe1974 wrote:
Thu Jul 26, 2018 2:06 pm
its a systemd service an is not listed @

sudo service --status-all

???!?!?!?

and i am not able to write a "script " :-(

any helo out there ?

Richard
Hi Richard,

what about this command?

Code: Select all

ps -ef | grep homebridge

roe1974
Posts: 18
Joined: Wed Jul 18, 2018 11:18 am

Re: Changing LED (ex. Heartbeat) when service is active/running

Thu Jul 26, 2018 2:33 pm

yes....
here the result:

Code: Select all

avahi      316     1  0 12:35 ?        00:00:08 avahi-daemon: running [homebridge-raspi.local]
root       485     1  0 12:35 ?        00:02:20 homebridge
pi        1202   879  0 16:31 pts/0    00:00:00 grep --color=auto homebridge
and now ?

Richard

User avatar
lmarmisa
Posts: 1260
Joined: Thu Feb 14, 2013 2:22 am
Location: Jávea, Spain

Re: Changing LED (ex. Heartbeat) when service is active/running

Thu Jul 26, 2018 3:10 pm

One question, Richard. How do you launch the program homebridge?.

roe1974
Posts: 18
Joined: Wed Jul 18, 2018 11:18 am

Re: Changing LED (ex. Heartbeat) when service is active/running

Thu Jul 26, 2018 6:14 pm

systemd service......

as described here:

http://pysselilivet.blogspot.com/2017/0 ... stemd.html

regards, richard

User avatar
lmarmisa
Posts: 1260
Joined: Thu Feb 14, 2013 2:22 am
Location: Jávea, Spain

Re: Changing LED (ex. Heartbeat) when service is active/running

Fri Jul 27, 2018 2:01 pm

This is a starting point for your script:

Code: Select all

#!/bin/bash
ps -A | grep -q homebridge
if [ $? -eq 0 ]; then
  echo "LED on"
else
  echo "LED off"
fi
exit 0
If homebridge is running, the outputl will show LED on. If homebridge is not running, the output will be LED off.

If you want to use this shell script, follow this procedure:

1) Create a bin directory for binaries and scripts:

Code: Select all

mkdir -p ~/bin
2) Update your environment running this command:

Code: Select all

. ~/.profile
3) Edit the script file:

Code: Select all

nano ~/bin/ledcontrol.sh
Copy the text, save and exit.

4) Give exec permission to script

Code: Select all

chmod +x ~/bin/ledcontrol.sh
5) Run the script:

Code: Select all

ledcontrol.sh
The script is not complete. You should substitute the command echo with an appropriate command for controlling the LED.

roe1974
Posts: 18
Joined: Wed Jul 18, 2018 11:18 am

Re: Changing LED (ex. Heartbeat) when service is active/running

Fri Jul 27, 2018 8:19 pm

ok !!!! .... thx a lot !!!!!!!
will try it and come back with feedback ;-)

greetings from vienna
richard

roe1974
Posts: 18
Joined: Wed Jul 18, 2018 11:18 am

Re: Changing LED (ex. Heartbeat) when service is active/running

Sat Jul 28, 2018 7:11 am

ok...works......
but how/when running the script .... with cron job ?

Richard

User avatar
lmarmisa
Posts: 1260
Joined: Thu Feb 14, 2013 2:22 am
Location: Jávea, Spain

Re: Changing LED (ex. Heartbeat) when service is active/running

Mon Jul 30, 2018 3:39 pm

roe1974 wrote:
Sat Jul 28, 2018 7:11 am
ok...works......
but how/when running the script .... with cron job ?

Richard
Of course, you can use cron. Type the command:

Code: Select all

crontab -e
For example, if you want to run your script every two minutes, add an entry like this:

Code: Select all

*/2 * * * * /home/pi/ledcontrol.sh
If you prefer a period lower than 60 seconds, you will have to modify your script. This is an example:

Code: Select all

#!/bin/bash

while true; do
  ps -A | grep -q ntp
  if [ $? -eq 0 ]; then
    echo "LED on"
  else
    echo "LED off"
  fi
  sleep 10
done
Then define an entry like this on crontab:

Code: Select all

@reboot /home/pi/ledcontrol.sh

Return to “General discussion”