Debian - how to run a script on startup as soon as there is an internet connection ready for use?
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.
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.
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]
# By default 'simple' is used, see also https://www.freedesktop.org/software/systemd/man/systemd.service.html#Type=
# Type=simple|forking|oneshot|dbus|notify|idle
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
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
systemd
will run the script in a restricted environment, similar to howcrontab
scripts 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 theEnvironment
keys if your script needs specific variables to be defined. Addingset -x
at the top of your bash script and then runningsystemctl status my_service
might 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
.
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.