Zabbix – Restart a service on Ubuntu – Linux

In this tutorial, I will show you, how to restart a service (systemctl) on Ubuntu/Debian monitor with Zabbix.

When we supervise a server and in particular the services, it happens that we want to restart a service which will be “fallen” automatically in order to guarantee a level of service.

Restarting a service to fix an “unknown” problem is not always the solution. Here I will take the example of a service that stops regularly, especially when applying updates.

The tutorial environment:

  • Zabbix 6.X monitoring server in container.
  • Ubuntu server monitor with Zabbix Agent 2

In this tutorial, I will configure Zabbix to restart the crowdsec-firewall-bouncer service which is used to inject blocking rules into Iptables.

The configuration is done in several steps:

  • Creating the script in Zabbix
  • Creating trigger in Zabbix
  • Configure monitored server to give rights to Zabbix user to restart service and configure agent to execute commands.

Creating the script in Zabbix

The first step will be to create our script in Zabbix which will allow us to restart the service when it stops.

From the menu, expand Administration 1 and click on Scripts 2.

By default, 3 scripts are already configured, Click on the Create a script 1 button at the top left.

Name the script 1, choose type Script 2 and select Zabbix agent 3.

Selecting Zabbix agent indicates that the script will be executed by the agent that is installed on the monitored server.

In the Commands field, enter the script executed 1 and then click the Add 2 button.

The script is added and available in Zabbix.

Creating a trigger that will use the script

We will now configure an action trigger, which will use our script to restart the service.

In the navigation menu, unfold Configuration 1, go to Actions 2 and click on Trigger actions 3.

Here, we find all the actions to configure linked to the triggers, click on the Create an action 1 button.

Name the action 1 then click on Add 2 in the Conditions part.

A modal window opens where we will configure the triggering of our action, in type, choose Trigger 1 then click on the Select button 2.

In the host part, choose the server 1 where the trigger is configured, then select the service trigger 2 which indicates that the service is not functional, finish by clicking on the Select button 3> >.

Click on the Add 1 button to create the condition.

The first part of the action is configured, we will now proceed to the execution of the script. Click on Operations 1.

At the level of Operations, click on Add 1.

By default, the operation is send Send message, use the scrollbar to select the script 1 created previously.

In the Target List, check the Current Host 1 box and click Add 2.

The operation is added, click on Add 1.

The action trigger is added.

Configuring Zabbix server and agent

Last game before we can have our services restarting.

On the server where the agent is, enter the command below to edit the /etc/sudoers file:

sudo visudo

At the end, add this line to allow Zabbix user to execute su script command without password:

zabbix ALL=(ALL) NOPASSWD:/bin/systemctl restart crowdsec-firewall-bouncer

Close the editor while saving.

Do not modify the /etc/sudoers file directly with nano or another editor, the visudo editor checks the file before saving. Misconfiguration in the file will block all sudo commands.

We will now configure the agent, open the configuration file:

sudo nano /etc/zabbix/zabbix_agent2.conf

In the search file: ### Option: AllowKey, add at the end of the comment block the line below:

AllowKey=system.run[*]

Search: #DenyKey=system.run[*] and uncomment the line:

DenyKey=system.run[*]

Save and close the configuration file.

Restart Zabbix agent service:

sudo systemctl restart zabbix-agent2.service

Test restarting the service

To test, the easiest way is to stop the service on the monitored machine and check if Zabbix restarts it.

Once the service is stopped, one can see the alert in Zabbix and that the action has been performed.

After checking on the server, we can see that the service is started.


Now you know how to restart a service automatically with Zabbix.

If you plan maintenance on a service, consider disabling the trigger.