Skip to content

How to make my PC execute a script after it established a connection to the internet?

My particular case is to connect to a ProtonVPN. ProtonVPN ships a command line tool (protonvpn-cli from the AUR) to do this.

At the moment I just start a little script a few seconds after login to KDE. It works fine most of the time, but it is not very elegant nor practical especially on a laptop.

I am sure it is explained somewhere in the wiki, but I have not found it so far and I don't know how to search for it.

Even a slight hint or a link to the arch wiki would most likely help me out.

Edited 21 Jul 2021. We only hold this version.

3 points12 comments
Open on Reddit
About this post
Posted
Archived
Edited
Score
3 · 81% up
Kind
Text
ID
ool1o6

Author

4
posts
45
comments
0
media
View profile

12 comments

Just use a systemd service. Something like

[Unit]
Description=Your description
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
ExecStart=/path/to/your/script/
# or
ExecStart=/bin/sh -c 'exec your-command'

[Install]
WantedBy=multi-user.target

Note that you'll need to create another unit if you want your first unit to be automatically restarted after resuming from suspend/hibernate, see my comment here

6 points

Well ofc one can do this with systemd. I should have thought about it! Thx for this suggestion!

1 points

Also look at this to go down the Network Manager route rather than systemd.

https://wiki.archlinux.org/title/NetworkManager#Network_services_with_NetworkManager_dispatcher

3 points

One side note. There is a way to connect to Proton via openVPN. If you do that, there is a checkbox in Network Manager gui to auto connect to a VPN for certain networks.

2 points

Good hint!

I was thinking about using openVPN. But Proton advices to use their tools. And I thought it is easier to setup and to maintain. You don't have to update any openVPN config files when proton changes anything.

Are you using protonVPN? Do you connect to it via openVPN?

1 points

Yes, I think this is exactly what I was looking for! This seems as simple as it should be. Thx!

1 points
3 points

Yeah I looked at that wiki page, but there wasn't anything about auto starting after establishing network connection.

1 points

I don't know your particular problem but on XFCE I start several services that need network via autostart desktop entry without worrying about whether the network is there or not. Of course, if the network is not there the service that needs it won't work, but this doesn't stop everything else...

1 points

Keyboard shortcuts

Move and act

jork
Next / previous post or comment
Enteroro
Open the post
f
Open the media viewer
s
Save
h
Hide (the toast has Undo)
Tab
Right after an action: the toast's Undo
x
Collapse the comment
v
Next layout: cards, compact, grid

Go to

/
Search
gthenh
Home
gthena
All
gthens
Subreddits
gthenl
Library
?
This list
Esc
Close a menu or this list

In the viewer

←or→
Previous / next file
↑or↓
Previous / next post
korj
Previous / next post
Space
Play / pause
m
Mute
z
Zoom
s
Save
o
Open the post (on its own page: back to it)
Esc
Close

Keys do nothing while you type in a box.