shell script to create thumbnail

create sh file from shell(via putty) by following command

sudo nano create_image_thumb.sh

========================================================

#!/bin/bash
THUMBS_FOLDER=/home/path/public_html/images_thumb
for file in /home/path/public_html/images/*
do
if [ -f “${THUMBS_FOLDER}/$(basename “$file”)” ]
then
echo “found.”
else
echo “${THUMBS_FOLDER}/$(basename “$file”) not found.”
convert “$file” -resize 250×215^ “${THUMBS_FOLDER}/$(basename “$file”)”
fi
done

==============================================================

press ctrl +x and press y to save

you can used this sh file from cron script by command

  • * 12 * * * sh -x /home/path of sh/create_image_thumb.sh – it execute on every 12 hr once a day
  • *  0,12 * * * sh -x /home/path of sh/create_image_thumb.sh – it execute on every 12 hr twice a day

You need to install imagemagick lib on server for it.

 

start apache2,mysql,nginx from shell script root crontab on ubuntu

in case Debian 3.2.68 distribution . all syntax will be working fine as per my live test.

cd /usr/local/vesta/bin

sudo nano apache-restart.sh

type following..

=========================================================================..

#!/bin/sh

ps auxw | grep apache2 | grep -v grep > /dev/null

if [ $? != 0 ]
then
        service apache2 start > /dev/null
fi
======================================================================================
save it as apache-restart.sh
sudo nano mysql-restart.sh
===================================================================================
#!/bin/bash
/usr/bin/mysqladmin ping| grep 'mysql is alive' > /dev/null 2>&1
if [ $? != 0 ]
then
 sudo service mysql restart
fi
====================================================================================================
save it as mysql-restart.sh

sudo nginx-restart.sh
=========================================================================================

#!/bin/sh
sudo service nginx status > /dev/null

if [ $? != 0 ]
then
 echo "nginx stopped"
 sudo service nginx start > /dev/null
 echo "nginx start"
fi

=============================================================
type crontab -e and type the following shell script to run every minute from root cron user.

* * * * * sh -x /usr/local/vesta/bin/mysql-restart.sh
* * * * * sh -x /usr/local/vesta/bin/apache-restart.sh
* * * * * sh -x /usr/local/vesta/bin/nginx-restart.sh

and save it . that's all. in case any problem you can contact me at [email protected]

 

mysql restart from cron using shell script

check crontab -u root -l - list of cron for user root
cd /home/smruti/
sudo nano mysql-check.sh
==========================================================
#!/bin/bash
/usr/bin/mysqladmin ping| grep 'mysqld is alive' > /dev/null 2>&1
if [ $? != 0 ]
then
    sudo service mysqld restart
fi
======================================
save as  mysql-check.sh
To make it executable chmod 0755 mysql-check.sh
create cron job  crontab -e from command 
type * * * * * sh -x /home/smruti/mysql-check.sh
press esc :wq for save

check it by type sudo service mysqld stop
it will automatically start after 1 min.

mysql restart shell script

#!/bin/bash
# mysql root/admin username
MUSER=”root”
# mysql admin/root password
MPASS=”xxxxxxx”
#nano /usr/etc/mysql/debian.cnf
# mysql server hostname
MHOST=”localhost”
#Shell script to start MySQL server i.e. path to MySQL daemon start/stop script.
# Debain uses following script, need to setup this according to your UNIX/Linux/BSD OS.
MSTART=”/etc/init.d/mysql start”
# Email ID to send notification
#EMAILID=”[email protected]
# path to mail program
#MAILCMD=”$(which mail)”
# path mysqladmin
MADMIN=”/usr/bin/mysqladmin”

#### DO NOT CHANGE anything BELOW ####
MAILMESSAGE=”/tmp/mysql.fail.$$”

$MADMIN -h $MHOST -u $MUSER -p${MPASS} ping 2>/dev/null 1>/dev/null
if [ $? -ne 0 ]; then
echo “” >$MAILMESSAGE
echo “Error: MySQL Server is not running/responding ping request”>>$MAILMESSAGE
echo “Hostname: $(hostname)” >>$MAILMESSAGE
echo “Date & Time: $(date)” >>$MAILMESSAGE
# try to start mysql
$MSTART > /dev/null
# see if it is started or not
o=$(ps cax | grep -c ‘ mysqld$’)
if [ $o -eq 1 ]; then
sMess=”MySQL Server MySQL server successfully restarted”
else
sMess=”MySQL server FAILED to restart”
fi
# Email status too
echo “Current Status: $sMess” >>$MAILMESSAGE
echo “” >>$MAILMESSAGE
echo “*** This email generated by $(basename $0) shell script ***” >>$MAILMESSAGE
echo “*** Please don’t reply this email, this is just notification email ***” >>$MAILMESSAGE
# send email
#$MAILCMD -s “MySQL server” $EMAILID < $MAILMESSAGE
else # MySQL is running 🙂 and do nothing
:
fi
# remove file
rm -f $MAILMESSAGE