Linux self-healing script to check some process - linux

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

Related

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.

Command to detect if elastic search is active in 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

Check if service exists in bash (CentOS and Ubuntu)

What is the best way in bash to check if a service is installed? It should work across both Red Hat (CentOS) and Ubuntu?
Thinking:
service="mysqld"
if [ -f "/etc/init.d/$service" ]; then
# mysqld service exists
fi
Could also use the service command and check the return code.
service mysqld status
if [ $? = 0 ]; then
# mysqld service exists
fi
What is the best solution?
To get the status of one service without "pinging" all other services, you can use the command:
systemctl list-units --full -all | grep -Fq "$SERVICENAME.service"
By the way, this is what is used in bash (auto-)completion (see in file /usr/share/bash-completion/bash_completion, look for _services):
COMPREPLY+=( $( systemctl list-units --full --all 2>/dev/null | \
awk '$1 ~ /\.service$/ { sub("\\.service$", "", $1); print $1 }' ) )
Or a more elaborate solution:
service_exists() {
local n=$1
if [[ $(systemctl list-units --all -t service --full --no-legend "$n.service" | sed 's/^\s*//g' | cut -f1 -d' ') == $n.service ]]; then
return 0
else
return 1
fi
}
if service_exists systemd-networkd; then
...
fi
Hope to help.
Rustam Mamat gets the credit for this:
If you list all your services, you can grep the results to see what's in there. E.g.:
# Restart apache2 service, if it exists.
if service --status-all | grep -Fq 'apache2'; then
sudo service apache2 restart
fi
On a SystemD system :
serviceName="Name of your service"
if systemctl --all --type service | grep -q "$serviceName";then
echo "$serviceName exists."
else
echo "$serviceName does NOT exist."
fi
On a Upstart system :
serviceName="Name of your service"
if initctl list | grep -q "$serviceName";then
echo "$serviceName exists."
else
echo "$serviceName does NOT exist."
fi
On a SysV (System V) system :
serviceName="Name of your service"
if service --status-all | grep -q "$serviceName";then
echo "$serviceName exists."
else
echo "$serviceName does NOT exist."
fi
In systemd (especially in Debian), it doesn't seems to work properly using the various answers from here. For some services like pure-ftpd if it's in disabled mode, it will not show up in service list when you trigger this command:
systemctl --all --type service
and when you start again the pure-ftpd with systemctl start pure-ftpd the list will appear again. So listing the service using systemctl --all --type service will not work for all services. Take a look at this for more information.
So, this is the best code so far (improvement from #jehon's answer) to check if a service is exist (even it has status inactive, dead or whatever status it is):
#!/bin/bash
is_service_exists() {
local x=$1
if systemctl status "${x}" 2> /dev/null | grep -Fq "Active:"; then
return 0
else
return 1
fi
}
if is_service_exists 'pure-ftpd'; then
echo "Service found!"
else
echo "Service not found!"
fi
Explanation:
If systemctl status found a service, it must have a text 'Active:' we filter using grep and it would return 0. If there is no 'Active:' text it would return 1.
If systemctl status does not find the 'Active:' text, it will print out a standard error. So, I put redirection 2> /dev/null to redirect the standard error. For example, if you are looking for the non existence service, you would get this error message if you don't put that error redirection:
Unit pure-ftpdd.service could not be found.
We don't want to have the above standard error message if you are doing scripting
EDIT:
Another method is to list out unit files which able to detect disabled service as pointed by #Anthony Rutledge for Debian system:
systemctl list-unit-files --type service | grep -F "pure-ftpd"
But using this method will not always work especially for older system because some unit files might not be detected using this command as explained in here. Also, using this method is slower if you have large unit-files that need to be filtered (as commented by #ygoe about heavy load on a small computer).
To build off of Joel B's answer, here it is as a function (with a bit of flexibility added in. Note the complete lack of parameter checking; this will break if you don't pass in 2 parameters):
#!/bin/sh
serviceCommand() {
if sudo service --status-all | grep -Fq ${1}; then
sudo service ${1} ${2}
fi
}
serviceCommand apache2 status
After reading some systemd man pages ...
https://www.freedesktop.org/software/systemd/man/systemd.unit.html
... and systemd.services(5)....
... and a nice little article ...
https://www.linux.com/learn/understanding-and-using-systemd
I believe this could be an answer.
systemctl list-unit-files --type service
Pipe to awk {'print $1'} to just get a listing of the service units.
Pipe to awk again to get the service names exclusively. Change the field separator to the period with -F.
awk -F. {'print $1'}
In summary:
systemctl list-unit-files --type service | awk {'print $1'} | awk -F. {'print $1'}
With variation and augmentation of the base solution, you can determine the state of your system's services by combining a for loop with systemctl is-active $service.
#!/bin/sh
service=mysql
status=$(/etc/init.d/mysql status)
print "$status"
#echo $status > /var/log/mysql_status_log
var=$(service --status-all | grep -w "$Service")
if [ "output" != "" ]; then
#executes if service exists
else
#executes if service does not exist
fi
$Service is the name of the service you want to know if exists.
var will contain something like
[+] apache2
if the service does exist
if systemctl cat xxx >/dev/null 2>&1; then
echo yes
fi
Try this, as ps command can be used in both Ubuntu&RHEL, this should be work in both platform.
#!/bin/bash
ps cax | grep mysqld > /dev/null
if [ $? -eq 0 ]; then
echo "mysqld service exists"
else
echo "mysqld service not exists"
fi

Check if a screen session is running and automating via cron

So trying to get this to work, not familiar with linux but cobbled this together just having some problems... also the echo ''$NOW' Server was down.... Started !' ...seems to make to "server was ddown..."etc onto a line below which is weird because if I use the same line in another script the whole thing goes onto 1 line in the servercheck.txt file.
in check.sh >>
#!/bin/bash
if screen -list | grep -q "minecraft"; then
echo "Server is running!"
cd /home/minecraft/
teststart.sh
NOW=$(date +"%b-%d %H:%M")
echo ''$NOW' Server was down.... Started !' >> /home/minecraft/servercheck.txt;
else
echo "Server dead"
fi
errors...
: not found: check.sh:
Server is running!
: not found: check.sh: teststart.sh
: not found: check.sh:
And how to run teststart.... also the: not found: errors
this has to go into a cron aswel for the user minecraft, any help with that aswel wanting it to run every 5mins.
With
if screen -list | grep -q "minecraft";
you're only checking if the screen is running, not the server. Instead you can use something like
ps -ef | grep -i bukkit | grep -v grep; echo $?
If return is 0 the (bukkit) server is running if return is 1 the server is not running.
To remove the \n after $NOW use
echo -ne ''$NOW' Server was down.... Started !\n' >> /home/minecraft/servercheck.txt;

Resources