The syntax to run a systemd timer every ten minutes is:
OnCalendar=*-*-* *:00,10,20,30,40,50:00
i.e. run it on the first second (:00
) of every 10th minute (00,10,20,30,40,50
).
The syntax to run a systemd timer every ten minutes is:
OnCalendar=*-*-* *:00,10,20,30,40,50:00
i.e. run it on the first second (:00
) of every 10th minute (00,10,20,30,40,50
).
I use this in git repositories to ease the deployment process
#!/bin/bash # This script installs and enables/starts a systemd service # It also installs the service file export NAME=MyService cat >/etc/systemd/system/${NAME}.service <<EOF # TODO Copy & paste the systemd .service file here EOF # Enable and start service systemctl enable --now ${NAME}.service
The following example automatically installs a docker-compose
systemd .service:
#!/bin/bash # This script installs and enables/starts a systemd service # It also installs the service file export NAME=MyService cat >/etc/systemd/system/${NAME}.service <<EOF [Unit] Description=${NAME} Requires=docker.service After=docker.service [Service] Restart=always User=root Group=docker WorkingDirectory=$(pwd) # Shutdown container (if running) before unit is being started ExecStartPre=$(which docker-compose) -f docker-compose.yml down # Start container when unit is started ExecStart=$(which docker-compose) -f docker-compose.yml up # Stop container when unit is stopped ExecStop=$(which docker-compose) -f docker-compose.yml down [Install] WantedBy=multi-user.target EOF # Enable and start service systemctl enable --now ${NAME}.service
sudo systemctl restart "gitlab-*"
will restart all systemd services starting with gitlab-
. The "
are only neccessary so your shell doesn’t try to interpret the wildcard.
When installing wireguard-tools
on Linux, it includes a script called reresolve-dns.sh
. This will take care of automatically re-resolving.
According to its documentation, you should run it every 30 seconds or so.
So we can just create a systemd timer to run it every 30 seconds.
Use our script
wget -qO- https://techoverflow.net/scripts/install-wireguard-reresolve-dns.sh | sudo bash /dev/stdin
Now you need to enable it for each relevant interface separately, for example for wg0
:
systemctl enable --now [email protected]
Do manually what our script does.
Create /etc/systemd/system/[email protected]
:
[Unit] [email protected] [Service] Type=oneshot ExecStart=/usr/share/doc/wireguard-tools/examples/reresolve-dns/reresolve-dns.sh %i
Create /etc/systemd/system/[email protected]
:
[Unit] [email protected] timer [Timer] [email protected]%i.service OnCalendar=*-*-* *:*:00,30 Persistent=true [Install] WantedBy=timers.target
Now you need to enable it for each relevant interface separately, for example for wg0
:
systemctl enable --now [email protected]
The syntax to run a systemd timer every 30 seconds is:
OnCalendar=*-*-* *:*:00,30
i.e. run on the first (00
) and 30
th second of every minute.
If you’ve added a wg-quick
config, e.g. /etc/wireguard/wg0.conf
, you can enable autostarting it on system boot using systemd
:
sudo systemctl enable --now [email protected]
If you have started Wireguard with this config manually before, you need to shut it down first or systemd will not be able to start it !
The following script will install Jupyter Hub in single user mode (i.e. only a single Linux user can login to Jupyter Hub using the web interface).
First install Python & PIP, then NodeJS, then Jupyter Hub, then configurable-http-proxy
:
curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash - sudo apt-get install -y nodejs python3-pip sudo pip3 install jupyterhub sudo npm i -g configurable-http-proxy
Run the following script using sudo
!
#!/bin/bash # This script installs and enables/starts the JupyterHub systemd service export NAME=JupyterHub # Create service file cat >/etc/systemd/system/${NAME}.service <<EOF [Unit] Description=${NAME} [Service] Type=simple ExecStart=/usr/bin/env jupyterhub --port=11569 User=root Group=root Restart=on-failure RestartSec=5s [Install] WantedBy=multi-user.target EOF # Enable and start service systemctl enable --now ${NAME}
This script will install a systemd service named JupyterHub
autostart it on boot. It is running on port 11569
by default.
Note: This will only allow a single (preconfigured) user to login to Jupyter lab! See How to run Jupyter Hub (multi-user mode) using systemd on how to deploy Jupyter Hub in multi-user mode using systemd, which allows any Unix user to login!
The following script will install Jupyter Hub in single user mode (i.e. only a single Linux user can login to Jupyter Hub using the web interface).
First install Python & PIP, then NodeJS, then Jupyter Hub, then configurable-http-proxy
:
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash - sudo apt-get install -y nodejs python3-pip sudo pip3 install jupyterhub sudo npm i -g configurable-http-proxy
Run the following script as the user you want to be able to login! Do not run the script using sudo
!
#!/bin/bash # This script installs and enables/starts a systemd service export NAME=JupyterHub-$USER export GROUP=$(id -gn $USER) # Create service file sudo tee /etc/systemd/system/${NAME}.service <<EOF [Unit] Description=${NAME} [Service] Type=simple ExecStart=/usr/bin/env jupyterhub WorkingDirectory=$HOME User=$USER Group=$GROUP Restart=on-failure RestartSec=5s [Install] WantedBy=multi-user.target EOF # Enable and start service systemctl enable --now ${NAME}
This script will install a systemd service named JupyterHub-$USER
(where $USER
is the current user, e.g. uli
) and autostart it on boot.
If you run multiple services, you can run the script for each user and choose a unique port for each service by adding --port=7219
to the /usr/bin/env jupyter hub
command, e.g.
ExecStart=/usr/bin/env jupyterhub --port=7192
Alternatively, you can run a single systemwide Jupyter Hub in multi-user mode where multiple users can log in.
If you want to run your Jupyter Lab as a network service on any modern Linux distribution, you can install a systemd
service that runs Jupyter. First, you need to install jupyter lab
using
sudo pip3 install jupyterlab
In case you don’t have pip3
, use sudo apt -y install python3-pip
or the equivalent on your distribution.
Note that this script will run Jupyter without token authentication and without password and it will listen on any IP (--ip=0.0.0.0
) by default. Either change the command line flags or be aware of the security implications !
#!/bin/bash # This script installs and enables/starts a systemd service export NAME=Jupyter # Create service file cat >/etc/systemd/system/${NAME}.service <<EOF [Unit] Description=${NAME} [Service] Type=simple ExecStart=/usr/bin/env jupyter lab --ip=0.0.0.0 --port 17256 --LabApp.token='' WorkingDirectory=/home/uli/jupyter User=uli Group=uli Restart=on-failure RestartSec=5s [Install] WantedBy=multi-user.target EOF # Enable and start service systemctl enable --now ${NAME}
You need to change the following entries in the script in order to make it work for you:
WorkingDirectory=/home/uli/jupyter User=uli Group=uli
Set the WorkingDirectory
to whatever directory you want Jupyter to run in. Note that anyone being able to access the webinterface will basically have full access to that directory!
Set User
and Group
to the user that should run. Note that running Jupyter as root is not allowed. In case you still want to do it, add the --allow-root
flag to the command line options.
Now run the script as root
to install the service:
sudo ./install-jupyter-service.sh
Now you can access Jupyter at http://[ip of the computer]:17256
.
In order to change the configuration, I recommend to edit /etc/systemd/systemd/Jupyter.service
(or /etc/systemd/systemd/${NAME}.service
if you changed export NAME=Jupyter
) directly. After that, run
sudo systemctl daemon-reload sudo systemctl restart Jupyter
You can also change the installation script and re-run it, but you still need to run daemon-reload
and restart
.
In order to run multiple instances, just run multiple copies of the installation script with different names. For example, use
export NAME=Jupyter-DeepLearning
If you have problems with Juypter Lab starting up, use
sudo systemctl status Jupyter
in order to view the status and
sudo journalctl -xfu Jupyter
to view all the logs.
Status output example:
● Jupyter.service - Jupyter Loaded: loaded (/etc/systemd/system/Jupyter.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2021-06-11 03:44:28 CEST; 4s ago Main PID: 48753 (jupyter-lab) Tasks: 1 (limit: 14226) Memory: 51.7M CGroup: /system.slice/Jupyter.service └─48753 /usr/bin/python3 /usr/local/bin/jupyter-lab --ip=0.0.0.0 --port 17256 --LabApp.token= Jun 11 03:44:29 uli-desktop env[48753]: [I 2021-06-11 03:44:29.215 ServerApp] nbclassic | extension was successfully loaded. Jun 11 03:44:29 uli-desktop env[48753]: [I 2021-06-11 03:44:29.216 LabApp] JupyterLab extension loaded from /usr/local/lib/python3.8/dist-packages/jupyterlab Jun 11 03:44:29 uli-desktop env[48753]: [I 2021-06-11 03:44:29.216 LabApp] JupyterLab application directory is /usr/local/share/jupyter/lab Jun 11 03:44:29 uli-desktop env[48753]: [I 2021-06-11 03:44:29.219 ServerApp] jupyterlab | extension was successfully loaded. Jun 11 03:44:29 uli-desktop env[48753]: [I 2021-06-11 03:44:29.220 ServerApp] Serving notebooks from local directory: /dev/shm Jun 11 03:44:29 uli-desktop env[48753]: [I 2021-06-11 03:44:29.220 ServerApp] Jupyter Server 1.8.0 is running at: Jun 11 03:44:29 uli-desktop env[48753]: [I 2021-06-11 03:44:29.220 ServerApp] http://uli-desktop:17256/lab Jun 11 03:44:29 uli-desktop env[48753]: [I 2021-06-11 03:44:29.220 ServerApp] http://127.0.0.1:17256/lab Jun 11 03:44:29 uli-desktop env[48753]: [I 2021-06-11 03:44:29.220 ServerApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). Jun 11 03:44:29 uli-desktop env[48753]: [W 2021-06-11 03:44:29.224 ServerApp] No web browser found: could not locate runnable browser.
In order to just stop and disable autostart (but not uninstall) the jupyter lab service, use
sudo systemctl disable --now Jupyter
After that, you can just remove the service file in order to permanently uninstall the service:
sudo rm /etc/systemd/system/Jupyter.service
You can always reinstall using our installation script.
Note that if you have changed the export NAME=...
line, you need to replace Jupyter
by the value of Name
The following script will install a systemd .timer
file and the associated .service
file into /etc/systemd/system/
and enable the timer (i.e. start on boot) and start the timer immediately.
#!/bin/bash # This script installs and enables/starts a systemd timer # It also installs the service file that is run by the given timer export NAME=MyService cat >/etc/systemd/system/${NAME}.service <<EOF # ADD SYSTEMD SERVICE FILE CONTENT HERE EOF cat >/etc/systemd/system/${NAME}.timer <<EOF # ADD SYSTEMD TIMER FILE CONTENT HERE EOF # Enable and start service systemctl enable --now ${NAME}.timer
In order to modify the script for your systemd service, replace MyService
by the desired name of your service in
export NAME=MyService
insert the content of your .service
file at
# ADD SYSTEMD SERVICE FILE CONTENT HERE
and insert the content of your .timer
file at
# ADD SYSTEMD TIMER FILE CONTENT HERE
The following complete example installs a systemd service named MyService
that runs /usr/bin/python3 myscript.py
every two hours:
#!/bin/bash # This script installs and enables/starts a systemd timer # It also installs the service file that is run by the given timer export NAME=MyService cat >/etc/systemd/system/${NAME}.service <<EOF [Unit] Description=${NAME} [Service] Type=oneshot ExecStart=/usr/bin/python3 myscript.py WorkingDirectory=/opt/myservice EOF cat >/etc/systemd/system/${NAME}.timer <<EOF [Unit] Description=${NAME} timer [Timer] OnCalendar=*-*-* 00,02,04,06,08,10,12,14,16,18,20,22:00:00 Persistent=true [Install] WantedBy=timers.target EOF # Enable and start service systemctl enable --now ${NAME}.timer
The syntax to run a systemd timer every minute is:
OnCalendar=*-*-* *:00,05,10,15,20,25,30,35,40,45,50,55:00
i.e. run it on the first second (:00
) of every 5th minute (00,05,10,15,20,25,30,35,40,45,50,55
).
The syntax to run a systemd timer every three hours is:
OnCalendar=*-*-* 00,03,06,09,12,15,18,21:00:00
i.e. run it on the first minute and first second (00:00
) of every second hour (00,03,06,09,12,15,18,21
).
The syntax to run a systemd timer every two hours is:
OnCalendar=*-*-* 00,02,04,06,08,10,12,14,16,18,20,22:00:00
i.e. run it on the first minute and first second (00:00
) of every second hour (00,02,04,06,08,10,12,14,16,18,20,22
).
The syntax to run a systemd timer every minute is:
OnCalendar=*-*-* *:*:00
i.e. run it on the first second (00
) every minute.
The following script will install a systemd service file into /etc/systemd/system/
and enable it (i.e. start on boot) and start it immediately.
#!/bin/bash # This script installs and enables/starts a systemd service export NAME=MyService # Create service file cat >/etc/systemd/system/${NAME}.service <<EOF # ADD SYSTEMD SERVICE FILE CONTENT HERE EOF # Enable and start service systemctl enable --now ${NAME}
In order to modify the script for your systemd service, replace MyService
by the desired name of your service in
export NAME=MyService
and insert the content of your .service
file at
# ADD SYSTEMD SERVICE FILE CONTENT HERE
The following complete example installs a systemd service named MyService
that runs /usr/bin/python3 myscript.py
:
#!/bin/bash # This script installs and enables/starts a systemd service export NAME=MyService # Create service file cat >/etc/systemd/system/${NAME}.service <<EOF [Unit] Description=MyService [Service] Type=oneshot ExecStart=/usr/bin/python3 myscript.py WorkingDirectory=/opt/myservice [Install] WantedBy=multi-user.target EOF # Enable and start service systemctl enable --now ${NAME}