I'm trying to achieve the following steps using bash script:
1) Check the status of Apache Server.
2) If it is up and running, do nothing. If it is not, then go to step 3.
3) If server is not running, then send a failure email first and restart the server
4) After restarting, check the status again, and send a confirmation email
Here is my code:
#checking if Apache is running or not
ps auxw | grep apache2 | grep -v grep > /dev/null
if [ $? != 0 ]
then
mailx -s "Apache web server is down, Trying auto-restart" -$
# web server down, restart the server
sudo /etc/init.d/apache2 restart > /dev/null
sleep 10
#checking if apache restarted or not -- This is not working
ps auxw | grep apache2 | grep -v grep > /dev/null
if [ $? = 0 ]
then
mailx -s "Apache restarted succesfully" -r "$SENDEREMAIL" "$NOTIFYEMAIL" < /$
else
mailx -s "Restart Failed, try restarting manually" -r "$SENDEREMAIL" "$NOTIFYEMAIL" <$
fi
fi
The code is working properly till step 3, and failing/not working on step 4 i.e. script is unable to check the status of the server after restart and sending a confirmation email.
Can someone please let me know where I'm going wrong.
Try this:
#checking if Apache is running or not
if ! pidof apache2 > /dev/null
then
mailx -s "Apache web server is down, Trying auto-restart"
# web server down, restart the server
sudo /etc/init.d/apache2 restart > /dev/null
sleep 10
#checking if apache restarted or not
if pidof apache2 > /dev/null
then
message="Apache restarted successfully"
else
message="Restart Failed, try restarting manually"
fi
mailx -s "$message" -r "$SENDEREMAIL" "$NOTIFYEMAIL"
fi
Note: every mailx line had a trailing -$, < /$, or <$ -- these looked like typos and were deleted.
Related
I wrote a script to check for some services if they are active or not and restart the services.
The script works when ran manually but when added to crontab it does not work, when I check the logs I get
/bin/sh: 1: Syntax error: end of file unexpected
This is my script
#!/bin/bash
echo "Checking odoo services"
sudo service odoo13 status | grep 'active (running)' > /dev/null 2>&1
if [ $? != 0 ]
then
echo "Odoo not running restarting..."
sudo service odoo13 restart > /dev/null
fi
echo "Checking nginx services"
sudo service nginx status | grep 'active (running)' > /dev/null 2>&1
if [ $? != 0 ]
then
echo "Nginx inactive restarting"
sudo service nginx restart > /dev/null
fi
I set the crontab to run every minute
*/1 * * * * /opt/check_services.sh > /dev/null 2>
I do not know, what seems to be the issue?
So I figured it out
instead of #!/bin/bash I used #!/usr/bin/env bash
strange isn't it?
I wrote a node.js application and have written a bash script to start it and verify if it's running. I have my script run npm start & first, then I have a check to see if the ports I want open are open using netstat. The check works fine when I run it after the script is run, but during the running of the script, the check fails because the server has not fully started before the check is run. My code is below:
echo "Starting the server..."
npm start & > /dev/null 2>&1
if [[ -z $(sudo netstat -tulpn | grep :$portNum | grep node) ]] ; then
echo -e "\tPort $portNum is not in use, something went wrong. Exiting."
else
echo -e "\tPort $portNum is in use!"
fi
Is there a good way to take the above script and change it so that the check doesn't occur until the server is fully started? I don't want to use sleep if I can help it.
You can use a wait call:
echo "Starting the server..."
npm start & > /dev/null 2>&1
wait
if [[ -z $(sudo netstat -tulpn | grep :$portNum | grep node) ]] ; then
echo -e "\tPort $portNum is not in use, something went wrong. Exiting."
else
echo -e "\tPort $portNum is in use!"
fi
The only limitation to this is that if npm daemonizes itself then it's no longer a child of the script so the wait command will have no effect (the reason for this is that a process daemonizes itself by terminating and spawning a new process that inherits its role).
I am trying to create a Bash .sh script for a cronjob that starts the OpenSSH server if it is down or failed.
Last night the SSH server was down and when I tried to access it today (from work) the connection was refused ofc.
No traces in the /var/log/messages for the failure.
So the question is - how to determine is sshd running so if it is not to "sudo service ssh start" it?
Thanks in advance!
Fellas, I believe that I have managed to do the task:
#!/bin/bash
service="ssh"
if (( ! $(sudo service ssh status | cut -d" " -f 3 | cut -d"." -f 1) == "running" ))
then
sudo /etc/init.d/ssh restart
fi
I have changed the LogLevel to Verbose, I hope the next time I will track more clues regarding the failure of the sshd.
I'm implementing the following:
[ $(sudo service ssh status | grep running | grep -v grep | wc -l) == 0 ] && sudo /etc/init.d/ssh restart
as other people said It's Incorrect to start it without finding what caused this problem, but I wrote some help for the script you want
first you should check the status of your openssh server for example:
ps -aux | grep ssh
then write an if to check if it is down or not
you can check it with the result of previous step.
if that was down, start it
sudo service ssh start
I have a script that checks if MySQL service is running on my Linux server.
If I run the script manually it works fine, but when crontab runs the script it gets different results..
This is my script:
#! /bin/sh
TODAY=$(/bin/date)
UP=$(/sbin/service mysql status| /bin/grep 'SUCCESS' | /usr/bin/wc -l);
if [ "$UP" -ne 1 ];
then
echo "mysql not working, Date: $TODAY" >> /scripts/sql_log.txt;
sudo /bin/mail -s "MySql is DOWN" mail#mail.com < /dev/null
sudo /sbin/service mysql start
else
echo "mysql is working, Date: $TODAY" >> /scripts/sql_log.txt;
fi
I am using the full path of the commands..the only part that I do not understand 100% is:
if [ "$UP" -ne 1 ];
What is this -ne 1?
So in this case MySQL is running:
If I run the script manually it writes that MySQL is working in the log file.
But Crontab just write that MySQL is not running in the log file (even if it is running) and it does not send any mail or something
If mysql service is stopped and I run the script manually, it send me an email and start the service as it should...
Any idea?
Now it works..looks like that the problem was caused because I did not write the full path of the command..This is the script that I am using now and it is working:
#! /bin/sh
UP=$(/sbin/service mysql status| /bin/grep 'SUCCESS' | /usr/bin/wc -l);
if [ "$UP" -ne 1 ];
then
sudo /bin/mail -s "MySql is DOWN" mail#mail.com < /dev/null
sudo /sbin/service mysql start
fi
I have a server Application which holds 1500 process id, I need a shell Script which checks for every hour whether the process is up, if not start the process using "dsmc -u xxxx -p **" if it is not started send a mail to my gmail(xxx#gmail.com). here is the code how can i send a mail to my gmail account.
if pidof -s vsftpd = /dev/null; then
echo 'ftp is stopped'
sudo /etc/init.d/vsftpd restart
else
echo "The FTP server is Down" | mail -s "Ftp Server is Down" abcd#example.com
fi
I don't get mail to my Gmail Account.
Reference: http://rtcamp.com/wordpress-nginx/tutorials/linux/ubuntu-postfix-gmail-smtp/
to write your script you probably would want to investigate the following functions and their associated man pages
ps
pgrep
ptree
mail
The script part is easy to do. Troubleshooting the mail however is not. I would recommend running the script in screen (screen -d -m /bin/bash "test.sh"), detaching the screen (ctrl -a + d) then killing the process (/etc/init.d/vsftp stop), waiting 1 minute, and reattach the screen (screen -r). That will provide you the error with mail you can troubleshoot.
The following script will monitor your service for you.
#!/bin/bash
process="vsftp"
while true ; do
until [ ! $(pgrep $process) ]; do
sleep 1 #The number or minutes to wait until next check
done
#If process is not found do the following
/etc/init.d/$process start > /dev/null #Run as root because sudo requires password
if [ $? != 1 ]; then
echo "The FTP server was restarted" | mail -s "Ftp Server $process was restarted" abcd#example.com
else
echo "The FTP server could not restart" | mail -s "Ftp Server $process is down" abcd#example.com
sleep 1
exit 0;
fi
done
Hope this helps and good luck.
We can do the script with ps -ef
try,
# cat vsftpd.sh
#!/bin/bash
/bin/ps -ef | grep vsftpd > /dev/null 2>&1
if [ $? -ne 0 ]
then
/etc/init.d/vsftpd restart > /dev/null 2>&1
/bin/mail -s "FTP service is RESTARTED now" abcd#example.com
else
sleep 0
fi
cron:
* * * * * /bin/sh /path/to/vsftpd.sh > /dev/null 2>&1