Running Thing

Once Building Thing, let's run it

Run

Manually run Thing binary from the command line:


$ ~/go/bin/thing

[dc_a6_32_7a_a6_d0] Model: "Thing", Name: "Thingy"

[dc_a6_32_7a_a6_d0] Not handled [SYSTEM]: {"Msg":"_CmdInit"}

[dc_a6_32_7a_a6_d0] Public HTTP server listening on port :80

[dc_a6_32_7a_a6_d0] Skipping public HTTPS server; port is zero

[dc_a6_32_7a_a6_d0] Skipping private HTTP server; port is zero

[dc_a6_32_7a_a6_d0] Skipping tunnel to mother; missing host

[dc_a6_32_7a_a6_d0] Received [SYSTEM]: {"Msg":"_CmdRun"}

Thing will log startup and running messages to standard output (and errors to standard error output).


Browse to http://localhost on Thing and see "Hello!" in browser.

Automatic Run

We can set up a systemd script to automatically start and run your Thing on system startup as a service.

Create a new file /lib/systemd/system/thing.service with this content:

[Unit]

Description=Thing

ConditionPathExists=/home/merle/go/bin/thing

After=network.target

[Service]

Type=simple

User=merle


WorkingDirectory=/home/merle/merle/

ExecStart=/home/merle/go/bin/thing


Restart=on-failure

RestartSec=1


StandardOutput=syslog

StandardError=syslog

SyslogIdentifier=thing

[Install]

WantedBy=multi-user.target


Adjust the script to use the correct /home/user directory path and binary name for your Thing (the blue items in the script).

The script will automatically restart Thing if it crashes or if the system restarts.

The script will log standard output/error to syslog. The log entries will be prefixed with [thing]. See next section to set up logging.

To install and start script:

$ sudo systemctl daemon-reload

$ sudo systemctl enable thing

$ sudo systemctl restart thing


To check on the service status, use:

$ sudo systemctl status thing

● thing.service - Thing

Loaded: loaded (/lib/systemd/system/thing.service; enabled; vendor preset: enabled)

Active: active (running) since Fri 2022-07-29 19:43:25 BST; 24s ago

Main PID: 91426 (thing)

Tasks: 6 (limit: 1830)

CPU: 22ms

CGroup: /system.slice/thing.service

└─91426 /home/merle/go/bin/thing


Jul 29 19:43:25 raspberrypi systemd[1]: Started Thing.

Jul 29 19:43:25 raspberrypi thing[91426]: [dc_a6_32_7a_a6_d0] Model: "Thing", Name: "Thingy"

Jul 29 19:43:25 raspberrypi thing[91426]: [dc_a6_32_7a_a6_d0] Not handled [SYSTEM]: {"Msg":"_CmdInit"}

Jul 29 19:43:25 raspberrypi thing[91426]: [dc_a6_32_7a_a6_d0] Public HTTP server listening on port :80

Jul 29 19:43:25 raspberrypi thing[91426]: [dc_a6_32_7a_a6_d0] Skipping public HTTPS server; port is zero

Jul 29 19:43:25 raspberrypi thing[91426]: [dc_a6_32_7a_a6_d0] Skipping private HTTP server; port is zero

Jul 29 19:43:25 raspberrypi thing[91426]: [dc_a6_32_7a_a6_d0] Skipping tunnel to mother; missing host

Jul 29 19:43:25 raspberrypi thing[91426]: [dc_a6_32_7a_a6_d0] Received [SYSTEM]: {"Msg":"_CmdRun"}

Logging

Disable Logging

Every Thing will output log messages, by default. While useful for debugging or monitoring, they maybe too voluminous for production. To disable logging, set thing.Cfg.LoggingEnabled = false before running.

(Sorry, no log-levels currently, only a big on/off switch)


Rsyslog

When running Thing as a service, use rsyslog to capture standard output/error to the log file /var/log/thing.log.

Create a /etc/rsyslog.d/thing.conf configuration file with this content:

if $programname == 'thing' then

/var/log/thing.log

& stop

Restart rsyslog after adding configuration file:

$ sudo systemctl restart rsyslog.service

You can restart Thing service and verify log message:

$ sudo systemctl restart thing

$ sudo tail -f /var/log/thing.log

...

What Next?

If you made it this far, you system is setup for Merle development and deployment.

Getting Started is a good next step.