Useful systemd service examples

119 words, est. 1 min

Recurring one shot job

This is a quick and dirty systemd service template for a recurring one shot job. If it sounds like a cron job it’s because it is. You get logging for free, though.

Service

/etc/systemd/system/job.service

[Unit]
Description=Script for X running as user Simon
After=network.target

[Service]
ExecStart=echo 1                                                                                                                                      
User=simon
Group=simon
Type=oneshot

[Install]
WantedBy=default.target

Timer

/etc/systemd/system/job.timer

[Unit]
Description=Echo every Monday to Friday 

[Timer]
OnCalendar=daily
OnCalendar=Mon..Fri 09:30
Persistent=true

[Install]
WantedBy=timers.target

Long running service

This is a template for a long running service, like a web server for example. Naturally, no timer is needed for this type of service.

Service

/etc/systemd/system/webserver.service

[Unit]
Description=Script for X
After=network.target

[Service]
WorkingDirectory=/home/simon/
ExecStart=webserver
Type=forking
PIDFile=/tmp/webserver.pid

[Install]
WantedBy=default.target

Arch Wiki page on the subject https://wiki.archlinux.org/title/Systemd.

This post was first published at