XivMitmLatencyMitigator icon indicating copy to clipboard operation
XivMitmLatencyMitigator copied to clipboard

Cleanup script not running on exit when running the script as a systemd service

Open ghost opened this issue 2 years ago • 5 comments

try: at line 1588 breaks early because of https://github.com/Soreepeong/XivMitmLatencyMitigator/issues/67#issuecomment-1287817048 and not even finally: will run for some reason

Maybe systemd is killing the process early so the script can't find it and it won't execute the rest of the code?

https://github.com/Soreepeong/XivMitmLatencyMitigator/blob/a32f36233077da9bcff51e9f6101c915fb954664/mitigate.py#L1588-L1663

ghost avatar Oct 22 '22 13:10 ghost

While playing around with logging.info() I noticed the while loop breaks at sock, source = listener.accept() and everything below that and outside of the loop won't be executed. Even the finally: at the very bottom won't be executed at all https://github.com/Soreepeong/XivMitmLatencyMitigator/blob/a32f36233077da9bcff51e9f6101c915fb954664/mitigate.py#L1617-L1635

ghost avatar Oct 22 '22 14:10 ghost

Why not just add the cleanup script to the ExecStopPost option in your unit file? Might be worth the unit separating out the components too but that'll get weird.

ayyaruq avatar Oct 24 '22 04:10 ayyaruq

That's what I did for now as a workaround but the problem yet remains

[Unit]
Description=XivAlexander script

[Service]
Type=simple
User=root
WorkingDirectory=/root/xivalexander
#ExecStart=/bin/bash run.sh
ExecStart=/usr/bin/python3 mitigate.py
ExecStop=-/bin/bash .cleanup.sh
ExecStop=-/bin/rm .cleanup.sh

[Install]
WantedBy=multi-user.target

ghost avatar Oct 24 '22 10:10 ghost

Ah I follow now, should maybe just make cleanup callable as a param, or write the iptables rules to some file that can be consumed by cleanup independent of the in-memory ports and crap.

I keep the cleanup script around for manual use, so it works just fine in systemd (except I don't use systemd), mostly since using fixed ports. Could be worth it for you, just add a flag and set port to it, env var it, or hardcode if you need. Probably a good PR to make port configurable.

ayyaruq avatar Oct 30 '22 04:10 ayyaruq

Try this:

[Unit]
Description=XivAlexander script

[Service]
Type=simple
User=root
WorkingDirectory=/root
ExecStart=/bin/bash run.sh
Restart=on-failure
RestartSec=5
KillSignal=SIGINT

[Install]
WantedBy=multi-user.target

(Added KillSignal=SIGINT line to [Service])

#!/bin/bash
curl https://raw.githubusercontent.com/Soreepeong/XivMitmLatencyMitigator/main/mitigate.py | exec python3

(Added exec to python3)

It makes it so that sigint (ctrl+c) is passed to the mitigate.py script

xzn avatar Jan 14 '23 14:01 xzn