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
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 have several Db2 databases that I want to automatically activate after a system reboot. Restarting the Db2 service after a reboot is not a problem, but activating the databases requires access to the instance profile.
Service start/stop is crontrolled by system / systemctl. Including some user-controlled setup scripts into those scripts doesn't seem like a good idea. I briefly looked into enable-linger for the Db2 instance user or to use EnvironmentFile to set up the instance profile.
How do you activate all or a set of databases after reboot? Do you use user/group/EnvironmentFile with systemd? Do you enable linger or do you have any other method?
Here is a simple script which must be run from the Db2 instance owner.
It assumes, that Db2 instance is auto started. If it's not the case, just comment out db2gcf -s and uncomment db2gcf -u.
The script waits for the instance startup a configured number of seconds, and activates all local databases found in the Db2 instance system directory.
The script may be scheduled to run at the OS startup via Db2 instance owner's crontab entry as shown.
Log file (see the ${LOG} variable) with commands history is created in the Db2 instance owner's home directory.
#!/bin/sh
#
# Function: Activates all local DB2 databases
# Crontab entry:
# #reboot /home/scripts/db2activate.sh >/dev/null 2>&1
#
TIMEOUT=300
VERBOSE=${1:-"noverbose"}
export LC_ALL=C
if [ ! -x ~/sqllib/db2profile ]; then
echo "Must be run by a DB2 instance onwer" >&2
exit 1
fi
[ -z ${DB2INSTANCE} ] && . ~/sqllib/db2profile
if [ "${VERBOSE}" != "verbose" ]; then
LOG=~/.$(basename $0).log
exec 1>>${LOG}
exec 2>>${LOG}
fi
set -x
printf "\n*** %s ***\n" $(date +"%F-%H.%M.%S")
# Wait for the instance startup
# (or even start it with 'db2gcf -u' instead of checking status: 'db2gcf -s')
TIME1=${SECONDS}
while [ $((SECONDS-TIME1)) -le ${TIMEOUT} ]; do
db2gcf -s
# db2gcf -u
rc=$?
[ ${rc} -eq 0 ] && break
sleep 5
done
if [ ${rc} -ne 0 ]; then
echo "Instance startup timeout of ${TIMEOUT} sec reached" >&2
exit 2
fi
for dbname in $(db2 list db directory | awk -v RS='' '/= Indirect/' | grep '^ Database alias' | sort -u | cut -d'=' -f2); do
db2 activate db ${dbname}
done
Simple script which must be run from the Db2 instance owner.
su - <INSTANCE>
db2iauto -on <INSTANCE>
Exiting
exit
run user root
./<INSTANCE>/sqllib/bin/db2fmcu -d;
cd /<INSTANCE>/sqllib/bin/
./db2fmcu -u -p /opt/ibm/db2/<VERSION DB2>/bin/db2fmcd
./db2fm -i <INSTANCE> -U
./db2fm -i <INSTANCE> -u
./db2fm -i <INSTANCE> -f on
ps -ef | grep db2fm|grep <INSTANCE>
Done
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 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