6

How do I automatically execute a script after an internet connection is successfully made.

I've tried placing the script in if-up.d and it just does not work and I can't find any documentation on how to do this. The script works if I manually execute.

Can anyone please assist?

mek zek
  • 61
  • 1
  • 2

1 Answers1

8

Stephen Boston mentioned you could use a systemd service to automate it.

Create a service file under /etc/systemd/system and name it FILENAME.service

Paste this into your file:

[Unit]
Description=YOUR DESCRIPTION
Requires=network-online.target 
After=network-online.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/YOUR/PATH/TO/SCRIPT

[Install]
WantedBy=multi-user.target

Type=oneshot means it will only be executed once.

The line requires=network-online.target states that the service needs the service network to be online and only executes after=network-online.target

You can see the service (test.service) started after the network.service

You can see the service (test.service) started after the network.service

To finish it up you need to enable the service with the command

sudo systemctl enable FILENAME.service

Check if your Service is enabled

sudo systemctl is-enabled FILENAME.service

You can ofcourse manually start or stop the service with

sudo systemctl start/stop/restart/status FILENAME.service

TIP: Pic was created with systemd-analyze plot > output.svg

thelaakes
  • 101
  • 5