Created
January 24, 2024 14:38
-
-
Save pansila/85fd33b98f996cde0046eb16fa4460ce to your computer and use it in GitHub Desktop.
helper script to create a simple systemd service
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 | |
# Check for help flag | |
if [ "$1" == "-h" ] || [ "$1" == "--help" ] | |
then | |
echo "Create a systemd service file" | |
echo "Usage: ./create-service.sh -s <service name> -c <command> -w <working directory> -E <run service at startup>" | |
exit 0 | |
fi | |
# Create new systemd service file | |
# Get service name from flag | |
while getopts s:c:w:E flag; do | |
case "${flag}" in | |
s) service=${OPTARG};; | |
c) command=${OPTARG};; | |
w) workingdir=${OPTARG};; | |
E) enable=true;; | |
esac | |
done | |
# Check if service name is set | |
if [ -z "$service" ]; then | |
echo "Service name is not set" | |
exit 1 | |
fi | |
# Check if command is set | |
if [ -z "$command" ]; then | |
echo "Command is not set" | |
exit 1 | |
fi | |
# check if working directory is set | |
if [ -n "$workingdir" ]; then | |
# Check if working directory exists | |
if [ ! -d "$workingdir" ]; then | |
echo "Working directory does not exist" | |
exit 1 | |
fi | |
fi | |
# check if command can be run | |
if ! command -v $command &> /dev/null; then | |
echo "Command cannot be run : $command" | |
exit 1 | |
fi | |
# stop command if running | |
if pgrep -x "$command" > /dev/null; then | |
echo "Stopping $command" | |
pkill $command | |
fi | |
# if $service does not contain .service then add it | |
if [[ ! $service == *.service ]]; then | |
service="$service.service" | |
fi | |
# Check if service file already exists | |
if [ -f "/etc/systemd/system/$service" ]; then | |
echo "Service file already exists : /etc/systemd/system/$service" | |
# Ask if user wants to overwrite | |
read -p "Do you want to overwrite the service file? (y/n) " -n 1 -r | |
echo | |
if [[ ! $REPLY =~ ^[Yy]$ ]]; then | |
exit 1 | |
fi | |
# Remove service file | |
rm /etc/systemd/system/$service | |
fi | |
# Print Details | |
echo "Service name: $service" | |
echo "Command: $command" | |
echo "Working directory: $workingdir" | |
# Create service file - if working directory is not set then don't include it | |
if [ -z "$workingdir" ]; then | |
echo "[Unit] | |
Description=$service | |
After=network.target | |
[Service] | |
Type=simple | |
ExecStart=$command | |
Restart=always | |
RestartSec=10 | |
[Install] | |
WantedBy=multi-user.target" > /etc/systemd/system/$service | |
else | |
echo "[Unit] | |
Description=$service | |
After=network.target | |
[Service] | |
Type=simple | |
ExecStart=$command | |
WorkingDirectory=$workingdir | |
Restart=always | |
RestartSec=10 | |
[Install] | |
WantedBy=multi-user.target" > /etc/systemd/system/$service | |
fi | |
# Reload systemd | |
echo "Reloading systemd" | |
systemctl daemon-reload | |
# Enable service | |
if [ "$enable" = true ]; then | |
echo "Enabling service" | |
systemctl enable $service | |
fi | |
# Start service | |
echo "Starting service" | |
systemctl start $service | |
# Check status | |
echo "Checking status..." | |
systemctl status $service |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inspired by this but it has some errors and author is not active.