Command to detect if elastic search is active in linux - linux

I have to detect if elastic search is running on Linux. If it is not running than start it machine using shell script.
Used following code to detect if elastic search is running, but every time else condition is executed even if service is running:
service=elasticsearch
if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 ))
then
echo "$service is running!!!"
else
echo "$service is not running!!!"
fi

Here is a sample script and used in a shell script to check if my sevice is running:
Take a look at this script to see if it helps you.
#verify if the service is running
ps aux | grep SERVICENAME | pidof java > /dev/null
verifier=$?
zero=0
if [ $zero = $verifier ]
then
echo "Service was successfully started"
else
echo "Service stopped"
fi
In my case my service was done in java then I use the pidof to properly filter my service

The issue with checking for pid is that the service might have started, but it might not be ready actually. So instead, I check for port that ES is listening on. It works better for me.
netstat -uplnt | grep :9200 | grep LISTEN > /dev/null
verifier=$?
if [ 0 = $verifier ]
then
echo "ES is listening on port 9200"
else
echo "ES is not ready yet"
fi

Related

How can I check if apache2 service is running with a bash script?

I want to monitor apache service for ubuntu but below script is not working.
#!/bin/bash
if [ $(ps aux | grep apache2 | wc -l) -gt 1 ]; then
echo "Statistic:0"
else
echo "Statistic:1"
fi
exit 0
I edited your question to make it readable - but I think it is working as expected once the format is cleaned up. It looks like you are trying to find out if apache2 is running.
#!/bin/bash
if [ $(ps aux | grep apache2 | wc -l) -gt 1 ]; then
echo "Statistic:0"
else
echo "Statistic:1"
fi
exit 0
Depending on what "flavor" of linux you are running, the following might be a better option for you. You can also use is-enabled, is-failed, to check other status.
if systemctl is-active -q apache2; then
echo "Statistic:0"
else
echo "Statistic:1"
fi

Bash - use 1 stop script for multiple similar services, and kill the correct process only

I have multiple processes running as services on a machine
Before moving from 1 process/service to multiple ones, I used the following script to stop my service
#!/bin/sh
SIGNAL=${SIGNAL:-TERM}
PIDS=$(ps ax | grep -i 'datastream' | grep java | grep -v grep | awk '{print $1}')
if [ -z "$PIDS" ]; then
echo "No Brooklin server to stop"
exit 1
else
kill -s $SIGNAL $PIDS
fi
The issue now is that this script kills all processes of this type if invoked as a service stop command
My services are called for example service-A, service-B, service-C. If I send a service service-C stop command, the current script will stop all 3 processes.
I would like to make the script use the provided service name to determine which process to stop (I can grep A/B/C from the process output to ps, but I haven't managed to tell it how to stop only the process given in the service stop command.
Does anyone have experience handling something similar?
You can try something like below while starting your application which can store your PID in a static file and then you can use the same file to kill the process.
Pasting below one of my start - stop script which I have used in past for churning up multiple processes.
Start Script :-
#!/bin/bash
export PORT=$1
. /application/setEnv.sh
/java/jdk1.8.0_152/bin/java -Xms512m -Xmx2G -XX:+DisableExplicitGC -jar /application/api-1.0-0-all.jar </dev/null >>$LOGDIR/service$PORT.log 2>&1 &
echo $! > /application/service$PORT.pid
disown $!
Stop Script :-
#!/bin/bash
PORT=$1
PID=`cat /application/service$PORT.pid`
if [ ! -z "$PID" ]; then
rm /application/service$PORT.pid
kill -9 $PID >/dev/null 2>&1
if [ $? -gt 0 ]; then
echo "PID file found but no matching process was found. Stop aborted."
exit 1
fi
else
echo "PID file is empty and has been ignored."
fi
mv /application/logs/service$PORT.log /application/logs/service$PORT.log`date +%d%m%Y%H%M%S`
Only change which I can think of is the replace my port utilisation logic viz. $PORT with your service names viz. A/B/C.

How to get the status of any service in a sh script on centos 7?

I am trying to get the service status (for a /bin/sh script) and start it if is not running.
I found some scripts, but does not work for centos 7.
There are, as usual, multiples ways to do this. Just one example, that check if postfix is running:
#!/bin/sh
PID=`cat /var/spool/postfix/pid/master.pid`
# echo $PID
PS=`/bin/ps axu | grep $PID | grep -v grep`
# echo $PS
if [ "$PS" = "" ]
then
/sbin/service postfix restart
fi
You could use "pid" file, or try to detect if process is running (parsing output of "ps axu |grep process_name"), or parse output of "service process status" command, etc, etc.

Linux self-healing script to check some process

Im new with Linux scripts, I need help to create one script with check some installed processes on a server and if one of these services is not running restart it, and recheck again those services and if there any error print it with echo as below :
dsisrv (DSI service) (7384) Running
midaemon (measurement interface) (1412) Running
misrv (measurement interface service) (1384) Running
perfalarm (Alarm generator) Stopped
perfalarmsrv (Alarm generator service) Stopped
scopent (data collector) Stopped
scopesrv (collector service) Stopped
perfd (Real Time Metric Access Daemon) (7888) Running
perfdsrv (Real Time Metric Access Service) (9020) Running
ttd (transaction tracking) (1808) Running
in case any of above services is stopped, the script to run restart command.
Appreciate if any one help me to start with this script
Regards,
#!/bin/sh
SERVICE='httpd'
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
echo "$SERVICE service running, everything is fine"
else
echo "$SERVICE is not running" echo "$SERVICE is not running!" | mail -s "$SERVICE down" root
fi
Just add the service your looking for, this will mail you if the service goes down. Im assuming your using bash so enjoy.
i did simple script i hope this will be helpful please run this script as a root and add your services or daemons inside declaration array
declare -a service=(vsftpd sshd)
full script
#!/bin/bash
declare -a service=(vsftpd sshd) ##declaration array
for x in ${service[#]} ##array with
do
process=` ps -A | grep $x | awk '{print $4}' ` ### all process output
all_services=`echo $x`
line_no=` ps -A | sed -n '/'$all_services'/=' `
if ` ps -A | grep ${process[#]} > 0 ` ## condition to check if service available or not
then
echo "status running", " `ps -A | sed -n ''$line_no''p | awk ' {print $1 $4}'` " ## service up running
else
service $all_services start ### start the daemon again
fi
done

Bash script to stop ssh and smbd depending on SSID

My bash scripting skills are very rusty (it's been ~10 years since I scripted regularly). So, I'm probably overlooking something obvious. I am trying to get the following script to work:
#!/bin/bash
SSID=$(iwgetid -r)
if [[ $SSID = NETWORK ]]; then
if (( $(ps aux | grep [s]shd | wc -l) > 0 )); then
sudo service ssh stop
if (( $(ps aux | grep [s]mdb | wc -l) > 0 )); then
sudo service smbd stop
if (( $(ps aux | grep [n]mdb | wc -l) > 0 )); then
sudo service nmbd stop
else
echo You are not connected to NETWORK
fi
fi
fi
fi
exit 0
What it should do: If I am connected to particular network (SSID removed from script), check to see if ssh, smbd, and nmbd are running, and if so, stop them. If they aren't, do nothing. If not connected to the network, say so and exit. Bash seems to be ignoring the last 2 if...then statements, and I'm not sure why (Wrong order maybe? Conditional formatting incorrect?)
Any help would be appreciated. Also, if you have style tips, I'll take those too (like I said, I'm very rusty). I spent a few hours looking around here and Google, but couldn't find anything relevant and helpful.
You almost certainly meant to do this:
SSID=$(iwgetid -r)
if [[ $SSID = NETWORK ]]; then
if (( $(ps aux | grep [s]shd | wc -l) > 0 )); then
sudo service ssh stop
fi
if (( $(ps aux | grep [s]mdb | wc -l) > 0 )); then
sudo service smbd stop
fi
if (( $(ps aux | grep [n]mdb | wc -l) > 0 )); then
sudo service nmbd stop
fi
else
echo You are not connected to NETWORK
fi
But there is no real cost to running service stop on something which is not running, so you can simplify:
SSID=$(iwgetid -r)
if [[ $SSID = NETWORK ]]; then
sudo service ssh stop
sudo service smbd stop
sudo service nmbd stop
else
echo You are not connected to NETWORK
fi
Finally, I would recommend not using sudo inside the script. Instead, run the script itself with sudo check_ssid (or whatever it is called), so that the script ends up being:
SSID=$(iwgetid -r)
if [[ $SSID = NETWORK ]]; then
service ssh stop
service smbd stop
service nmbd stop
else
echo You are not connected to NETWORK
fi
By the way, if you really want to use ps and grep to check if a process is running, consider my answer here: How to test if a process is running with grep in bash?

Resources