Openhab PID file

I’ve installed openhab 1.8.3 form the apt repositories on Raspbian Jessie on RPi2. I’ve set it up to start on boot using systemctl. And all works fine. Reading the startup script that is installed at /etc/init.d/openhab I would expect that there is a PID file located at /var/run/openhab.pid that file however doesn’t exist. Where is the pidfile located in this configuration?

I don’t think OH runs as a forking process. systemd doesn’t create a pid file for non-forking services.

From the systemd.service man page:

Type=

If set to forking, it is expected that the process configured with ExecStart= will call fork() as part of its start-up. The parent process is expected to exit when start-up is complete and all communication channels are set up. The child continues to run as the main daemon process. This is the behavior of traditional UNIX daemons. If this setting is used, it is recommended to also use the PIDFile= option, so that systemd can identify the main process of the daemon. systemd will proceed with starting follow-up units as soon as the parent process exits.

PIDFile=
Takes an absolute file name pointing to the PID file of this daemon. Use of this option is recommended for services where Type= is set to forking. systemd will read the PID of the main process of the daemon after start-up of the service. systemd will not write to the file configured here, although it will remove the file after the service has shut down if it still exists.

Thank you for your reply. What I’m trying to do is monitor the openhab process using jstat. Or to be more precise using check_jstat.sh and icinga. To make this work I need the current PID. Currently I’m using “pgrep java” pragmatically that’s fine, it’s just not guaranteed to actually be the openhab process in every case.

@Steamrunner - did you have any luck? Im trying to do the same thing

Didn’t continued looking for a better solution on this. Still using pgrep java. If you find a better option please let me know. :slight_smile:

ps aux --sort=start_time | grep openhab.*java | grep -v grep | awk '{print $2}' | tail -1
2 Likes

Finally I found a work around for that:
first I modified start.sh found in the openhab directory to :

#!/bin/sh

echo Launching the openHAB runtime...

echo $$ > /var/run/openhab/openhab2.pid
DIRNAME=`dirname "$0"`
exec "${DIRNAME}/runtime/bin/karaf" "daemon"

and rename it startpid.sh, Make sure to add execute permission to the file.

Then go to /lib/systemd/system/openhab2.service file and as root edit it to add in the service section:

# Run ExecStartPre with root-permissions
PermissionsStartOnly=true
ExecStartPre=-/usr/bin/mkdir /var/run/openhab
ExecStartPre=-/usr/bin/chown -R openhab:openhab /var/run/openhab

I put those lines after the user and group definitions.

Replace the ExecStart line :

# ExecStart=/usr/share/openhab2/runtime/bin/karaf $OPENHAB_STARTMODE
ExecStart=/usr/share/openhab2/startpid.sh

and save.

Now reload, stop and start the service:

sudo systemctl  daemon-reload
sudo systemctl stop openhab2.service
sudo systemctl start openhab2.service
sudo systemctl status openhab2.service

And you should be good