this post was submitted on 17 May 2025
8 points (83.3% liked)

Arch Linux

8753 readers
7 users here now

The beloved lightweight distro

founded 5 years ago
MODERATORS
 

I try to download a file with curl directly after logging into Gnome. This usually fails because mostly the WiFi connection is only established after login. If I wait long enough in the login screen before logging in, the connection is already established and the download works.

So far I have used Manjaro. WiFi was already active long before the login screen of GDM was even displayed. The download therefore always worked.

The download is initiated with a *.desktop file in ~/.config/autostart.

Is there another way to start the download after the user is logged in and the WiFi connection is really ready?

top 10 comments
sorted by: hot top controversial new old
[–] jrgd@lemm.ee 19 points 4 weeks ago (1 children)

Create a systemd user unit that waits for the network-online.target.

A script something like:

[Unit]
Description=Startup script
Requires=network-online.target
After=network-online.target

[Service]
Type=oneshot # either simple or oneshot, but sounds like oneshot
ExecStart=/home/<user>/script.sh
RemainAfterExit=yes #if oneshot, otherwise no

[install]
WantedBy=default.target

Edit the template according to your needs and dump it into ~/.local/share/systemd/user/<unit>.service and enable it with systemctl --user enable --now <unit>

[–] MoLoPoLY@discuss.tchncs.de 3 points 3 weeks ago (1 children)

What's the difference between oneshot and simple?

[–] jrgd@lemm.ee 8 points 3 weeks ago (1 children)

Oneshot services are for things like scripts that do a thing and exit. Simple is for basic services that intend to run for the lifetime of the system (or for user units, the lifetime of the user's session).

[–] MoLoPoLY@discuss.tchncs.de 2 points 3 weeks ago

Ahh ok. Since in this case the script should only run at start and then exit, I use oneshot. Many thx.

[–] MummifiedClient5000 0 points 3 weeks ago

The simplest solution is probably a script that waits and retries until curl succeeds.

[–] just_another_person@lemmy.world 0 points 4 weeks ago

You can have your WiFi start with the rest of your network service before login if you choose. Maybe that's the limitation here.

[–] harsh3466@lemmy.ml -1 points 4 weeks ago (1 children)

Easiest way would be to just add a sleep 15 command at the top of the script. Time how long it takes your wifi to come up, and adjust the sleep time with like a 2-3 second buffer in case it takes linger for some reason.

More exact would be to create a systemd service for your script that depends on network connectivity to execute.

[–] MoLoPoLY@discuss.tchncs.de -1 points 3 weeks ago

Yep, I know. But when I only depend on

After=network-online.target
Wants=network-online.target

this doesn't mean, I have a working internet connection. That's why the loop.

Or is that wrong?

[–] MoLoPoLY@discuss.tchncs.de -1 points 4 weeks ago (1 children)

Nice. I will try the systemd unit. Meanwhile, i have added a loop in the script, which helps a little bit:

maxwait=15
if [[ $(nm-online -x) != *"online"* ]]; then
	echo "missing internet connection, waiting..."
	for (( i = 1; i <= maxwait; ++i )); do
		sleep 1 &
		echo $i
		if [[ $(nm-online -x) == *"online"* ]]; then
			break
		fi

		if [[ $i -eq $maxwait ]]; then
			echo "No internet connection"
			exit 1
		fi
		wait
	done 
fi

In my test, this exits the script after 15 seconds without connection, but continues, when the connection is available or is established within this time.

But i think, its a nice idea, to add this to the script AND use the systemd unit together.

[–] F04118F@feddit.nl 4 points 3 weeks ago

With these kinds of things, where you need to manage state (waiting, executing, failed, etc), it is very easy to miss a case or transition and generally better to rely on proven tech.

Let the waiting for network connection and retrying be done by systemd, half the internet runs on it. You can trust that it won't mess that part up. Write only what is specific to you in your script.