SIGTERM is not handled correctly, re-requests password

This forum is for admins who are looking to build or expand their OpenVPN setup.
Forum rules
Please use the [oconf] BB tag for openvpn Configurations. See viewtopic.php?f=30&t=21589 for an example.
Post Reply
cherio
OpenVpn Newbie
Posts: 2
Joined: Wed Jun 24, 2020 7:33 pm

SIGTERM is not handled correctly, re-requests password

Post by cherio » Wed Jun 24, 2020 7:51 pm

I automated openvpn startup via my own custom systemd service. It works well but killall openvpn fails to shutdown the process when invoked from the systemd service. Here is my service, it is as simple as a brick (/etc/systemd/system/my-ovpn-service.service):

Code: Select all

[Unit]
Description=start/stop my OpenVPN connection on startup/shutdown
After=network-online.target
Requires=network-online.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/path/to/startup_script
ExecStop=/usr/bin/killall openvpn

[Install]
WantedBy=multi-user.target
When I run /usr/bin/killall openvpn from the command line it works as expected. I tried replacing it with killall openvpn, and with /usr/bin/pkill openvpn, tried specifying the signal explicitly as pkill -SIGTERM openvpn etc. All those invocations work as expected from command line (openvpn connection shuts down) but fail when executed from the service via ExecStop=. All it does is tries to re-authenticate into the remote server with the password.

I am lost at what I can do and need advice from the experts here

User avatar
TinCanTech
OpenVPN Protagonist
Posts: 7389
Joined: Fri Jun 03, 2016 1:17 pm

Re: SIGTERM is not handled correctly, re-requests password

Post by TinCanTech » Wed Jun 24, 2020 7:59 pm

Try:

Code: Select all

man killall

cherio
OpenVpn Newbie
Posts: 2
Joined: Wed Jun 24, 2020 7:33 pm

Re: SIGTERM is not handled correctly, re-requests password

Post by cherio » Wed Jun 24, 2020 10:04 pm

TinCanTech wrote:
Wed Jun 24, 2020 7:59 pm
Try:

Code: Select all

man killall
man killall seems to be working as expected showing man for killall.

Turns out the problem was not OpenVPN. It was correctly shutting down. The problem is systemd expects ExecStop command to wait until openvpn shuts down. Since the process needs time to shutdown it was still present when systemd checked whether the service was still alive; it determined the process didn't shutdown successfully and re-spawned it :!:

I solved it by adding another ExecStop line

Code: Select all

ExecStop=/bin/bash -c "while /usr/bin/pgrep openvpn>/dev/null; do /bin/sleep 0.02; done"
after killall openvpn. This way systemd was satisfied.

User avatar
TinCanTech
OpenVPN Protagonist
Posts: 7389
Joined: Fri Jun 03, 2016 1:17 pm

Re: SIGTERM is not handled correctly, re-requests password

Post by TinCanTech » Wed Jun 24, 2020 10:37 pm

OpednVPN fully supports systemd .. perhaps you should consider using the tools correctly ?

Post Reply