From crontab to systemd Feb 2016

The crontab “@reboot” approach mentioned in the hub’s installation guide has as benefit that it’s very easy to do, without the risk of messing up anything serious, because it doesn’t involve “sudo”. It also should work on just about any system - cron has been around for a long time.

But if you’re willing to do just a little more work, there’s actually a more flexible mechanism in recent Linux distributions, called systemd: it will take care of starting and stopping a service, all its output logs, and catching any runaway or otherwise failing launches.

Here’s how to set up the hub to run under systemd as a service, but first:

Now create a file called “jet.service”, with the following lines in it:

[Unit]
Description=JeeLabs JET Daemon
After=mosquitto.service
After=network.target

[Service]
WorkingDirectory=/home/jcw/jet-v4
ExecStart=/home/jcw/jet-v4/hub-linux-arm
User=jcw
Restart=always

[Install]
WantedBy=multi-user.target

Note that this is set up to wait for both Mosquitto and the network to be ready.

Be sure to check the ExecStart, WorkingDir, and User settings, and adjust as needed for your situation. If you prefer to put the hub (and its data store and packs) in a more central directory: there’s an “/opt“ area intended for just that purpose. Here’s how you can migrate the hub to it:

sudo mkdir -p /opt
sudo mv ~/jet-v4 /opt/

In which case the jet.service file will need to be adjusted to:

WorkingDirectory=/opt/jet-v4
ExecStart=/opt/jet-v4/hub-linux-arm

And if you’ve set up a “jet” script, you’ll need to adjust the path in there as well.

The last step is to put the service in place:

sudo chown root:root jet.service
sudo mv jet.service /etc/systemd/system/

Now you can start and stop the hub (and its child processes, i.e. active JET packs) at will:

sudo systemctl start jet
sudo systemctl stop jet

One thing to beware of is that you need to enable the service if you want it to also start automatically on power-up or after a reboot:

sudo systemctl enable jet

You only need to do this once, it’ll stay that way until you disable it again.

To see the status and the last few lines of the hub’s output, use … you guessed it:

sudo systemctl status jet

Here is some sample output with a freshly-installed hub:

$ sudo systemctl status jet
● jet.service - JeeLabs JET Daemon
   Loaded: loaded (/etc/systemd/system/jet.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2016-02-02 16:37:45 CET; 7s ago
 Main PID: 11645 (hub-linux-arm)
   CGroup: /system.slice/jet.service
           └─11645 /opt/jet/hub-linux-arm

Feb 02 16:37:45 xudroid systemd[1]: Started JeeLabs JET Daemon.
Feb 02 16:37:45 xudroid systemd[1]: Starting JeeLabs JET Daemon...
Feb 02 16:37:45 xudroid hub-linux-arm[11645]: 2016/02/02 16:37:45 [JET/Hub] ...)
Feb 02 16:37:45 xudroid hub-linux-arm[11645]: 2016/02/02 16:37:45 connected ...3
Feb 02 16:37:45 xudroid hub-linux-arm[11645]: 2016/02/02 16:37:45 opening da...b
Feb 02 16:37:45 xudroid hub-linux-arm[11645]: 2016/02/02 16:37:45 starting H...7
Hint: Some lines were ellipsized, use -l to show in full.

(note: the above still shows duplicate timestamps - this has been fixed in the latest hub revision)

If you want to shorten this last command, add the following line to your “~/.bashrc” script:

alias jets='sudo systemctl status jet'

(as always with a .bashrc change: re-load or re-login to put it in effect)

Now, typing “jets” will give you a quick glimpse of the JET/Hub’s status.

It’s really convenient and the new “standard” way to run services in Linux: letting you start, stop, and check up on the hub at any time. Thanks to Thomas L. for his suggestion and help with this.

Weblog © Jean-Claude Wippler. Generated by Hugo.