Start, Stop, check Status of running linux programs (Exec Binding, with HABPanel switch)

Hi everybody,

while playing around with my Raumfeld Devices i came to a point where I want to see if my Raumserver is running on my openHAB Server (Ubuntu).

When testing things out I created a script and HABPanel Widget that can check the status of a linux program and start/stop it. I’ve tried to built it for use with any daemon/program on linux, so it could maybe used for different purpose.
Exec Binding has to be installed.

What i’ve done:

  • Create a bash script runprogram.sh in the openHAB scripts folder
#!/bin/bash
#
# Script used by openHAB2 to check, start and stop a program on command line

status(){
	[ "$(pidof $1)" ] && echo "ON" || echo "OFF";
}

start(){
	$1
}

stop(){
	killall $1
}

case "$1" in
  start)
        start $3$2
        ;;
  stop)
        stop $3$2
        ;;
  status)
        status $2
        ;;
  restart)
        stop $3$2
        start $3$2
        ;;
   *)
        echo "Usage: $0 {start|stop|status|restart} program programpath"
        exit 2
esac

to use this you need to call it with 3 parameters:

  1. what should be done: start, stop, status, restart
  2. program name: this has to be the name of the running process (used to start, check and kill it)
  3. program path: this is the path to the executable file used to start it (see parameter 2)
  • Create a thing for the exec binding
Thing exec:command:raumservercheck [command="/etc/openhab2/scripts/runprogram.sh status raumsrvDaemon /etc/raumserver/raumserverDaemon/", interval=15, timeout=5]
  • Create an string item that receives ON or OFF from the status of the script
String Raumserver "[%s]" {channel="exec:command:raumservercheck:output"}
  • Create a rule that reacts on received commands for item
rule "RaumserverStart"
when
  Item Raumserver received command ON
then
  logInfo("raumserver.start", "Raumserver starting")
  executeCommandLine("/etc/openhab2/scripts/runprogram.sh start raumsrvDaemon /etc/raumserver/raumserverDaemon/")
end

rule "RaumserverStop"
when
  Item Raumserver received command OFF
then
  logInfo("raumserver.stop", "Raumserver stopping")
  executeCommandLine("/etc/openhab2/scripts/runprogram.sh stop raumsrvDaemon /etc/raumserver/raumserverDaemon/")
end

So that switch shows you the current state of the program/daemon and you can start/stop it.

I hope this maybe helps some users or gives you inspiration :slight_smile:

I’m not a linux pro, so maybe this script is not the best. If somebody has ideas to make something better please let me know. At the moment it is maybe not the best solution, but it works for me :stuck_out_tongue_winking_eye:

Michael

2 Likes

@sondi
Realizing that the thread is quite old, I hope you can help me out anyway.

I installed Raumserver on my OH3.2.0 system (PI4) via NPM.
I now want to start/stop it with a rule, similar as your scenario above.
I do not find the raumsrvDeamon anywhere.
What do I need to do to get a start/stop rule working?
thanks,
Frank

Hi ffr,

it has really be a little time since I last used that. A few years ago I’ve switched from Raumfeld to Sonos - so I’m not using this anymore.

When I got your problem right you can’t find the RaumserverDeamon after the installation. I’ve taken a quick look at the Raumserver repo and the one that is installed be npm seems to be a complete new Version that is using nodeJS. So the Raumserver is started with “npm start”. I think you could start and stop the Raumserver with commands like these:

/node_modules/node-raumserver/npm start
/node_modules/node-raumserver/npm stop

The Exec Binding should just run executable files, so maybe you can just use the executeCommandLine command without the use of your own runprogram.sh

executeCommandLine("/node_modules/node-raumserver/npm start")

But maybe anyone with more nodeJS experience can say more. Hope that helps you.

Regards
Michael

Unfortunately, the Statement below did not start Raumserver.

executeCommandLine("sudo","/etc/raumserver/node_modules/node-raumserver/npm","start")

I also tried

executeCommandLine("sudo","node","/etc/raumserver/node_modules/node-raumserver/raumserver.js")

Also did not work.

Both versions work when executed directly from the command line.

Any help is appreciated.

You could see if it tells you why -

val execresult = executeCommandLine(Duration.ofSeconds(10), "sudo","/etc/ ...
logInfo("test", "results: " + execresult)

Getting executeCommanLine to work can be a real bugger sometimes.

This is a good sign that the basic command is correct but something may be keeping it from running within openHAB. My first guess would be to check permissions on the script itself. Make sure the user openHAB runs as can execute the script. Doing so isn’t easy though because usually the user openHAB runs as does not have command line abilities
Edit to add: somewhere on this forum is a thread about getting it working… search

here this is the thread I was thinking about it is about the exec binding but a lot of it still applies to executeCommandLine
https://community.openhab.org/t/how-to-solve-exec-binding-problems/18131

@rossko57
thanks for the hint.
I received the folllowing

We trust you have received the usual lecture from the local System

Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.

    #2) Think before you type.

    #3) With great power comes great responsibility.

sudo: no tty present and no askpass program specified

I then checked my sudoers.d file and changed it, so that the NPM command should be allowed without password.

openhab ALL=(ALL)  NOPASSWD: /usr/bin/ssh, /sbin/shutdown, /sbin/poweroff, /sbin/reboot, /sbin/systemctl, /etc/raumserver/node_modules/node-server/npm start

but still go the same message back from the execCommanline.
what is wrong with my sudoers.d entry?

@sondi
I got the START working with the following setup:

In sudoers.d add for openhab without password:

/usr/bin/npm

Then use the following command on the script:

executeCommandLine("sudo","/usr/bin/npm","--prefix","/etc/raumserver/node_modules/node-raumserver/","start")

Now … it looks like there is no STOP script defined in the package.js file for raumserver. What do I need to do to also enable STOP of raumserver via execcommandline??

Thanks

Hi @ffr ,

nice to hear that you’ve got the START working. Because I’m not involved in the development of the raumserver (i was just a simple user) I can’t do anything to implement such a STOP script into the raumserver.

What you maybe could do is check the processes that are running when the raumserver is running an kill the whole process with linux standard command “kill” or “killall”, maybe with the command

killall node

(if node is the correct name of the process). Or maybe a better way is to get the PID of the running process and kill this one. I think for that you have to write a little bash script that searches the PID and then runs the kill command with that PID as parameter.

I’ve found this information about killing a node process on a linux machine. Maybe some linux enthusiastic can say more about that option.

https://stackoverflow.com/questions/31649267/how-to-kill-a-nodejs-process-in-linux

Hope this helps you

Regards
Michael