LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Systemd dependency not working (https://www.linuxquestions.org/questions/linux-newbie-8/systemd-dependency-not-working-4175648835/)

NikkyG 02-22-2019 07:18 AM

Systemd dependency not working
 
Hello,


I am fighting trying to make a dependency between services to work properly. I've looked everywhere and it looks like I'm doing the right thing (at least based on what everyone is saying on how it should be achieved), but unfortunately, it is not working. I want to have my TEST service only run whenever the openvpn.service is running. Unfortunately, it does not stop when I kill the VPN, for example.

Code:

[Unit]
Description=TEST
Requires=openvpn.service
After=openvpn.service

Any help would be appreciated

Thanks a lot

jsbjsb001 02-22-2019 07:26 AM

One of the main ideas behind systemd was automatic dependency resolution, so I'm not sure the "Requires=openvpn.service" line is valid (but I could be wrong).

The "After=openvpn.service" line means don't run your service until the "openvpn.service" has started, if my understanding is correct. I hope you haven't got the "Any help would be appreciated." line in your service, because that is most certainly not valid.

The output you have given won't do what your asking, if I'm understanding your goal correctly.

NikkyG 02-22-2019 07:57 AM

Thanks. I corrected the code :-).

I understood that Requires=... ensures that the service of the [UNIT] (TEST in my case) is running only when the "required" service is running (openvpn in my case).
I have the same understanding as you for the After=... line.

What would you then recommend that I try if I want to:
- start openvpn at startup or if down (added "Restart=always" line in the openvpn@.service [SERVICE] section. I tried and it does work)
- start TEST after openvpn has started
- do not start TEST if openvpn cannot start
- stop TEST if openvpn stops running

It should be a unidirectional command: only start/stop TEST when openvpn is up/down. I do not want to have openvpn depend on the TEST service status.

jsbjsb001 02-22-2019 08:00 AM

I think you could add the "PartOf=<service name here>" keyword there, if you want your service to stop when the openvpn service does.

Here's a link that explains it: https://stackoverflow.com/questions/...ervice-crashes

(look down the bottom of that same link)

NikkyG 02-22-2019 11:41 AM

I read the very same thread and tried this already, without success :-(. Same for "BindsTo=openvpn.service".

jsbjsb001 02-22-2019 11:53 AM

You could have a look at this thread. From what I read, it looks like it might be "BindsTo=<service name here>" that you need instead.

Let us know how it works out.

NikkyG 02-22-2019 12:59 PM

Sorry for the typo. I tried to replace "Requires=" by "BindsTo=" as well (saw this post too :-)). Does not work.

ondoho 02-23-2019 01:38 AM

Quote:

Originally Posted by NikkyG (Post 5965540)
I read the very same thread and tried this already, without success :-(. Same for "BindsTo=openvpn.service".

Quote:

Originally Posted by NikkyG (Post 5965580)
Sorry for the typo. I tried to replace "Requires=" by "BindsTo=" as well (saw this post too :-)). Does not work.

sorry, but this isn't good enough.
jsb has given you plenty hints, now you need show us something to work with.
commands issued, journal excerpts, systemctl status etc.
and continue reading that documentation:
Code:

man -K bindsto
PS: if that other software is so tightly bound to your openvpn requirements, maybe you could replace the current openvpn.service with, say, my-openvpn.service, which executes a shell script that does exactly what you want?

NikkyG 02-23-2019 02:25 AM

Quote:

sorry, but this isn't good enough.
jsb has given you plenty hints, now you need show us something to work with.
I'm very much willing to provide whatever you need to know, but I'm in the Newbie section for a reason ;-).

I can recap what I did:

1- I stopped TEST manually.
2- I edited the [UNIT] section

Code:

[Unit]
Description=TEST
Requires=openvpn.service
After=openvpn.service

I replaced "Requires=openvpn.service" by "PartOf= openvpn.service".

3- I reloaded TEST and restarted it manually.

4- I stopped the VPN:
Code:

sudo pkill openvpn
(I checked after this command and the VPN was down as expected - the IP address is different) but the TEST service was still running (TEST is actually transmission-daemon in case that helps, but it's shorter to write TEST in this thread)

I did the same by replacing then "PartOf= openvpn.service" by "BindsTo= openvpn.service" without succeeding in making TEST stop.



When the VPN is running this is the status:

Code:

sudo systemctl status openvpn

* openvpn.service - OpenVPN service
  Loaded: loaded (/lib/systemd/system/openvpn.service; enabled; vendor
  Active: active (exited) since Sat 2019-02-23 00:01:27 CET; 9h ago
 Main PID: 277 (code=exited, status=0/SUCCESS)
  CGroup: /system.slice/openvpn.service

Feb 23 00:01:27 osmc systemd[1]: Starting OpenVPN service...
Feb 23 00:01:27 osmc systemd[1]: Started OpenVPN service.

Interestingly, when the VPN is killed
Code:

sudo systemctl status openvpn
* openvpn.service - OpenVPN service
  Loaded: loaded (/lib/systemd/system/openvpn.service; enabled; vendor
  Active: active (exited) since Sat 2019-02-23 00:01:27 CET; 9h ago
 Main PID: 277 (code=exited, status=0/SUCCESS)
  CGroup: /system.slice/openvpn.service

Feb 23 00:01:27 osmc systemd[1]: Starting OpenVPN service...
Feb 23 00:01:27 osmc systemd[1]: Started OpenVPN service.

Is there a way to check the host. I used to run with debian and had this command to check
Code:

curl ipconfig.me/host
.... but it does not seem to work anymore. Any idea how to check this with systemd ? I could not find anything online. Thank you.

Just let me know what you would like to see (and how to proceed...my knowledge is limited as I said). Thanks in advance.

jsbjsb001 02-23-2019 02:42 AM

You need to make sure the openvpn service is stopped, if the actual service is still running, it won't matter what you have in your service file.

ondoho 02-23-2019 02:47 AM

Quote:

Originally Posted by NikkyG (Post 5965830)
Interestingly, when the VPN is killed...
Code:

sudo systemctl status openvpn
* openvpn.service - OpenVPN service
  Loaded: loaded (/lib/systemd/system/openvpn.service; enabled; vendor
  Active: active (exited) since Sat 2019-02-23 00:01:27 CET; 9h ago
 Main PID: 277 (code=exited, status=0/SUCCESS)
  CGroup: /system.slice/openvpn.service

Feb 23 00:01:27 osmc systemd[1]: Starting OpenVPN service...
Feb 23 00:01:27 osmc systemd[1]: Started OpenVPN service.


well, you didn't stop the service, you just killed openvpn, that's not the same...

and pease don't paraphrase, show us each command and the output it generated.
i am particularly unsure about "I reloaded TEST and restarted it manually".

and i think you should look at a unit's staus everytime you change something.

btw, arch wiki has very good systemd documentation. recommend, even if you use another distro.

all that said, not everything needs to be accomplished by systemd alone, as i already pointed out.

NikkyG 02-23-2019 11:10 AM

Alright. I need then some further insight: what's the difference between "killing" and "stopping" a service ?

When I "kill" the VPN, the service seems to remain ACTIVE while the IP address is the one without VPN (90.x.x.x).
When I "stop" the VPN, the service seems to be INACTIVE while the IP address is the one of the VPN (185.x.x.x) !!! (therefore, to me it looked like the VPN is still running)

Here is the status after "stopping" the service with the following command:
Code:

sudo systemcl stop openvpn
Code:

sudo systemctl status openvpn
* openvpn.service - OpenVPN service
  Loaded: loaded (/lib/systemd/system/openvpn.service; enabled; vendor preset:
  Active: inactive (dead) since Sat 2019-02-23 17:55:23 CET; 1min 23s ago
 Main PID: 277 (code=exited, status=0/SUCCESS)

Feb 23 00:01:27 osmc systemd[1]: Starting OpenVPN service...
Feb 23 00:01:27 osmc systemd[1]: Started OpenVPN service.
Feb 23 17:55:23 osmc systemd[1]: Stopped OpenVPN service.

The TEST service is stopped !! which is what I'm looking for, so I guess the dependency is working.

But then, when I want to restart my VPN with
Code:

sudo  systemcl start openvpn
followed by
Code:

sudo openvpn --config /etc/openvpn/VPN1.conf
, the VPN IP address remains unchanged, the same as when the VPN is running (185.x.x.x) and the VPN tries to start over an over...



Code:

Sat Feb 23 18:02:08 2019 Initialization Sequence Completed
Sat Feb 23 18:02:12 2019 Connection reset, restarting [0]
Sat Feb 23 18:02:12 2019 /sbin/ip addr del dev tun0 local 10.8.0.234 peer 10.8.0.233
Sat Feb 23 18:02:12 2019 SIGUSR1[soft,connection-reset] received, process restarting
Sat Feb 23 18:02:17 2019 TCP/UDP: Preserving recently used remote address: [AF_INET]185.212.226.142:443
Sat Feb 23 18:02:17 2019 Attempting to establish TCP connection with [AF_INET]185.212.226.142:443 [nonblock]
Sat Feb 23 18:02:18 2019 TCP connection established with [AF_INET]185.212.226.142:443
Sat Feb 23 18:02:18 2019 TCP_CLIENT link local: (not bound)
Sat Feb 23 18:02:18 2019 TCP_CLIENT link remote: [AF_INET]185.212.226.142:443
Sat Feb 23 18:02:21 2019 [server] Peer Connection Initiated with [AF_INET]185.212.226.142:443
Sat Feb 23 18:02:22 2019 TUN/TAP device tun0 opened
Sat Feb 23 18:02:22 2019 do_ifconfig, tt->did_ifconfig_ipv6_setup=0
Sat Feb 23 18:02:22 2019 /sbin/ip link set dev tun0 up mtu 1500
Sat Feb 23 18:02:22 2019 /sbin/ip addr add dev tun0 local 10.8.0.234 peer 10.8.0.233
Sat Feb 23 18:02:22 2019 Initialization Sequence Completed
Sat Feb 23 18:02:26 2019 Connection reset, restarting [0]
...


jsbjsb001 02-23-2019 11:35 AM

You "kill" a process, you "stop" a service. Your service stopped because you actually stopped the openvpn service. Which was why it wasn't working for you before, because you had not stopped the openvpn service.

A running process is when you start an app, it may only have one process, it may have several - it depends on the app. So if you started Firefox, you would see "firefox" in your list of running processes. The operating system manages running processes.

A service is something that runs in the background, and is managed by an init system (like systemd, but could be another init system depending on which Linux distribution it is you are talking about).

NikkyG 02-23-2019 11:40 AM

Thank you. That's clearer but I still have multiple confusions/questions.

1- How can OPENVPN service be stopped but the VPN still running ? - I would have still expected that the process would be killed at the same time.

2- Actually, that makes me think that what I 'm trying to get to is a dependency with the VPN connection and not with the OPENVPN service. Because I want to stop TEST from running whenever the VPN connection is lost. It might never be the case that the OPENVPN service will be down, while it will surely happen that the VPN connection will be lost. Any idea how to do this ?

3- Any idea about why I cannot restart my VPN connection manually ?

Thank you.

ondoho 02-23-2019 01:24 PM

you need to read a good systemd tutorial or documentation.
i recommend https://wiki.archlinux.org/index.php/Systemd


All times are GMT -5. The time now is 05:54 PM.