Skip to content

Instantly share code, notes, and snippets.

@danielkrizian
Last active December 20, 2021 12:15
Show Gist options
  • Save danielkrizian/c74062a151ae8a09a917d5391d9d8a86 to your computer and use it in GitHub Desktop.
Save danielkrizian/c74062a151ae8a09a917d5391d9d8a86 to your computer and use it in GitHub Desktop.
systemd cheatsheet (+ systemctl + journalctl)

hourly runs

https://linuxhint.com/cron_systemd_timer/

cat /home/dk/.config/systemd/user/gdrive.org.service
[Unit]
Description=Upload org files to my google drive
# after networking is up on the server
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/gdrive sync upload --keep-local --delete-extraneous /home/dk/org <dir.id>
[Install]
WantedBy=multi-user.target
cat /home/dk/.config/systemd/user/gdrive.org.timer
# https://linuxhint.com/cron_systemd_timer/
[Unit]
Description=Schedule google drive backups every hour

[Timer]
OnCalendar=hourly
# RandomizedDelaySec=7200

Persistent=true

[Install]
WantedBy=timers.target
sudo apt install golang-go
go get github.com/prasmussen/gdrive
sudo install ~/go/bin/gdrive /usr/local/bin/gdrive
gdrive about
# Authentication needed
# Go to the following url in your browser:
# Enter verification code: 

run via user and start on boot

https://askubuntu.com/questions/676007/how-do-i-make-my-systemd-service-run-via-specific-user-and-start-on-boot

loginctl enable-linger dk
systemctl --user daemon-reload
systemctl --user enable gdrive.org.service
systemctl --user enable --now gdrive.org.timer

systemctl

systemctl start  svc  # right now
systemctl stop   svc
systemctl enable svc  # e.g. on boot
systemctl disable svc
systemctl status svc
systemctl list-unit-files

investigate errors

https://community.chakralinux.org/t/how-to-investigate-systemd-errors/7024

systemctl --failed
journalctl _PID=<PID>                 # find by pid
journalctl -p err                     # find errors
journalctl -S today -f -u my.service  # follow/tail since today

keep program running (as a service)

cat /home/dk/.config/systemd/user/q.service
[Unit]
Description=Keep q program running
# after networking is up on the server
After=network.target
StartLimitIntervalSec=30
StartLimitBurst=20

[Service]
Type=simple
WorkingDirectory=/home/dk
Environment=QLIC=/home/dk/conda/q QHOME=/home/dk/conda/q
ExecStart=/home/dk/conda/bin/q -p 7042
TimeoutSec=30s
Restart=always
RestartSec=15s

[Install]
WantedBy=multi-user.target
systemctl start q.service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment