You are not logged in.

#1 2022-08-12 18:26:42

IrreleventIdiot
Member
From: SC
Registered: 2017-01-28
Posts: 35

[Solved] systemd User Unit - only run on active internet?

I'm trying to mount a Cloud synced (onedriver) gocryptfs folder. I have already written a user .service file that does the job, and enabled it.

The thing is, it's a bit messy. Currently I run a ExecStartPre sleep command for 10 seconds to delay the mounting for long enough to allow the network to connect, and for onedriver to initialize and mount my OneDrive content, before I actually perform the gocryptfs command.

I've been trying recently to figure out how to get the service to only run once an internet connection of some sort is active, in my case it's WiFi.

However, it seems that there is absolutely no way with user units to check on or reference system targets such as network-online.target. I tried linking those related .service files to use but it didn't work (Dependancy Failed errors).

Ideally I would like to not only have my service run only when starting up and internet is available, I'd also like to have the service dynamically start whenever I connect to the internet so I automatically have my encrypted folder available for use.

Any ideas on how to accomplish this? I've tried multiple different things already but none have worked. I've seen a couple of mentions of using "sockets" to communicate network connectivity to my user units but that's honestly outside of my knowledge scope at the moment. If that's the only way to do it that's fine, but I might need some guidance (read step-by-step instructions) on how to accomplish this.

Thanks.

Last edited by IrreleventIdiot (2022-08-24 20:28:28)


Yes, I know I spelt "Irrelevant" wrong.

Offline

#2 2022-08-12 19:02:41

seth
Member
Registered: 2012-09-03
Posts: 51,227

Re: [Solved] systemd User Unit - only run on active internet?

it seems that there is absolutely no way with user units to check on or reference system targets such as network-online.target.

Hummm?

https://wiki.archlinux.org/title/System … work_is_up
For NM  also see https://wiki.archlinux.org/title/Networ … ait-online to make it behave not retarded useless.

I'd also like to have the service dynamically start whenever I connect to the internet

This vastly depends on the network daemon you use, see eg. https://wiki.archlinux.org/title/Networ … dispatcher

Offline

#3 2022-08-12 19:50:28

IrreleventIdiot
Member
From: SC
Registered: 2017-01-28
Posts: 35

Re: [Solved] systemd User Unit - only run on active internet?

The "Running service after network is up" section references service files located in `/etc/systemd/system`, which contains system services, which can reference targets like network-online.target.

I'm writing a service located in `~/.config/systemd/user/` which I run using `systemctl --user` commands, which run in a seperate process that doesn't have paths configured to access system service directories. I tried using `systemctl --user link /path/to/system/service/file` to create links for both `NetworkManager.service` and `NetworkManager-wait-online.service` to my `~/.config/systemd/user/` directory, enabling them, and also making `After=` and `Require=` statements in my gocryptfs service file referencing those two service files, but that didn't work.

So, as far as I can tell, the first link you referenced will not work in my situtation.

I might try writing in that `nm-online` command into my unit file to force my service file to wait for a network connection, rather than waiting for 10 seconds...

thanks for you time, and sorry I didn't make it more clear of my situation.


Yes, I know I spelt "Irrelevant" wrong.

Offline

#4 2022-08-12 19:58:18

IrreleventIdiot
Member
From: SC
Registered: 2017-01-28
Posts: 35

Re: [Solved] systemd User Unit - only run on active internet?

update: nm-online doesn't solve the issue, my gocryptfs mount fails because the network location isn't available. the /bin/sleep 10 solution so far is the only one that works.


Yes, I know I spelt "Irrelevant" wrong.

Offline

#5 2022-08-12 20:17:52

seth
Member
Registered: 2012-09-03
Posts: 51,227

Re: [Solved] systemd User Unit - only run on active internet?

update: nm-online doesn't solve the issue, my gocryptfs mount fails because the network location isn't available.

https://wiki.archlinux.org/title/System … _variables has a user service "After=network.target" post your service and its status (in particular if you're depending it on "After=network-online.target", but also the version w/ "/usr/bin/nm-online -q")

Also you should check whether "/usr/bin/nm-online -q" (eg. in an interacive shell) actually does what you want it to do.

Offline

#6 2022-08-12 20:38:33

IrreleventIdiot
Member
From: SC
Registered: 2017-01-28
Posts: 35

Re: [Solved] systemd User Unit - only run on active internet?

Tried that just now, doesn't work.

FYI I'm rebooting every time I make a change right now to test the effects of a cold-boot, logging into my desktop to see if my service successfully mounts my encrypted folder.

I first tried adding the `After=network.target` line, to no effect (still fails). Then I tried again, also adding in `Requires=network.target`, and that's where things get interesting. With just `After=` nothing happens except failure, but with `Requires=` I get a `network.target dependancy not found` error, which shows that I cannot reference system targets with my user unit service--unless there's something else I'm missing.


Yes, I know I spelt "Irrelevant" wrong.

Offline

#7 2022-08-12 20:41:44

IrreleventIdiot
Member
From: SC
Registered: 2017-01-28
Posts: 35

Re: [Solved] systemd User Unit - only run on active internet?

terribly sorry, here's the output of `systemctl --user status obsidian_crypt_mount.service`:

obsidian_crypt_mount.service - Auto-Mount Encrypted Obsidian Vault from Onedrive
     Loaded: loaded (/home/greg/.config/systemd/user/obsidian_crypt_mount.service; enabled; preset: enabled)
     Active: inactive (dead) since Fri 2022-08-12 16:32:40 EDT; 7min ago
    Process: 1325 ExecStart=gocryptfs --extpass=secret-tool lookup secret password Sync/OneDrive/Documents/Obsidian/Crypt Sync/Vault (code=exited, status=6)
        CPU: 6ms

Aug 12 16:32:40 orga systemd[1308]: Starting Auto-Mount Encrypted Obsidian Vault from Onedrive...
Aug 12 16:32:40 orga systemd[1308]: obsidian_crypt_mount.service: Control process exited, code=exited, status=6/NOTCONFIGURED
Aug 12 16:32:40 orga systemd[1308]: obsidian_crypt_mount.service: Failed with result 'exit-code'.
Aug 12 16:32:40 orga systemd[1308]: Failed to start Auto-Mount Encrypted Obsidian Vault from Onedrive.

Last edited by IrreleventIdiot (2022-08-12 20:43:02)


Yes, I know I spelt "Irrelevant" wrong.

Offline

#8 2022-08-12 20:53:55

seth
Member
Registered: 2012-09-03
Posts: 51,227

Re: [Solved] systemd User Unit - only run on active internet?

Since you're using NM, how about just skipping systemd and using a  dispatcher script, also because

seth wrote:

I'd also like to have the service dynamically start whenever I connect to the internet

This vastly depends on the network daemon you use, see eg. https://wiki.archlinux.org/title/Networ … dispatcher

Offline

#9 2022-08-12 21:00:24

IrreleventIdiot
Member
From: SC
Registered: 2017-01-28
Posts: 35

Re: [Solved] systemd User Unit - only run on active internet?

Oh shoots, I missed that, I apologize! I will try it later.


Yes, I know I spelt "Irrelevant" wrong.

Offline

#10 2022-08-13 12:55:09

IrreleventIdiot
Member
From: SC
Registered: 2017-01-28
Posts: 35

Re: [Solved] systemd User Unit - only run on active internet?

So, I tried placing a simple script that invokes an su command as my user account when the networks comes "up", but I'm now at a loss since it always exits with a status of "1". Not sure what I'm doing wrong. I set it to be executable and it was created with root.

if [ "$2" = "up" ]; then
    su user -c "systemctl --user start crypt_folder_mount.service"
fi

Yes, I know I spelt "Irrelevant" wrong.

Offline

#11 2022-08-13 20:01:10

seth
Member
Registered: 2012-09-03
Posts: 51,227

Re: [Solved] systemd User Unit - only run on active internet?

Missing user bus, the "su" environment lacks (at least) $DBUS_SESSION_BUS_ADDRESS to talk to the relevant systemd user bus.

Offline

#12 2022-08-14 01:47:34

IrreleventIdiot
Member
From: SC
Registered: 2017-01-28
Posts: 35

Re: [Solved] systemd User Unit - only run on active internet?

I'm afraid I'm not knowledgeable enough with dbus to know what I'd need to do.

Thankfully, if I have to I can revert to my original unit script, and then just run it manually if I'm not home.

Thank you for your time, but I don't mean to pester you further if the issue is going to prove a burden on the both of us to conjure a solution.


Yes, I know I spelt "Irrelevant" wrong.

Offline

#13 2022-08-14 06:06:23

seth
Member
Registered: 2012-09-03
Posts: 51,227

Re: [Solved] systemd User Unit - only run on active internet?

This has nothing to do w/ dbus specifically, systemctl --user needs an environment that su won't provide.
In your active session, run "echo $DBUS_SESSION_BUS_ADDRESS", it'll print sth. like "unix:path=/run/user/1000/bus"
You can then eg.

su user -c "env DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus systemctl --user start crypt_folder_mount.service"

Offline

#14 2022-08-15 01:09:11

IrreleventIdiot
Member
From: SC
Registered: 2017-01-28
Posts: 35

Re: [Solved] systemd User Unit - only run on active internet?

Oh, ok. (I should still learn about dbus at some point.)

I'll give it a try and see what happens.


Yes, I know I spelt "Irrelevant" wrong.

Offline

#15 2022-08-15 02:52:30

jonno2002
Member
Registered: 2016-11-21
Posts: 684

Re: [Solved] systemd User Unit - only run on active internet?

refer to my post here, specifically the 2nd part

Offline

#16 2022-08-15 05:54:30

seth
Member
Registered: 2012-09-03
Posts: 51,227

Re: [Solved] systemd User Unit - only run on active internet?

@jonno2002, that's where he started

The OP wrote:

The thing is, it's a bit messy. Currently I run a ExecStartPre sleep command for 10 seconds to delay the mounting for long enough to allow the network to connect…

I've been trying recently to figure out how to get the service to only run once an internet connection of some sort is active, in my case it's WiFi.

However, it seems that there is absolutely no way with user units to check on or reference system targets such as network-online.target. I tried linking those related .service files to use but it didn't work (Dependancy Failed errors).

Ideally I would like to not only have my service run only when starting up and internet is available, I'd also like to have the service dynamically start whenever I connect to the internet so I automatically have my encrypted folder available for use.

Offline

#17 2022-08-15 07:36:46

jonno2002
Member
Registered: 2016-11-21
Posts: 684

Re: [Solved] systemd User Unit - only run on active internet?

sorry i meant to just use the script in the 2nd part of my post to replace the "sleep 10" and actually wait for internet access before running the mount command.

Offline

#18 2022-08-15 14:00:00

IrreleventIdiot
Member
From: SC
Registered: 2017-01-28
Posts: 35

Re: [Solved] systemd User Unit - only run on active internet?

thanks jonno, that looks like an improvement over my "sleep 10" setup.


Yes, I know I spelt "Irrelevant" wrong.

Offline

#19 2022-08-15 20:55:38

IrreleventIdiot
Member
From: SC
Registered: 2017-01-28
Posts: 35

Re: [Solved] systemd User Unit - only run on active internet?

I'm starting to wonder if the issue isn't with anything I'm trying to do, but rather some quirk with onedriver.

I tried jonno's script, which tests for a valid internet connection. I also tried testing first if the directory I wanted to decrypt was NOT empty before I mounted. Neither of which resolve the issue. Only the forced wait with sleep works.

So, could it be that it's just that onedriver takes a little while longer to fully initialize before I can do anything?


Yes, I know I spelt "Irrelevant" wrong.

Offline

#20 2022-08-15 21:12:19

IrreleventIdiot
Member
From: SC
Registered: 2017-01-28
Posts: 35

Re: [Solved] systemd User Unit - only run on active internet?

Aha! I might've solved this one bit. I instead tried trying to read a file from the synced location first before trying for a mount operation. Now it works.

So that would seem to confirm my suspicion that it was onedriver that was "ready" but not "fully ready" when I tried to mount the folder. I had `requires=` and `after=` in place in my service file that referenced the onedriver service file. But I guess that wasn't enough.


Yes, I know I spelt "Irrelevant" wrong.

Offline

#21 2022-08-24 20:29:03

IrreleventIdiot
Member
From: SC
Registered: 2017-01-28
Posts: 35

Re: [Solved] systemd User Unit - only run on active internet?

Marking this as solved, since the "check for file readability" seems to work persistently and satisfactorily. Thank ya'll again for all the help and guidance.


Yes, I know I spelt "Irrelevant" wrong.

Offline

Board footer

Powered by FluxBB