Hi,
I just started working on a script. After my research, i found a command which can help me:
AIM: To build a script which starts the services (Services 1) on server 1 automatically whenever its down. And it has a dependency on other service (Service 2) on Server 2.
So my script has to check if service 2 is up and running on other machine and only then has to start Service 1. I cannot use any passwords in the script or do not any a ID with no password required ability to login to other machine using ssh and check the status of the service 2.
So my IDEA is, Service 2 always uses particular port X...so my script which stays on server 1 has to just check if port X is in use, if so just go ahead and start service 1. If not wait for sometime till service 2 is up and then start service 1.
Please help...
I think i can use IF statement and then put nc command and based on its put start service 1. Any thought/help will be really appreciated.
You could do something like (Illustration is for service 2 listening on port 9999):
if nc -w 2 server2 9999 < /dev/null > /dev/null 2>&1
then
# Start Service 1
else
# Service 2 is not running on server2
fi
I have another question about this.....can i use any command or another script which can wait and check for port 9999 on server 2 and whenever it founds that a service is started on this port...it can kick off my script on server 1....
Really appreciate your help!! i am making progress... :)
Assumption here is that whenever port goes from unserviced to serviced that your script should be run. This script should be started in the background with nohup (perhaps from /etc/init.d):
while true
do
if nc -w 2 server2 9999 < /dev/null > /dev/null 2>&1
then
# Service is currently running
[ "$PREV" = "NOT" ] && /path/to/your/script
PREV="RUNNING"
else
# Service 2 is not running on server2
PREV="NOT"
fi
done
1 Like
@Chubler....
I got the script working but i have one last thing...
i am trying to look for a particular word in a logfile while its getting update and when it finds it i want it to run second script...can you help?
tail -F /path to log file| grep "Started in" | while read; do sh /path to script2.sh start ; done
Above command is not working for some reason...any thoughts??
also to run the script in background...but when i use nohup myscript.sh .... i doesnt run in background but stays there untill i hit control c.... am i missing something here?
I will post the script i have soon so it helps other....Thanks again for your help!!