Skip to content

Instantly share code, notes, and snippets.

@Yejneshwar
Last active January 24, 2024 14:42
Show Gist options
  • Save Yejneshwar/cdc3f512cf12db75ea813dcae27ff3a4 to your computer and use it in GitHub Desktop.
Save Yejneshwar/cdc3f512cf12db75ea813dcae27ff3a4 to your computer and use it in GitHub Desktop.
Create new systemd service
#!/bin/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 [ -z "$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
Requires=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
[Service]
Type=simple
Requires=network.target
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
@pansila
Copy link

pansila commented Jun 23, 2023

Great script

@pansila
Copy link

pansila commented Jun 23, 2023

As '-E' only set 'enable' to 'true' without OPTARG, it should have no semi after opt 'E'? Also Requires=network.target is deprecated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment