0

I want to setup a script which will connect to a linux server and then stop/start a service. But between the stop and start of the service, it should wait ~10 seconds and check if the service is really stopped.

service httpd stop
--wait 10 seconds. check ps -ef | grep httpd, kill if any hanged processes
service httpd start

Please can someone help me on how to do it?

rjdkolb
  • 10,377
  • 11
  • 69
  • 89
Sai Avinash
  • 287
  • 4
  • 7
  • 17
  • Use `sleep` and a `kill -9` instead of explicit checking. – arkascha Jun 15 '17 at 19:12
  • can i connect to the server using the username and password? I am checking in the forms but most of them say create an ssh key. I will deploy this shell script on the same server i want to run the commands – Sai Avinash Jun 15 '17 at 19:17
  • You certainly can configure the ssh server to accept a password. But that is not recommended, since it is much more insecure than a key. And you can only do that in an interactive manner, so _not_ with a script. At least you should not. – arkascha Jun 15 '17 at 19:18
  • i am trying sleep but i am getting this error sleep: invalid time interval `10\r', can you tell me how to fix this. this is what i added in the script : sleep 10 – Sai Avinash Jun 15 '17 at 19:32
  • You need change the line break coding in your script. Use Unix-/Linux line breaks in your editor, not MS-Windows style. – arkascha Jun 15 '17 at 19:33
  • please another question, how do i check if there are hanged process and kill them? if i use kill -9, i am getting error that the command is not complete – Sai Avinash Jun 15 '17 at 19:39
  • 1
    You obviously need to specify _what_ process to kill. Read the man pages of the commands you try to use, if you are unsure about their usage. For example `man kill`. In your case `killall -9 /usr/sbin/httpd` might be a good alternative, since it does not require a specific process id. – arkascha Jun 15 '17 at 19:41
  • You should ask a specific question for a particular problem. Since Stack Overflow hides the Close reason from you: *"Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it."* – jww Jun 16 '17 at 00:14
  • You might want to [Take the Tour](https://stackoverflow.com/tour) to learn how to use Stack Overflow effectively. In the meantime, maybe [Rent A Coder](http://www.rentacoder.com) or [Freelancer](http://www.freelancer.com) can help you out. – jww Jun 16 '17 at 00:14

1 Answers1

1

The wait command is for a different purpose, we need to use the sleep command instead.

Check this link for detailed differences. https://stackoverflow.com/a/13296927/3086531


Script to check if process is running or not

#!/bin/bash
UP=$(pgrep httpd | wc -l);
if [ "$UP" -ne 1 ];
then
        echo "httpd is down.";
else
        echo "All is well.";
fi

Your final code would be

#!/bin/bash

service httpd stop 

sleep 10 

UP=$(pgrep httpd | wc -l);
if [ "$UP" -ne 1 ];
then
  service httpd start  #start serivce again, since no process are found 
else
  echo "Service is not stopped yet.";
  killall -15 httpd  #Kills all processes related to httpd
  service httpd start #start httpd process
fi
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
bhar1red
  • 440
  • 3
  • 10
  • There is no need for this complexity. Why bother to check if there still is a process, you are going to hard kill it anyway. Just fire a `killall -9` without checking and you know all is fine. – arkascha Jun 15 '17 at 19:42
  • @arkascha, processes should be stopped gracefully. We should allow them to perform clean-up routines (such as removing lock files, flushing in-memory data to hard drives etc.) before terminating. – Ruslan Osmanov Jun 15 '17 at 19:45
  • Then do a `killall -15` (or simply `killall`) instead of a `killall -9`. That was not my point. – arkascha Jun 15 '17 at 19:46
  • @arkascha, what's wrong with sending signal 15 (SIGTERM)? It is meant to be handled, while SIGKILL can not be handled by a process. – Ruslan Osmanov Jun 15 '17 at 19:48
  • How is `wait` deprecated? Its purpose is different from `sleep`. – Benjamin W. Jun 15 '17 at 19:48
  • @BenjaminW. I apologize, My bad. Corrected it now. – bhar1red Jun 15 '17 at 19:51
  • I already confirmed that SIGTERM is fine and maybe better here. As said: that is not the point I made. I pointed out that it is not necessary to first check if a process still exists. `killall` which just terminate silently if that is _not_ the case. So you can blindly use it. – arkascha Jun 15 '17 at 19:58
  • @arkascha I agree with what you say, `killall` can terminate process safely. However, my answer is inspired from this https://unix.stackexchange.com/a/28265 which basically states, it's safe to kill daemon applications using `service stop`. Not sure about actual reason or technicalities behind it. – bhar1red Jun 15 '17 at 20:05
  • Actually thinking about this brings me to state that `killall -15` does _not_ make sense. The `stop` command already does that. The OP wants to kill the process in case it hangs. So we are back at `killall -9` which should _not_ find any processes left, but guarantees a clean situation for the `start` command. – arkascha Jun 15 '17 at 20:07
  • @bhar1red thank you. Can i also do this procedure with other services? Like i have a service called appmanager. service appmanager start and service appmanager stop. I would like to do this same thing with that also. appmanager is a java app that is in /home/mydir/appmanager.jar – Sai Avinash Jun 15 '17 at 23:20
  • 1
    @SaiAvinash In order to run java apps as service, you need to follow this procedure here https://stackoverflow.com/a/11203626/3086531 or here is a simple solution for what you wanted to achieve https://stackoverflow.com/a/11203634/3086531 – bhar1red Jun 16 '17 at 01:32
  • @SaiAvinash In order to run java apps as service, you need to follow this procedure here https://stackoverflow.com/a/11203626/3086531 or here is a simple solution for what you wanted to achieve https://stackoverflow.com/a/11203634/3086531 – bhar1red Jun 16 '17 at 01:33