Autoinstall-Skript für systemd-Timer und zugehörigen Dienst
English
Deutsch
Das folgende Skript installiert eine systemd-.timer-Datei und die zugehörige .service-Datei in /etc/systemd/system/ und aktiviert den Timer (d.h. Start beim Booten) und startet den Timer sofort.
install_systemd_timer.sh
#!/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}.timerUm das Skript für den eigenen systemd-Dienst anzupassen, MyService durch den gewünschten Namen des Dienstes ersetzen in
install_systemd_timer_set_name.sh
export NAME=MyServiceden Inhalt der .service-Datei einfügen bei
install_systemd_timer_service_placeholder.sh
# ADD SYSTEMD SERVICE FILE CONTENT HEREund den Inhalt der .timer-Datei einfügen bei
install_systemd_timer_timer_placeholder.sh
# ADD SYSTEMD TIMER FILE CONTENT HEREVollständiges Beispiel
Das folgende vollständige Beispiel installiert einen systemd-Dienst namens MyService, der /usr/bin/python3 run.py alle zwei Stunden ausführt:
install_systemd_timer_example.sh
#!/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 run.py
WorkingDirectory=/opt/myproject
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}.timerCheck out similar posts by category:
Systemd
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow