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?
Related
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.
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 wrote a small script to check if openvpn is running and start it if it's not.
Here is the script i'm running
#!/bin/bash **-x**
ps auxw | grep openvpn | grep -v grep > /dev/null
if [ $? != 0 ]
then
/etc/init.d/openvpn start > /dev/null
log="/root/ServerRestart.log"
echo "The Openvpn Server was restarted at\n" > $log
date >> $log
fi
here is the crontab:
* * * * * /root/vpnmonitor.sh
it shows in the syslog that it runs the script but it does not seem to actually execute, the script works fine when run from a terminal.
The openvpn service won't start whitout the rigth path.
Try to include on your "vpnmonitor.sh":
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Like:
#!/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
logger "VPN restarted from cron"
/etc/init.d/openvpn restart vpn-servername
I made a new cronjob in /etc/crontab rather than using crontab -e and it works now, thanks everyone.
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
We have a Linux Daemon in c and a bash script to start it. The daemon sometimes fail to start because of some configuration file errors but the script reports the daemon was started successfully. A snippet of the script is shown as below, could someone tell me what's wrong with the script?
...
case "$1" in
start)
echo -n "Starting Demo Daemon: "
sudo -u demouser env DEMO_HOME=$DEMO_HOME /usr/local/demouser/bin/democtl startup > /dev/null 2> /dev/null
if [ "$?" = "0" ]; then
echo_success
else
echo_failure
fi
echo
;;
...
Thanks!
I feel there is nothing wrong with the script,it is the reponsibility of daemon to return non zero exit status if failed to start properly and based on those the script will display the message.(which i think it does)
You can add following line in your script to get running status of your Linux Daemon
status=`ps -aef |grep "\/usr\/local\/demouser\/bin\/democtl" |grep -v grep|wc -l`
if [ "$status" = "1" ]; then
echo_success
else
echo_failure
fi