I am running Debian 7 Wheezy and I need to start some screens on startup as soon as there is a fully functional internet connection. However, not, if the internet connection broke and was connected again. So only on the first functional internet connection after boot.
Could you please post a dummy script for this and tell me where to put it and make it be executed under the given conditions?
The script only needs to start the screen and then terminate but the screen should continue.
EDIT
I have already heard of the /etc/network/if-up.d/ folder. But how can I make sure that the script is not executed again if the internet connection is lost and then re-established?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Put your script in /etc/network/if-up.d and make it executable. It will be automatically run each time a network interface comes up.
To make it do work only the first time it is run on every boot, have it check for existence of a flag file which you create after the first time. Example:
#!/bin/sh
FLAGFILE=/var/run/work-was-already-done
case "$IFACE" in
lo)
# The loopback interface does not count.
# only run when some other interface comes up
exit 0
;;
*)
;;
esac
if [ -e $FLAGFILE ]; then
exit 0
else
touch $FLAGFILE
fi
: here, do the real work.
Method 2
This is a job very well suited for systemd.
Running a script as a systemd service
If your system is running systemd, then you can configure your script to run as a systemd service which provides control over the lifecycle and execution environment, as well as preconditions for starting the script, such as the network being up and running, restarting on failure, etc.
The recommended folder for your own services is /etc/systemd/system/ (another option is /lib/systemd/system but that should normally be used for OOTB services only).
Create the file e.g. with sudo vim /etc/systemd/system/autossh.service :
[Unit]
Description=Autossh keepalive daemon
## make sure we only start the service after network is up
Wants=network-online.target
After=network.target
[Service]
## here we can set custom environment variables
Environment=AUTOSSH_GATETIME=0
Environment=AUTOSSH_PORT=0
# By default type 'simple' is used, see also https://www.freedesktop.org/software/systemd/man/systemd.service.html#Type=
# Type=simple|exec|forking|oneshot|dbus|notify|idle
ExecStart=/usr/local/bin/ssh-keep-alive.sh
ExecStop=pkill -9 autossh
# don't use 'nobody' if your script needs to access user files
# (if User is not set the service will run as root)
#User=nobody
# Useful during debugging; remove it once the service is working
StandardOutput=console
[Install]
WantedBy=multi-user.target
Now you can test the service:
sudo systemctl start autossh
Checking the status of the service:
systemctl status autossh
Stopping the service:
sudo systemctl stop autossh
Once you verified that the service works as expected enable it with:
sudo systemctl enable autossh
NOTE: For security purposes
systemdwill run the script in a restricted environment, similar to howcrontabscripts are run, therefore don’t make any assumptions about pre-existing system variables such as $PATH or whatever you have in your/.bashrc,~/.zshrc, etc. Use theEnvironmentkeys if your script needs specific variables to be defined. Addingset -xat the top of your bash script and then runningsystemctl status my_servicemight help identify why your script is failing. As a rule of tumb, always use absolute paths for everything includingecho, or explicitly define your $PATH by addingEnvironment=MYVAR=abc.
Method 3
The internet connection is brought up by an entry in /etc/rc6.d/ probably S35networking. If you change that file and insert your commands at the end, or better add a /etc/init.d/mystuff and link /etc/rc0.d/S36mystuff to it and insert your commands there, then that will start as soon as the network is up.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0