Mosquitto MQTT on TinyCore Linux
Build an Extension
There is no prebuilt extension for Mosquitto which means that have to build the program from source and create a custom extension for TinyCore Linux.
First, download the source and unpack:
Next, install build dependencies.
$ tce-load -w make $ tce-load -w gcc $ tce-load -w compiletc $ tce-load -w openssl-dev $ tce-load -w squashfs-tools $ tce-load -i make $ tce-load -i gcc $ tce-load -i compiletc $ tce-load -i openssl-dev $ tce-load -i squashfs-tools
There are several Makefiles in the Mosquitto project directory.
Some of these contain the command
install -s --strip-program foo but the
--strip-program option is not supported by busybox install.
Remove that option (not the call) in all affected Makefiles:
./Makefile ./lib/Makefile ./lib/cpp/Makefile ./client/Makefile ./src/Makefile
Build and Install
According to the Mosquitto README, optional dependencies are
libuuid, both of which are not available.
So we will call
make WITH_SRV=no WITH_UUID=no to build.
The README says to use
WITH_DNS_SRV=no to build without ares.
We need to do this if we do not want to build ares ourselves.
This is not quite correct, it is
Installation is done as root so we have the correct permissions on all installed files.
TinyCore Linux recommends to not include the docs in the extension
(optionally package them in a separate -docs extension). The
option does exactly this.
The TinyCore Linux guide recommends to strip the created binaries.
This has already been done in the Makefiles with
After installation, all relevant files are located in
/tmp/mosquitto/usr/local/sbin/mosquitto to make a test-run.
To run Mosquitto with a dedicated user, create that user:
And edit configuration accordingly.
The config file goes into
This is not the default location, so we need to use the
option when starting Mosquitto to tell it where to look for configuration.
Of course, the config file should be included in the list of files
to be backed up and restored on boot.
Include it in
TinyCore Linux does not come with a syslog daemon. If logging is desired, write to a log file:
log_dest file /var/log/mosquitto.log
On boot, make sure that the file is writable for the
Mosquitto can write connection, subscription and message data to disk
and reload it on every restart.
To enable this in combination with TinyCore Linux, configure Mosquitto with persistence
and include the
mosquitto.db in backup/restore.
persistence true persistence_location /var/lib/mosquitto/ persistence_file mosquitto.db autosave_interval 1800
autosave_interval means that data is saved every 1800 seconds
(30 minutes). Additionally, state is stored on exit.
The persistence directory should be created and chown`ed to
To include it in backup and restore, include it in
To help with shutdown and monitoring, we let Mosquitto write a pidfile:
Start on boot
To start the service on boot, include this in
# start Mosquitto in background touch /var/log/mosquitto.log chown mosquitto:staff /var/log/mosquitto.log mkdir -p /var/lib/mosquitto chown mosquitto:staff /var/lib/mosquitto mosquitto --daemon --config-file /usr/local/etc/mosquitto.conf
This will start Mosquitto as root unless the configuration file defines the user to run as:
Which is recommended.
To shutdown gracefully, include this in
Monitoring with monit
check process mosquitto pidfile /var/run/mosquitto.pid group mqtt if failed uid mosquitto then alert check program mqtt-connect with path "/usr/local/bin/mosquitto_sub --quiet -C 1 --id monit --topic $SYS/broker/version" group mqtt if status != 0 then alert
In order to check if the broker is running, connect to it and retrieve
one message. We request a message from the
$SYS hierarchy and pick
one that is static so that we can be sure that it exists.
$SYS hierarchy is described in the Mosquitto docs 7.
mosquitto_sub comes with the Mosquitto installation an we can use it like this:
This should be sufficient, i.e. exit with "0" if the connection was possible.
-C 1 options is necessary to exit the program after the first message.
Note that you need to escape the "$" for the command line but not in