1

I have a script that uses the route command to add or remove a network route. It works fine if I run it from the terminal as a regular script.

However I tried to automate its execution so it runs on boot using a systemd one-shot service.

I've created the service and everything appears to run fine, no complains anywhere, no error codes or messages that I can observe anywhere, however the route is neither added nor deleted.

I suspect systemd is imposing some kind of restriction when running "route add/del" but can't figure out what it is.

I'm running CentOS 7 so adding "+" to the ExecStart, ExecStop is not possible.

I've tried to set KillMode=None (to allow forking from within the script, I know this is not advised) it does not help.

I made a version of the script that only adds/removes the route, this is, for testing purposes I've reduced the script to practically a single line doing "route add"/"route del" to troubleshoot this and I can say 100% that the problem lies on running "route add/del" and not something else in the script borking systemd.

I'm not sure how to proceed, if anyone could offer some insight or ways to debug this it would be great.

This is my service definition, nothing out of the ordinary:

[Unit]
Description=Add/remove network X
After=network-online.target

[Service]
Type=oneshot
KillMode=none
ExecStart=/usr/local/bin/script.sh --start
ExecStop=/usr/local/bin/script.sh --stop

[Install]
WantedBy=multi-user.target

Notes: I'm aware 'systemd-networkd' can create network routes, but this is not what I'm after as the script does many more things besides adding or removing the network route.

4
  • Have you done any testing to confirm if it's running the script at all? Commented Aug 30, 2022 at 12:10
  • What is the output of route add when your script is started with systemd? Did you add set -xv to your script and looked at the output with journalctl? Commented Aug 30, 2022 at 13:55
  • @bmx yes, it is running the script, that I confirmed by adding some echoes in the bash script, I can see the output in the service's status and in the syslog. Commented Aug 30, 2022 at 19:04
  • @ctx, I didn't run the script with set -xv, this is a good suggestion, I added some "echoes" to parts of the script though, I will try that, as this is a script that has been working for a long time I didn't think on using the good old -x (or -xv) Commented Aug 30, 2022 at 19:07

1 Answer 1

3

Ok, found what the problem is, if the service type is oneshot one needs to add to it: RemainAfterExit=yes otherwise what seems to happen when you run systemctl start <service> is that ExecStart is run and then ExecStop. The the script was running fine and the route was being added with ExecStart and then removed with ExecStop.

It makes sense to use RemainAfterExit=yes in my use case because the use case of the service is to change the system state, as stated in the systemd docs.

However I have no clue if this behaviour of running ExecStop after ExecStart when RemainAfterExit=no is a bug in CentOS7 or intended behaviour, I haven't been able to find a detailed explanation on the Systemd documents (Or I might have missed it).

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.