4

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
  • 41
  • Possible duplicate: https://askubuntu.com/questions/258580/how-to-run-a-script-depending-on-internet-connection – thelaakes Oct 29 '19 at 08:26
  • Which Ubuntu version are you using? What do you want the script to do? – Stephen Boston Oct 29 '19 at 09:41
  • 1
    @thelaakes I've looked at that thread and tried the solution to the tee but it does not work. I am unsure if my interfaces file is correct. It only has auto lo iface lo inet loopback – mek zek Oct 29 '19 at 10:25
  • @StephenBoston Ubuntu 18.04. I want my PC to automatically VNC to another PC whenever there is a successful WIFI connection. I just cannot get this to work on Ubuntu. – mek zek Oct 29 '19 at 10:27
  • If I manually execute the script it works. It asks for my authentication and it VNC into my second PC. It is a simple script #!/bin/bash xtightvncviewer 192.168.0.10. I dont know if this script is executing at all or not when placed into /etc/network/if-up.d. Is there a way to check? – mek zek Oct 29 '19 at 10:31
  • You could add a log line to the script. With 18.04 the network system changed a bit so perhaps you'll have better luck with a systemd service. Take a look at https://stackoverflow.com/questions/35805354/systemd-start-service-at-boot-time-after-network-is-really-up-for-wol-purpose and https://bbs.archlinux.org/viewtopic.php?id=230785 – Stephen Boston Oct 29 '19 at 12:41
  • I might revert to an older version of Ubuntu and attempt this again. – mek zek Oct 30 '19 at 11:40
  • I've attempted this in 16.04 and i get the error in my syslog 'Error: Can't open display'. How do I open the display via my script – mek zek Oct 31 '19 at 09:47

1 Answers1

6

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