I am new to bash scripting and trying to figure out why the below script is outputting that Apache server is not running whereas it is running properly.
ps cax | grep httpd
if [ $? -eq 0 ]; then
echo "Process is running."
else
echo "Process is not running."
fi
I'm running it on Ubuntu 14.04.2 LTS
Also, how do I make changes to the script that this can test apache server installed on another machine.
Kindly help
This is a working sample of bash script which check the apache status, restart it automatically if down, and alert by telegram bot within unicode emoji.
#!/bin/bash
telegram=(xxxxx, yyyyyy)
if ! pidof apache2 > /dev/null
then
# web server down, restart the server
echo "Server down"
/etc/init.d/apache2 restart > /dev/null
sleep 10
#checking if apache restarted or not
if pidof apache2 > /dev/null
then
for i in "${telegram[#]}"
do
curl -s -X POST https://api.telegram.org/botxxxxxx:yyyyyyyyyyyyyyyyyyyyyyyyyy/sendMessage -d chat_id="$i" -d text="`echo -e '\U0001F525'` Apache stoped on Molib Stage. Automatically restarted succesfully."
done
else
for i in "${telegram[#]}"
do
curl -s -X POST https://api.telegram.org/botxxxxxx:yyyyyyyyyyyyyyyyyyyyyyyyyy/sendMessage -d chat_id="$i" -d text="`echo -e '\U0001F525'` Apache stoped on Molib Stage. Automatically restart failed. Please check manually."
done
fi
fi
Use this:
service apache2 status
Or this:
service --status-all | grep apache2
Instead of httpd try to grep "apache2". To be sure try to check services with the next command and decide the registered name of the apache webserver:
service --status-all
Try and see - simply simplest, most didactic here and well working on Ubuntu 20.04:
catching output of status to bash variable
"if" status includes substring (from "Active:" statement) - do job you wanted
"else" - do another job you defined
#!/bin/bash
servstat=$(service apache2 status)
if [[ $servstat == *"active (running)"* ]]; then
echo "process is running"
else echo "process is not running"
fi
This work perfect in an old Debian. Remember to run with bash and not with sh.
In Centos replace with httpd.
#!/bin/bash
if [ $(/etc/init.d/apache2 status | grep -v grep | grep 'apache2 is running' | wc -l) > 0 ]
then
echo "Process is running."
else
echo "Process is not running."
fi
## Plz run this script .. its working
------------------------------------------------
ps cax | grep httpd
if [ $? -eq 1 ]
then
echo "Process is running."
else if [ $? -eq 0 ]
echo "Process is not running."
fi
fi
----------------------------------------------
This is menu driven one stop shell script in which you can check the firewall,apache or any other webservices ,you can start or stop the services just by choosing the option in below script
echo "welcome please select your options"
read choice
firewall=`sudo systemctl status firewalld`
apache=`sudo systemctl status apache2`
firewall1=`sudo systemctl stop firewalld`
apache1=`sudo systemctl stop apache2`
startrfirewall=`sudo systemctl start firewalld`
startapache=`sudo systemctl start apache2`
case $choice in
1) status of the firewall is $firewall
;;
2) status of apache is $apache
;;
3) echo stop firewall by $firewall1
;;
4) echo stop apache by $apache1
;;
5) echo start firewall by $startrfirewall
;;
6) echo start apache by $startapache
;;
*) echo exit
esac
I put this together based on the above and made so can use other services.
Hope this helps.
#!/bin/bash
# Must be running as root or via sudo permissions to be able to restart
# Put your process name restart command line in
PROCESS_NAME=httpd
if ! pidof $PROCESS_NAME > /dev/null
then
# web server down, restart the server
echo "Server $PROCESS_NAME down"
/usr/sbin/apachectl restart > /dev/null
echo "Tried restart of $PROCESS_NAME. Waiting 10 seconds to settle."
# wait ten
sleep 10
#checking if process restarted or not
if pidof $PROCESS_NAME > /dev/null
then
echo "$PROCESS_NAME was down but is now up."
else
echo "$PROCESS_NAME is still down. Please take some action."
fi
else
echo "Server $PROCESS_NAME up."
fi
Related
I want to check if one, (or all) services are running, if yes, stop it
#!/bin/bash
# Define an array of processes to be checked.
# If properly quoted, these may contain spaces
check_process=( "nagios" "httpd" )
for p in "${check_process[#]}"; do
if pgrep "$p" > /dev/null; then
echo "Process \`$p' is running, stopping it"
service $p stop
else
echo "Process \`$p' is not running"
fi
done
For httpd service all works fine, script detects correctly httpd service state.
I have issues detecting nagios service state.
But although nagios service is not running, script shows it's running
Process `nagios' is running, stopping it
Stopping nagios:No lock file found in /usr/local/nagios/var/nagios.lock
Process `httpd' is not running
Is there any more elegant way of detecting if nagios service is running without checking if nagios.lock file exists ?
pgrep nagios shows no output when service is not tunning.
I gave up, this works fine for me:
although ps -ef | grep -v grep | grep $service | wc -l shows 0 for nagios, script reports that nagios service is running
#!/bin/bash
logfile=/tmp/stop_nagios.txt
exec >> $logfile
exec 2>&1
service=httpd
if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 ))
then
echo "$service is running, stopping it"
date
sudo service $service stop
else
echo "$service is not running"
fi
# check nagios service
FILE=/usr/local/nagios/var/nagios.lock
if test -f "$FILE"; then
echo "nagios service is running, stopping it"
date
sudo service nagios stop
else
echo "nagios is not running..."
fi
I have to check service is running or not on Remote Server(192.168.1.105) using Nagios Server if service is not running then I want to run this service.
I am using Nagios with NRPE.
For this I am using below script
#!/bin/bash
if pgrep -f "index.js" >/dev/null; then
echo "index.js is Running."
exit 0
else
echo "index.js is Stopped."
exit 2
fi
With help of above script i am able to check service is running or not.
But my question is that if service is not running so how can i run this service.
For run service on remote server i am just edit the above service as mentioned in below
#!/bin/bash
if pgrep -f "index.js" >/dev/null; then
echo "index.js is Running."
exit 0
else
echo "index.js is Stopped."
servicestatus=$(ssh root#192.168.1.105 nohup node /root/demo/index.js > index.log &)
echo "$servicestatus"
exit 2
fi
But this is not working for me.
This question already has answers here:
Pass commands as input to another command (su, ssh, sh, etc)
(3 answers)
Closed 6 years ago.
This is the script but output is wrong even Apache is running its show stop. I'm using Ubuntu 12.04.
ssh -qn root# ip
if ps aux | grep [h]ttpd > /dev/null
then
echo "Apcache is running"
else
echo "Apcahe is not running"
fi
Try the following:
if ssh -qn root#ip pidof httpd &>/dev/null ; then
echo "Apache is running";
exit 0;
else
echo "Apache is not running";
exit 1;
fi
These exit commands will send the correct EXIT_SUCCESS and EXIT_FAILURE too ( Will be usefull to extend this script in future, if you need ).
But ONE ADVICE : Is better to put the script as a remote process to run with a sudoer user over ssh account
You are not running the commands on the remote host.
Try this instead.
if ssh -qn root#ip ps aux | grep -q httpd; then
echo "Apache is running"
else
echo "Apache is not running"
fi
Just to be explicit, ps aux is the argument to ssh and so that is what is being executed on the remote host. The grep runs as a child of the local script.
First of all httpd is not available in ubuntu. For ubuntu apache2 is available.
So this command ps aux | grep [h]ttpd will not work on ubuntu.
No need to write any script to check the apache status. From ubuntu terminal run this command to get the status:
sudo service apache2 status
Output will be:
A > if apache is running: Apache2 is running (pid 1234)
B > if apache is not running: Apache2 is NOT running.
Since ssh returns with exit status of the remote command check man page for ssh and search for exit status
so Its as simple as
ssh root#ip "/etc/init.d/apache2 status"
if [ $? -ne 0 ]; then # if service is running exit status is 0 for "/etc/init.d/apache2 status"
echo "Apache is not running"
else
echo "Apache is running"
fi
You do not need ps or grep for this
I've been looking for a bit but can't find a free/open-source tomcat 7 monitoring tool that will send out e-mails or notifications when certain situations occur. For example when CPU utilization spikes or RAM is consistently full. Things Like that.
I've looked at JMelody and Psi-Probe and neither of them have the ability to send e-mails when some event occurs.
You can take a look at jboss RHQ
https://docs.jboss.org/author/display/RHQ/Alerts
This might help someone!!
If one do not want to use any monitoring tool, then set up an email configurtion in Ubuntu server using mailutils package.
https://rianjs.net/2013/08/send-email-from-linux-server-using-gmail-and-ubuntu-two-factor-authentication
https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-postfix-as-a-send-only-smtp-server-on-ubuntu-14-04
To monitor Tomcat status you can use below script and set up cron job which runs every minute/hour/day according to your needs.
#!/bin/bash
TOMCAT_HOME=/opt/tomcat
PUBLIC_IP=`wget http://ipecho.net/plain -O - -q ; echo`
EMAIL_BODY="Hi Admin,\n\n$PUBLIC_IP Tomcat is down at $(date -d "+330 minutes" +"%Y-%m-%d %T") IST, Please take necessary action.\n\n\nDo not reply to this email as it is auto generated by Ubuntu system\n"
tomcat_pid() {
echo `ps aux | grep org.apache.catalina.startup.Bootstrap | grep -v grep | awk '{ print $2 }'`
}
start() {
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "Tomcat is already running (pid: $pid)"
else
# Start tomcat
echo "Starting tomcat"
/bin/sh $TOMCAT_HOME/bin/startup.sh
fi
return 0
}
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "Tomcat is running with pid: $pid"
#stop
else
echo "Tomcat is not running"
# send an email alert then start
echo -e $EMAIL_BODY | mail -s "$PUBLIC_IP Tomcat is down" user#email.com
echo "Mail sent"
#remove cache and release memory occupied by heavy processes
start
fi
exit 0
I've written a script for me to start and stop my Perforce server. To shutdown the server I use the kill -SIGTERM command with the PID of the server daemon. It works as it should but there are some discrepancies in my script concerning the output behavior.
The script looks as follows:
#!/bin/sh -e
export P4JOURNAL=/var/log/perforce/journal
export P4LOG=/var/log/perforce/p4err
export P4ROOT=/var/local/perforce_depot
export P4PORT=1666
PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"
. /lib/lsb/init-functions
p4start="p4d -d"
p4stop="p4 admin stop"
p4user=perforce
case "$1" in
start)
log_action_begin_msg "Starting Perforce Server"
daemon -u $p4user -- $p4start;
echo "\n"
;;
stop)
echo "BLABLA"
echo "$(pidof /usr/local/bin/p4d)"
#daemon -u $p4user -- $p4stop;
p4dPid="$(pidof /usr/local/bin/p4d)"
echo $p4dPid
if [ -z "$(pidof /usr/local/bin/p4d)" ]; then
echo "ERROR: No Perforce Server running!"
else
echo "SUCCESS: Found Perforce Server running!\n\t"
echo "Shutting down Perforce Server..."
kill -15 $p4dPid;
fi
echo "\n"
;;
restart)
stop
start
;;
*)
echo "Usage: /etc/init.d/perforce (start|stop|restart)"
exit 1
;;
esac
exit 0
When p4d is running the stop block works as intended, but when there is no p4d running the script with stop only outputs BLABLA and an empty new line because of the echo "$(pidof /usr/local/bin/p4d)". The error message stating that no server is running is never printed. What am I doing wrong here?
PS: The part if [ -z "$(pidof /usr/local/bin/p4d)" ]; then has been changed from if [ -z "$p4dPid" ]; then for debug reasons.
EDIT: I narrowed down the problem. If I don't use the p4dPid variable and comment out the lines p4dPid="$(pidof /usr/local/bin/p4d)" and echo $p4dPid the if block is processed and the error messages is printed. Still I don't unterstand what is causing this behavior.
EDIT 2: Problem solved!
The -e in #!/bin/sh -e was causing the shell to exit the script after any statement returning a non-zero return value.
When your service is not running, the command
echo "$(pidof /usr/local/bin/p4d)"
is processed as
echo ""
because pidof did not return any string. So the command outputs an empty line.
If you do not want this empty line, then just remove this statement, after all you print an error message when the process is not running.
Problem solved!
The -e in #!/bin/sh -e was causing the shell to exit after any statement returning a non-zero return value.