How to enable/disable service on the linux host


I would like to bind Linux services in my OpenHab instance.

Is there an easy way to use “Switch” item type bound on “sudo service x status”, “sudo service x enable”, “sudo service x disable” ?


You should be able to use this to run command prompt commands.

I don’t really understand how to bind 3 different commands to one item.

Is it something like this ?
{ exec="<[service x status:60000] exec=">[ON:service x start] >[OFF:service x stop]" }

What do you expect the return data of service x status to be? I think depending on your answer a Switch item may not be suitable as a Switch can only have a state of ON, OFF or NULL/UNINITIALIZED.

I’m running on a Raspberry Pi, Debian Jessie (sometimes Raspbian, sometimes OSMC)
The real command like will be “systemctl status nginx” and the result is like

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2015-01-27 19:41:23 EST; 22h ago
 Main PID: 495 (nginx)
   CGroup: /system.slice/nginx.service
           ├─495 nginx: master process /usr/bin/nginx -g pid /run/; error_log stderr;
           └─496 nginx: worker process

Jan 27 19:41:23 desktop systemd[1]: Starting A high performance web server and a reverse proxy server...
Jan 27 19:41:23 desktop systemd[1]: Started A high performance web server and a reverse proxy server.

I think you’ll probably need two items. A switch which runs start and stop commands and a String item that runs and returns status.

and don’t forget: normally only root is allowed to use service x start|stop, so you have to grant special rights to the openhab-user.

How to grant special rights for systemctl ?

When using systemd, you have to perform

sudo systemctl start|stop|restart openhab2.service

There is a group for using journalctl as a normal user (systemd-journactl) but I’m not aware of a group to grant full systemctl access. So, you will have to add the openHAB-user to sudoers file with NOPASSWD option (please use visudo for editing, as it’s possible to lockout all users from sudo when messing up the file)