Linux Service Restart Script

Updated on 13th Feb 2025

Hi! I wanted to a Linux service restart script. What is a Linux service restart script? Well, it’s a bash script that makes it easier for me to restart common programs. You see, I was forever getting confused on the two different ways to restart a service.

The Linux in my example is on my Raspberry Pi, which was also running Plex.

You can either use:

sudo service SERVICE_NAME restart

Or, you can use:

sudo systemctl restart SERVICE_NAME

But both of these are slightly different, with the service name either being before or after the restart flag. You might be able to see this more clearly from the screenshot below:

Linux Service Restart Script - plex server status examples

So I created a bash script (.sh) file that would allow me to chose which service to restart without having to remember the different commands. Below are my requirements:

  • Give a list of services
  • read which service to restart
  • Restart service
  • restart script

Linux Service Restart Script

You can see my script below:

#!/bin/bash

restart_script () {
 bash /home/pi/restartservices
}


NUMBER=1
echo "$(tput setaf 3)Following services available:"
echo ""

#LIST SERVICES

for SERVICE in "VSFTPD" "Plex" "VNC Server" "LightDM"
 do
 echo "$(tput setaf 3)($NUMBER)$(tput setaf 7)$SERVICE"
 NUMBER=$[$NUMBER +1]
done

echo ""
echo "$(tput setaf 3)(E)xit:"

read -p "$(tput setaf 3)Chose a service to restart:" READINPUT

if [ $READINPUT = "1" ]
 then
 echo "$(tput setaf 3)Restarting VSFTPD Service...."
 sudo service vsftpd restart
 echo "$(tput setaf 2)Success!"
 restart_script
elif [ $READINPUT = "2" ]
 then
 echo "$(tput setaf 3)Restarting Plex Service...."
 sudo service plexmediaserver restart
 echo "$(tput setaf 2)Success!"
 restart_script
elif [ $READINPUT = "3" ]
 then
 echo "$(tput setaf 3)Restarting VNC Server...."
 sudo service vncserver restart
 echo "$(tput setaf 2)Success!"
 restart_script
elif [ $READINPUT = "4" ]
 then
 echo "$(tput setaf 3)Restarting LightDM Service...."
 sudo systemctl restart lightdm
 echo "$(tput setaf 2)Success!"
 restart_script
elif [ $READINPUT = "E" ] || [ $READINPUT = "e" ]
 then
 echo "$(tput setaf 3)Exiting Script"
 exit 1
else
 echo "$(tput setaf 1)INVALID RESPONSE - WILL LOOP UNTIL RESPONSE IS VALID!"
 restart_script
fi

Rather than having to type in the name of the service I wanted to restart, I used a for loop to add a number to each entry in the services list. This can be found from line 14 to line 19.

If you wanted to restart a VNC server on a Raspberry Pi for example, you could use:

sudo service vncserver restart

Then to run my script from a terminal session, I simply type “./restartservices”

Enjoy!

1 thought on “Linux Service Restart Script”

  1. Pingback: Coloured BASH Output Using TPUT – I.T Delinquent

Leave a Comment

Your email address will not be published. Required fields are marked *

email popup image
Mark Harwood
NEVER miss a blog post again! Subscribe for email notifications whenever a new post is live!
Subscribe
NEVER miss a blog post again! Subscribe for email notifications whenever a new post is live!
No obligation call, message, or email
Contact form image
I'll Be In Touch Soon