How to run MQTT scrapper at the server startup?

My infrastructure consists of:

  • RPi 4
  • Influxdb latest version at this time
  • Telegraf’s latest version at this time
  • Mosquitto latest version at this time
  • set of sensors sending data to Mosquitto broker.
    I configured all of them, and according to the configuration panel I ran the telegraf command

    And until this command runs my telegraf grabs data from sensors and I see them on the dashboards. But when I break to run this command then data from my sensors aren’t delivered to the telegraf.
    So my question is: what do I have to do to my MQTT scrapper start when my server starts or after rebooting, without my intervention?

To simplify my question. I want to have my telegraf scrapper running as a service. And it can restart when any error occurs. For example something like that:

As you can see, in the end, I restarted telegraf manually, but I want it to happen automatically.

I tried to find out a solution to my problem. What I did do?

  1. I used this website first: How to create systemd service unit in Linux - Linux Tutorials - Learn Linux Configuration

  2. I created a service using a service configuration file (sensors.service):

  3. I created an authentication for Mosquitto broker using Authentication methods | Eclipse Mosquitto

And finally, I have the following:

What I requested to have.

1 Like

Have you considered using something like Easy, proactive monitoring of processes, programs, files, directories, filesystems and hosts | Monit ?


Hi @marekpow,
The service looks great. You can install Telegraf directly as a service though you would still have to edit the service file to point at your hosted Telegraf config in InfluxDB. In default installations the service will look for the Telegraf config at /etc/telegraf/telegraf.conf

@Jay_Clifford I have the Telegraf service installed as a service by default. I’m asked what to do with the MQTT scrapper. And finally, I found such a solution.

Thank you for your reply.

@Pooh looks interesting. Do you use it in your daily practice with a Linux distribution?

Yes, I do. I use it on several Debian & Devuan machines to manage services
like rsyslog, Apache, Icinga, Asterisk, FreeSwitch, InfluxDB, Mosquitto,
OpenVPN, telegraf, Grafana, MariaDB and corosync (which in turn manages
clusters of HA resources). I also use it to manage non-standard applications
and general scripts.

In my opinion monit is simple to configure, light on resources, and produces
helpful log file entries.