Shell script that can check the status of service, if online then only it should run
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Shell script that can check the status of service, if online then only it should run
I am new in this filed and have very less knowledge. Could someone please help me making a Shell script that can check the status of service, if the service is online then only it should run and should wait till the service status "Online". Thanks in advance.
will explain the scenario(if any suggestion in below scenario , u r most welcome:-
1. Want my script to run at last after every service has been started.
2. for that will call my script from local.rc.
3. So the script for which i needed your help will be checking one of the service from init.d, and iff the service status is online then only it should run , otherwise it should wait for it get online and then it should run.
Last edited by manish007raut@gmail.com; 12-09-2015 at 04:53 AM.
Reason: New here, got some good suggestions about how to ask questions. :)
Making a Shell script that can check the status of service, if online then only it should run and should wait till the service status "Online".
Hello Manish, since you introduced yourself as "Working as an IT Engineer" you should be able to design the functionality, create the script yourself and post it here for comments. So by all means have a go at it. No need to ask for handouts (or ask questions that don't start with "Could you please help" and don't end with a question mark).
Depends on the distro, too. SystemV- (or is it Upstart- ?) based distros use the service command to check the status, systemd-based ones use the systemctl command, and there are others which have their own way of doing this.
To get you going:
Code:
if ! service httpd status >/dev/null 2>&1
then service httpd start
fi
Making a Shell script that can check the status of service, if online then only it should run and should wait till the service status "Online". Thanks in advance.
Great...so since you're making it, why don't you share it with us? Or are you having a problem?? If so, we can (and will) only help you if you post what YOU have written/tried on your own. We ARE NOT going to write shell scripts for you...especially since you're an "IT Engineer". Read the "Question Guidelines" link in my posting signature.
I am new in this filed and have very less knowledge. Could someone please help me making a Shell script that can check the status of service, if the service is online then only it should run and should wait till the service status "Online". Thanks in advance.
will explain the scenario(if any suggestion in below scenario , u r most welcome:-
1. Want my script to run at last after every service has been started.
2. for that will call my script from local.rc.
3. So the script for which i needed your help will be checking one of the service from init.d, and iff the service status is online then only it should run , otherwise it should wait for it get online and then it should run.
...which is frowned upon, since editing your posts makes things harder to follow.
Quote:
Originally Posted by manish007raut@gmail.com
I am new in this filed and have very less knowledge. Could someone please help me making a Shell script that can check the status of service, if the service is online then only it should run and should wait till the service status "Online". Thanks in advance.
will explain the scenario(if any suggestion in below scenario , u r most welcome:-
1. Want my script to run at last after every service has been started.
2. for that will call my script from local.rc.
3. So the script for which i needed your help will be checking one of the service from init.d, and iff the service status is online then only it should run , otherwise it should wait for it get online and then it should run.
And again, we all understand what you WANT...what you haven't shown us is ANY effort on your part to actually DO IT. Again, WE WILL NOT write scripts for you, period. We will gladly help you, but you have to post what you have done.
There are tens of thousands of very easily-found scripting tutorials (with examples), you can find with a brief Google search. Unless you actually start doing work, you'll always be 'new with very less knowledge'. Start doing your own work...post what you do/try, and tell us where you're stuck, and you can get help. Otherwise, just re-posting the same question asking for a handout still won't get you anything.
I am new in this filed and have very less knowledge.
Have you read any simple BASH shell tutorials? (Hint: search LQ.)
Quote:
Originally Posted by manish007raut@gmail.com
Could someone please help me making a Shell script that can check the status of service, if the service is online then only it should run and should wait till the service status "Online".
- What specific service are you talking about?
- What is the complete and proper command to check if the service is available?
- What is the full output of the previous command?
- What is the full output of the previous command if it is offline or errors out for other reasons?
- Actually i am working on bpm services(i have one product installed on my machine)
-I will be writing a script that will be called from local.rc, so first i have to check weather the service is online or not, for that will use command "/etc/init.d/bpm-server status" and after that "echo $?". if the output of echo $? = 1 , then the service is online, otherwise offline(2 for offline).
- So its not end here, i have to wait for the service to get online, so will have to use sleep, so sleep till echo $? = 1 . and when it satisfies this condition then only proceed for further script.
- so please suggest me how it can be done!
Thanks in advance.
- Actually i am working on bpm services(i have one product installed on my machine)
-I will be writing a script that will be called from local.rc, so first i have to check weather the service is online or not, for that will use command "/etc/init.d/bpm-server status" and after that "echo $?". if the output of echo $? = 1 , then the service is online, otherwise offline(2 for offline).
- So its not end here, i have to wait for the service to get online, so will have to use sleep, so sleep till echo $? = 1 . and when it satisfies this condition then only proceed for further script.
- so please suggest me how it can be done!
You just SAID how it can be done. You stated the steps; AGAIN, YOU need to show us what YOU have written/tried on your own. We STILL will not write this for you, especially with the thousands upon thousands of bash-scripting tutorials you can easily find with a brief search...including one in my own posting signature. Since you say YOU will be writing this script, then show us what you have accomplished, and tell us where you're stuck.
Otherwise, see ANY of the many easily-found examples of rc scripts you can find with a brief Google search, and use them to get you going.
/etc/init.d/bpm-server status
if [ "$?" == 1 ]; then
files=`find / -type f -perm 0777`
for f in $files
do
echo "file :"$f
chmod o-w $f
done
else
sleep 10m
fi
-I want this script should check seamlessly until the $? = 1, and after that only run the for loop.
files=`find / -type f -perm 0777`
for f in $files
do
echo "file :"$f
chmod o-w $f
done
I am missing any relation to your requirement.
Here is something that fits your requirement
Code:
# run a service check; if the exit status is not 0 (0=success) then ...
/etc/init.d/bpm-server status
if [ $? -ne 0 ]
then
# start the service
...
# after a sleep check the service again
...
fi
Do the experienced people here always talk to everyone like this?
Where's the friendliest Linux Newbie site with the most handholding.
I never saw a rule saying Can you please Help is disallowed, or question marks.
Sure there are hundreds of tutes on the web, but I've followed some random advice in the last week that turned out to be a bad way to do somethings, so it might be more helpful if you actually point us towards the trusted sources, rather than......
I don't want to put it into words but I have some pretty strong feelings about the way people are talking to each other in here
Am sure that feelings are not encouraged but sorry, we are all human.
Do the experienced people here always talk to everyone like this?
Where's the friendliest Linux Newbie site with the most handholding. I never saw a rule saying Can you please Help is disallowed, or question marks.
Read the "Question Guidelines" link in my posting signature, as the OP here was directed to. Hand-holding is fine, and as said, we are ALWAYS happy to help, but asking for a HANDOUT (that is, "I need a script to do xxx"), isn't. Being helpful and friendly is one thing...doing someones job/homework FOR THEM is another.
To put it another way, suppose someone asked you to help them move...then they sat in a chair on the lawn, and watched you load the truck? That's not HELP; help is the person asking doing SOMETHING too.
Quote:
Sure there are hundreds of tutes on the web, but I've followed some random advice in the last week that turned out to be a bad way to do somethings, so it might be more helpful if you actually point us towards the trusted sources, rather than......
...and the 'trusted sources' would be what??? Are you saying that folks here should do Google searches and try EVERYTHING for people who ask, to save them the trouble of actually doing it themselves? And those tutorials work for SOME people, not others. What you have described is a process called 'learning'...trying things, and figuring out what works and what doesn't.
Quote:
I don't want to put it into words but I have some pretty strong feelings about the way people are talking to each other in here Am sure that feelings are not encouraged but sorry, we are all human.
Yes, we are all human...mistakes are part of life. However, showing zero effort of your own is NOT 'being human'...it's being lazy.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.