Last active
March 18, 2021 00:38
-
-
Save RichardBronosky/6c748146615140b1abc5489d43a4f75c to your computer and use it in GitHub Desktop.
Examples for creating Systemd service units.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -eu | |
# Made for EC2 Amazon Linux 2 "user data" script, but should work most places | |
# FROM: https://gist.github.com/RichardBronosky/6c748146615140b1abc5489d43a4f75c | |
main(){ | |
[[ $UID -eq 0 ]] && sudo="" || sudo="sudo" | |
install_docker | |
install_systemd_unit | |
[[ -n $sudo ]] && sleep 5 && $sudo systemctl status $service_name | |
} | |
install_docker(){ | |
$sudo yum update | |
$sudo yum install -y docker | |
$sudo systemctl enable docker | |
$sudo systemctl restart docker | |
} | |
install_systemd_unit(){ | |
cmd="$(which docker)" | |
image_name="nginx:alpine" | |
service_name="nginx.service" | |
service_desc="Nginx (in Docker) Service" | |
service_start_pre=( | |
"/bin/sh -c '" | |
"$cmd stop %n ||:;" | |
"$cmd rm %n ||:;" | |
"$cmd pull $image_name;" | |
"'" | |
) | |
service_start=("$cmd run --rm --name %n -p 80:80 $image_name") | |
service_stop=("$cmd kill --signal QUIT %n") | |
service_log="/var/log/$service_name.log" | |
service_unit="/lib/systemd/system/$service_name" | |
service_work_dir="/tmp" | |
service_start_with_logging=( | |
"/bin/sh -c '" | |
"{ echo; echo; date -u +%%FT%%TZ; echo; } >>$service_log;" | |
"exec ${service_start[*]} >>$service_log 2>&1;" | |
"'" | |
) | |
$sudo tee $service_unit >/dev/null <<EOF | |
[Unit] | |
Description=$service_desc | |
After=docker.service | |
Requires=docker.service | |
[Service] | |
WorkingDirectory=$service_work_dir | |
# Service Types: https://wiki.archlinux.org/index.php/systemd#Service_types | |
Type=simple | |
TimeoutSec=10 | |
Restart=always | |
ExecStartPre=${service_start_pre[*]} | |
ExecStart=${service_start_with_logging[*]} | |
ExecStop=-${service_stop[*]} | |
[Install] | |
WantedBy=multi-user.target | |
EOF | |
$sudo chmod 644 $service_unit | |
$sudo systemctl daemon-reload | |
$sudo systemctl enable $service_name | |
$sudo systemctl reset-failed $service_name | |
$sudo systemctl start $service_name | |
} | |
main | |
# vim: set et ts=4 sts=4 sw=4 : |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -eu | |
# A slightly Raspberry Pi specific Systemd service unit example. | |
# FROM: https://gist.github.com/RichardBronosky/6c748146615140b1abc5489d43a4f75c | |
service_name="sample.service" | |
service_desc="My Sample Service" | |
service_cmd="/usr/bin/python /home/pi/sample.py" | |
sudo tee /lib/systemd/system/$service_name >/dev/null <<EOF | |
[Unit] | |
Description=$service_desc | |
After=multi-user.target | |
[Service] | |
# To cd first: | |
#WorkingDirectory=/usr/local | |
# Service Types: https://wiki.archlinux.org/index.php/systemd#Service_types | |
Type=idle | |
ExecStart=$service_cmd | |
# Or, to save stdout and stderr to a log file: | |
#ExecStart=$service_cmd > /home/pi/$service_name.log 2>&1 | |
[Install] | |
WantedBy=multi-user.target | |
EOF | |
sudo chmod 644 /lib/systemd/system/$service_name | |
sudo systemctl daemon-reload | |
sudo systemctl enable $service_name | |
sudo reboot |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment