1

I have a script that sets some iptables rules (/bin/myrules).

I start it with systemd using this unit file myrules.service:

[Unit]
Description=My Rules

[Service]
Type=oneshot
ExecStart=/sbin/myrules start
ExecStop=/sbin/myrules stop
RemainAfterExit=no

[Install]
WantedBy=multi-user.target

I would like to let systemd know that, after the script is executed and exited, the service is "alive" (if iptables rules are correctly set).

My script is already able to check the status of my iptables rules. Running :

/bin/myrules status

will return a 0 exit code if everything is OK. If there is something wrong, it will return 1.

Is it possible to make the systemctl status myrule command call my specific script to check if status is OK and return a message accordingly ?

Edit: I need systemctl status myrules to display something like

Active: active (whatever)

[Edit-2] But if anything modifies any of the iptables rules, it should re-check if all rules are active and then display

Active: inactive (whatever)

This is possible only if it runs myrules status to check service status.

PS: I don't need more information about iptables, I use the -C option to check

2

With the oneshot type systemd will run the process and once it returns consider the process completed and go back to inactive or if RemainAfterExit=yes keep the service active even after the process has exited.

If your ExecStart script succeeds in changing the iptables configurations it should exit 0 and if it fails it should exit 1. With the use of the RemainAfterExit=yes and the process exit 0 with success it will consider the service active.

You could also use ExecStartPost= to run the status command which will cause the service to fail if the status command fails. However this also means the commands in ExecStart must exit 0.

3
  • The ExecStartPost= executes once the verification command. But if something happens after that, systemctl status is not updated – lauhub Dec 28 '18 at 10:16
  • @lauhub That status comes from the main process indicated by ExecStart. Thats why I mentioned that method first. Your main process should be exiting with proper status codes. – jdwolf Dec 28 '18 at 12:05
  • It is exiting with proper status code, but it is not my question: I wonder if there is a solution integrated into systemd that can check for current status (and not startup status) like for other services which can be restarted if they are dead – lauhub Dec 28 '18 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.