Configure Hubot to run in background via systemd

In /etc/systemd/system create a “hubot.service” file and populate it as follows:


$ sudo vi hubot.service

[Unit]
Description=Hubot
Requires=network.target
After=network.target

[Service]
Type=simple
WorkingDirectory=/io

Restart=always
RestartSec=10

ExecStart=/io/bin/hubot --adapter slack

[Install]
WantedBy=multi-user.target

Hubot uses environment variables to store certain parameters. In order for the systemd Hubot service to be make use of environment variables, a hubot.conf file is needed. This file needs to be stored in a specific location so that systemd can read it when starting the service.

In the example below you can see where I’ve configured the Slack Access Token as an environment variable to enable my Hubot to be able to connect to my Slack team.


$ sudo mkdir hubot.service.d

$ cd hubot.service.d

$ sudo vi hubot.conf

[Service]

Environment="HUBOT_SLACK_TOKEN=xxxx-12345678901-XXXXXXXXXXXXXXXXXXXXXXXX"

$ sudo systemctl daemon-reload

With systemd configured to operate Hubot, commands like service hubot start are now available.

Using systemctl enable hubot set systemd to start Hubot automatically whenever the server is restarted.

Here is the output of the service hubot status command after a reboot of the server:


$ sudo service hubot status
Redirecting to /bin/systemctl status hubot.service
● hubot.service - Hubot
Loaded: loaded (/etc/systemd/system/hubot.service; enabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/hubot.service.d
└─hubot.conf
Active: active (running) since Sun 2016-08-21 18:07:47 IST; 10s ago
Main PID: 819 (node)
CGroup: /system.slice/hubot.service
└─819 node node_modules/.bin/coffee /io/node_modules/.bin/hubot --name io --adapter slack

systemd[1]: Started Hubot.
systemd[1]: Starting Hubot...
hubot[819]: [Sun Aug 21 2016 18:07:52 GMT+0100 (IST)] INFO Logged in as io of Chatty McChatface
hubot[819]: [Sun Aug 21 2016 18:07:53 GMT+0100 (IST)] INFO Slack client now connected
hubot[819]: [Sun Aug 21 2016 18:07:53 GMT+0100 (IST)] INFO hubot-redis-brain: Using default redis on localhost:6379
hubot[819]: [Sun Aug 21 2016 18:07:54 GMT+0100 (IST)] INFO hubot-redis-brain: Data for hubot brain retrieved from Redis

Cleaning up Hubot’s Configuration

With Hubot up and running and configured to do more than just display pictures of pugs, I noticed the status output of systemd was very noisy:


$ sudo service hubot start

$ sudo service hubot status

Redirecting to /bin/systemctl status  hubot.service

hubot.service - Hubot

Loaded: loaded (/etc/systemd/system/hubot.service; disabled; vendor preset: disabled)

Drop-In: /etc/systemd/system/hubot.service.d

└─hubot.conf

Active: active (running) since Mon 2016-08-15 04:14:45 EDT; 26s ago

Main PID: 23105 (node)

CGroup: /system.slice/hubot.service

└─23105 node node_modules/.bin/coffee /io/node_modules/.bin/hubot --name io --adapter slack

Aug 15 04:14:45 ip-192-168-20-246.eu-west-1.compute.internal systemd[1]: Started Hubot.

Aug 15 04:14:45 ip-192-168-20-246.eu-west-1.compute.internal systemd[1]: Starting Hubot...

Aug 15 04:14:48 ip-192-168-20-246.eu-west-1.compute.internal hubot[23105]: [Mon Aug 15 2016 04:14:48 GMT-0400 (EDT)] INFO Logged in as io of Chatty McChatface

Aug 15 04:14:49 ip-192-168-20-246.eu-west-1.compute.internal hubot[23105]: [Mon Aug 15 2016 04:14:49 GMT-0400 (EDT)] INFO Slack client now connected

Aug 15 04:14:49 ip-192-168-20-246.eu-west-1.compute.internal hubot[23105]: [Mon Aug 15 2016 04:14:49 GMT-0400 (EDT)] WARNING Loading scripts from hubot-scripts.json i... script.

Aug 15 04:14:49 ip-192-168-20-246.eu-west-1.compute.internal hubot[23105]: Your hubot-scripts.json is empty, so you just need to remove it.

Aug 15 04:14:49 ip-192-168-20-246.eu-west-1.compute.internal hubot[23105]: [Mon Aug 15 2016 04:14:49 GMT-0400 (EDT)] ERROR hubot-heroku-alive included, but missing HU...d= -f2)`

Aug 15 04:14:50 ip-192-168-20-246.eu-west-1.compute.internal hubot[23105]: [Mon Aug 15 2016 04:14:50 GMT-0400 (EDT)] INFO hubot-redis-brain: Using default redis on lo...ost:6379

Aug 15 04:14:50 ip-192-168-20-246.eu-west-1.compute.internal hubot[23105]: [Mon Aug 15 2016 04:14:50 GMT-0400 (EDT)] WARNING The HUBOT_AUTH_ADMIN environment variable not set

Aug 15 04:14:50 ip-192-168-20-246.eu-west-1.compute.internal hubot[23105]: [Mon Aug 15 2016 04:14:50 GMT-0400 (EDT)] INFO hubot-redis-brain: Data for hubot brain retr...om Redis

Hint: Some lines were ellipsized, use -l to show in full.

In order to tidy up the log so that important items don’t get lost in the noise, I removed a couple of unnecessary items from Hubots configuration.

I deleted the “hubot-scripts.json” file, as Hubot no longer uses it. All that happens is a warning is thrown when Hubot starts – it’s harmless, I just removed it for the sake of a less noisy log.

I removed the reference to Heroku from the external-scripts.json file so that the Heroku connector didn’t get called when Hubot starts.

CategoriesUncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s