Sani Evans
Sani Evans

Reputation: 81

Start script once connected to network

I have a bot for the chat we use stored on a workstation that reboots every night. Right now it reboots and runs the script for the bot while starting up without anyone having to log in. Usually it will be connected to the network by that time, but if it's taking longer to connect to the network, the script will quit with an error.

I've tried adding it to the rc.local file and I've also tried putting a custom made script in the init.d directory and running update-rc.d script.sh defaults.

What file would I edit that would run my script once the computer is connected to the network without anyone needing to be logged in?

I've tried looking around and saw a lot of examples for things that work once you log in, and I also saw this answer that looks like it might be the right track?

--update--

I've been reading into /etc/network/if-up.d/ and see if a script is placed in there and marked as executable without the .sh extension, it should run. The Debian Docs here and here explain:

The ifupdown system automatically runs scripts installed in-
"/etc/network/*/" while exporting environment variables to scripts.

and

The ifupdown-extra package provides easy network connection tests for use 
with the ifupdown package.

  -The network-test(1) command can be used from the shell.

  -The automatic scripts are run for each ifup command execution.

The network-test command frees you from the execution of cumbersome low 
level commands to analyze the network problem.

The automatic scripts are installed in "/etc/network/*/" and perform the following:

  -Check the network cable connection

  -Check duplicate use of IP address

  -Setup system's static routes based on the "/etc/network/routes" definition

  -Check if network gateway is reachable

  -Record results in the "/var/log/syslog" file

Does this process occur on startup? And if a script is placed here, will it run only once a connection is established without someone needing to login? If my script runs before a connection's established, it'll fail due to no response from the host and will need to be run manually once someone comes in.

--update--

The script isn't being executed when the computer reboots and is connected to a network, I've tried looking into why and read this answer here and changed the value in NetworkManager.conf from

[ifupdown]
managed=false

to

[ifupdown]
managed=true

What am I missing? I know the script isn't being called because it should be outputting into a log but the log never appears.

Upvotes: 2

Views: 1613

Answers (2)

Sani Evans
Sani Evans

Reputation: 81

Up to this point, everything was done correctly. I read this answer and ran

sudo ifup --all -v

and it had shown I had a syntax error in my script. After fixing that, it worked as intended.

Upvotes: 1

Bango
Bango

Reputation: 1001

I believe you can use system.d for that task

[Unit]
Description=My Test Service
After=network.target
...

https://www.digitalocean.com/community/tutorials/understanding-systemd-units-and-unit-files

Upvotes: 0

Related Questions