dmwynne
asked on
Simple bash script to see status and restart a service using if then
I am wondering if someone could post an example of an interactive bash script that prompts the user with a menu with 2 choices:
1. Do you want to see service status
2. Do you want to stop, start, restart service
If 1. show status, if 2 go to another menu. The 2nd menu would give options for either stopping, starting, or restarting the service.
I know there are many ways to do this but I am trying to understand if this can be done using just if / then statements and if you could post the code it would help me understand as I am just starting to learn about bash scripts.
1. Do you want to see service status
2. Do you want to stop, start, restart service
If 1. show status, if 2 go to another menu. The 2nd menu would give options for either stopping, starting, or restarting the service.
I know there are many ways to do this but I am trying to understand if this can be done using just if / then statements and if you could post the code it would help me understand as I am just starting to learn about bash scripts.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So I ran the script and selected option 2 and it gave me "other sub menu should appear to give you the options:" line. Shouldn't it display option 2. Do you want to stop...
Also how do I get the command to run when they select option 1. So the user picks 1 to see service status, how do I actually put the command service sshs status or something in the script?
Also how do I get the command to run when they select option 1. So the user picks 1 to see service status, how do I actually put the command service sshs status or something in the script?
The script was an example and not a functional one.
The detail on what to do when an option is selected requires a process. i.e. if the service is running, prompt to stop/restart
if the service is stopped, option to start
this is why the selection of option 2 would in general require a sub-menu display
The current status is X
what do you want to do:
1. st
etc.
For whom are you creating this script/process? Usually service management can only be done by those who have elevated or root rights.
The detail on what to do when an option is selected requires a process. i.e. if the service is running, prompt to stop/restart
if the service is stopped, option to start
this is why the selection of option 2 would in general require a sub-menu display
The current status is X
what do you want to do:
1. st
etc.
For whom are you creating this script/process? Usually service management can only be done by those who have elevated or root rights.
ASKER
Arnold - thank you. This is a script for me to use on a Centos box I admin. This was really an exercise for me to learn something that I was having trouble grasping, being new to scipting. I know Linux well enought to get a box up and running and to perform a function like monitor with nagios or backup machines but want to expand my knowledge to scripting. Thanks for your help.
duncan_roe - thanks. I really appreciate you providing the detailed answer you did. I am going to split points because you both helped me very much.
Thanks
duncan_roe - thanks. I really appreciate you providing the detailed answer you did. I am going to split points because you both helped me very much.
Thanks
Look at any if the startup scripts in /etc/init.d
There are many learn bash scripting sites/examples.
There are many learn bash scripting sites/examples.
By the way, here's some output from my previous post
Open in new window