I have been migrating to openHAB 2 and noticed a few things about starting and stopping openhab 2. I have been starting with ./start.sh, this works fine and opens the console, but causes issues if that terminal window dies. When I try nohup ./start.sh it never fully starts.
Also whats the best way to shutdown, with openHab 1.8.x I just did a kill on the pid and it would shutdown cleanly unless it was messed up and then I had to do a kill -9. With openHab 2.x I have tried shutdown then yes in the console and it does indeed shut some things down, but no matter how long I wait it does not fully shutdown unless I kill -9 the pid.
The common way to start/stop openHAB would be a script, either init, or (more common these days) systemd (belongs to your linux distribution).
You would start openHAB as a service, e.g. with sudo systemctl start openhab2.service.
You could look for the status of openHAB using systemctl status openhab2.service (yes, without sudo).
And you could stop openHAB using sudo systemctl stop openhab2.service, that’s pretty simple.
To be honest, you wouldn’t have the log output, but this would make no sense in 99% of time anyway
You can get the whole log the same way as in openHAB1 through openhab.log and events.log file, or - much more convenient - through karaf console.
Create the file /lib/systemd/system/openhab2.service with the following content:
[Unit]
Description=The openHAB 2 Home Automation Bus Solution
Documentation=http://docs.openhab.org
Wants=network-online.target
After=network-online.target
[Service]
Type=simple
User=openhab
Group=openhab
GuessMainPID=yes
WorkingDirectory=/opt/openhab2
#EnvironmentFile=/etc/default/openhab2
ExecStart=/opt/openhab2/start.sh server
ExecStop=/bin/kill -SIGINT $MAINPID
Restart=on-failure
[Install]
WantedBy=multi-user.target
Next, enable the service to be executed on system startup, start the service and retrieve status information:
# initialize the new service (execute only once)
sudo systemctl daemon-reload
sudo systemctl enable openhab2.service
#start and retrieve status
sudo systemctl start openhab2.service
sudo systemctl status openhab2.service