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:

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.
How to Upgrade MySQL Community to MySQL Enterprise on Red Hat 9
Been a while since I made a post, so let’s jump straight back in with…
How to Setup WordPress Backups (FREE!)
Personally, I like my website. A lot. And I wouldn’t want one mistake to make…
Install NGINX on an Offline RHEL System – 2 Systems Required!
Been a while since my previous post, but let’s hope the magic hasn’t rubbed off…
MySQL 8.4 Replication with SSL on Ubuntu 22.02
So I recently wrote a post and made a YouTube video on setting up MySQL…
Gutenberg Just Broke my Website
More of a story and vent than an informative article. So to save you reading…
How to Design a Website in 5 Steps
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!
Pingback: Coloured BASH Output Using TPUT – I.T Delinquent