Rasberry Pi/Openhabian : useful scripts for starting, stopping, rebooting, shutting down, clearing the cache, and starting/stopping z-way server

Please note Markus’ comments below. These scripts are more for a memory aid for myself, and may be a useful reference to find the commands for others. They are not necessarily the best way to go about these processes, and some (cleaning the cache) should not be done other than after upgrade or specific exceptional circumstances, and will slow your system down considerably while everything is re-loaded/configured.

In sorting out some bugs on my system, I’ve been making heavy use of some scripts I made some time ago, and I figured I’d share them. As with many of the users on here, my everyday job doesn’t involve linux commands, and (ideally) I don’t often have to go deep into openhab to adjust settings often. As such, I would have to re-look up the correct commands often.

  1. ohstop (stops openhab)
echo "Stopping Openhab.  Note: This should be run as sudo"
systemctl stop openhab2.service
  1. ohstart
echo "Starting Openhab.  Note, This should be run as sudo"
systemctl start openhab2.service
  1. nicereboot (shuts down openhab, waits, then reboots. you may need to adjust the delay)
echo "Starting a nice shutdown.  Ensure this is run as sudo."
echo "Shutting down openhab, waiting 35 seconds ..."
systemctl stop openhab2.service
sleep 35
echo "rebooting"
reboot now
  1. niceshutdown (stops openhab, waits (for the shutdown of OH to be complete), then shuts down the pi. Ideally this helps stop corrupted files.
echo  "Shutting down nicely.  Ensure this is run as sudo."
echo "Stopping Openhab, waiting...35 seconds"
systemctl stop openhab2.service
sleep 35
echo "Shutting Down Now"
shutdown now
  1. clearstart (stops openhab, clears the cache, then restores permissions, and then restarts openhab)
echo "Script to stop openhab, clear the cache, fix file ownership, and then restart openhab.  Should be run as sudo."
echo  "Stopping Openahab"
systemctl stop openhab2.service
echo "Cleaning Cache"
openhab-cli clean-cache
echo "Resetting ownership"
openhab-cli reset-ownership
echo "Starting openhab service"
systemctl start openhab2.service

==
Finally, useful for those who use raZberry hats (or other z-way server compatible z-wave dongles), I have scripts to move between z-way and openhab. This is useful if using z-way to backup your z-wave controller or to update the controller’s firmware. (To install it, see: https://storage.z-wave.me/z-way/download-z-way/index.php?id=24 - but be aware if you install it, you may need to disable the autostart for ‘z-way-server’ and ‘zbw_connect’ with update-rc.d for openhab z-wave binding to work correctly with future restarts)

  1. startzway
echo "Start switching over to z-way"
echo "Stopping Openhab"
sudo systemctl stop openhab2.service
echo "Start Z-Way"
sudo /etc/init.d/z-way-server start
echo "go to port 8083 for ZWay server"
  1. stopzway
echo "Stop Z-way, start Openhab"
echo "Stop Z-Way Server"
sudo /etc/init.d/z-way-server stop
echo "Stop zbw_connect"
sudo /etc/init.d/zbw_connect stop
echo "Start Openhab"
sudo systemctl start openhab2.service
echo "Done.  If still having issues, you may need to remove z-way autostart.  Hint: sudo update-rc.d -f z-way-server remove"

==
for those just starting out, you can create these files in your home directory on the raspberry pi. You can use nano to create/edit these files.

nano ~/ohstop

then paste the code in, press ‘control-X’ then ‘Y’ then [enter] (exit, save yes, enter).

You then need to make the script executable:

chmod +x ~/ohstop

You then can run them with:

sudo sh ~/ohstop

Alternately, from the home dir, you can run sudo ./ohstop.

Note, the ~ is short-hand for /home/openhabian, or whatever the currently-logged-in user’s home directory is.

If there are other useful scripts, or if there are problems with my scripts, I’m open to feedback!

Ben

3 Likes

Thanks for sharing, I appreciate your willingness to help and give back to the community.
But you are recommending actions I disagree with. And your post leaves readers with the impression that there are bugs/deficits in standard openHABian related to those actions you scripted and that’s something I feel I need to correct.

First, OH is supposed to run 24x7. In proper installs, there is no need to restart it so you should NOT ease that. To restart is usually bad advice, in particular to beginners.
The number of occasions where you REALLY need to is negligibly small.
And #1,#2 are just simple 1:1 aliasing, i.e. just shortcuts for lazy people to type in less, so you ARE targeting beginners/users to frequently type this.

Second, there is NO need to reboot (except in very rare cases such as a kernel update).

Third, there is NO need to wait after shutdown or before rebooting.
And things get a lot more complicated when you also make use of ZRAM and/or Samba. as there’s dependencies on each other. Another good reason not to restart OH when you don’t have to.

Fourth, there is NO need to clear the cache except after OH installation or upgrades.
It is actually harmful to startup speed and HW reliability if you clear the cache in situations other than these.

There also should not be the need to correct ownerships when you take care of with this when creating new config/items/rules files. To auto-correct stuff means to hide that stuff fromawareness, and this quite often is preventing a proper fix.

Sorry for not honoring your contribution but we’re working hard inside openHABian to make all this work properly based on standard Linux mechanisms, so it`s not helpful to our work when you spread word that there is a need to do things in different ways. You can do and recommend whatever you like when you installed your own Linux from scratch, but please neither do nor recommend this on openHABian.

If you feel a use case is not properly covered there, open a Github issue please to let us know.

PS: and on the RaZberry part, you should not start zbw_connect. It’s to auto-update parts of your ZWay related software which is not a good idea as it can get you unwanted software changes that might interfere with proper ZWave operation in OH.

1 Like

Also just printing it to user to run it as sudo is not really great. If this is a do everything in one script, the script should check if the current user have superuser rights.

Also I can’t really understand why you have to stop openhab first and just after reboot… Why don’t close all services then before reboot?

Also if you use shutdown -r you will have the same effect (you can also see that on the screen showing that a job is running for xy service to stop that…).

Thanks Markus, those are some great points. I hadn’t intended to convey there being bugs/deficits in openhabian. I ran openhabian without having to touch anything significant for over a year.
These scripts were meant to sit, unused, perhaps for years, until you need them while sorting out unforseen bugs.
My challenges began with an sd card fail that progressed to me choosing to upgrade my raZberry hat firmware, which (I think) changed the frequency of the z-wave dongle that took weeks to figure out why it couldn’t see/communicate with my devices. All of this involved a lot of troubleshooting, clearing cache (and I now always fix permissions after clearing the cache, after being burned once, possibly when clear cache was run as the wrong user).

My plan was not to make it easy to often run these commands, but to be able to remember the commands when a lot of time has passed (my usual problem). I can see, though, that it may have this message for some.
Finding these commands is not always easy, and this makes it easy. I could put them all in a document, but given that I connect from a variety of computers, sometimes internet-facing and sometimes not, the common denominator is residing on the pi - which is why I had put them in scripts there. For those of us who spend <<1% of our time and brain space in linux systems, it’s important to make it easy to do things the right way, and spending 10 minutes to again figure out which of the websites had the right way mentioned in the comments. If I want to get ‘under the hood’, it’s as easy as typing ‘cat scriptname’.

I understand why you may be concerned about the optics with this one, and certainly want to be appreciative of the huge amount of work you and the development team puts into openhab / openhabian development. If you’d rather the post not be up, that’s fine and I can take it down - but I do stand beside my choice in making the scripts, using when needed.

PS for the zbw_connect portion, you’ll notice that I didn’t stop it - only stop it. I had seen it running, and openhab wasn’t able to find the dongle (ended up being due to the serial port changing), so it made sense to try stopping it as well.
.

@rkrisi - I hadn’t been aware of how to (or considered to look into how) to check if the user is sudo.
This could certainly be added, especially if polishing for others’ use. I think stopping openhab came from old habits before shutdown ensured all processes were shut down cleanly first. I had my an issue once last year where my settings were apparently corrupted, happening after I had used a simple shutdown command, and since then revived old habits. Does shutdown -r run a clean openhab shutdown? If so, that makes it much simpler.

You’re free to do whatever you like on your box, but with this post you turned your solution into a recommendation to others, and that’s no good one for Joe-Average user, hence my reply.
(btw, why don’t you just store all your information including those “hard to remember” commands in a simple text file on your Pi ? ‘cat filename’ whenever you have to remember).

yes

You can do something like:

if [ "$EUID" -ne 0 ]
  then echo "Please run as root"

or
use [“$EUID” -e 0] to check if is root (the above checks if not root).

Afaik yes shutdown -r does a clean shutdown. Buster also has some improvements with the simple reboot command so there this should also apply to the reboot command. (It waits for every single service to close gracefully before continuing with the shutdown).

1 Like

Hi Ben
I have stored about 10 txt files with nearly same commands, now I have just 1 after copying your set :slightly_smiling_face:
Thanks for this!

I am neither an expert in Linux nor IT pro, mostly just playing around with my OH installation.
I understand the doubts from Markus and Kristof for new users but appreciate your work posting this!

1 Like

Thanks for the clarification on the shutdown, Markus. This is useful when unplugging the pi, swapping battery backups, or triggering when my battery backup gets low.

I’ve added a disclaimer to my initial post that hopefully will convey your concerns prior to me giving the wrong ideas.
Yes, having a single text file on the pi to cat would accomplish the same thing - and I may move in this direction.