Showing posts with label Scripts. Show all posts
Showing posts with label Scripts. Show all posts

Wednesday, June 11, 2014

Error establishing a database connection fix

To fix the below error, follow the steps below:


Go to the root directory of wordpress installation and execute the below script to generate the MySQL query.

root@server [/home/clearosa/public_html]# for i in `find -iname wp-config.php` ; do j=$(grep 'DB_NAME' $i | awk -F "'" '{print $4}') ; k=$(grep 'DB_USER' $i | awk -F "'" '{print $4}') ;l=$(grep 'DB_PASSWORD' $i | awk -F "'" '{print $4}') ; echo GRANT ALL PRIVILEGES ON $j.* TO $k@"localhost" identified by "'$l'"";" ; done

Sample Output:

root@meow [/home/kooi/public_html]# for i in `find -iname wp-config.php` ; do j=$(grep 'DB_NAME' $i | awk -F "'" '{print $4}') ; k=$(grep 'DB_USER' $i | awk -F "'" '{print $4}') ;l=$(grep 'DB_PASSWORD' $i | awk -F "'" '{print $4}') ; echo GRANT ALL PRIVILEGES ON $j.* TO $k@"localhost" identified by "'$l'"";" ; done
GRANT ALL PRIVILEGES ON kooid.* TO buhaha@localhost identified by 'koppan';

Now copy the MySQL query and execute it in MySQL prompt to fix this error.


Monday, May 7, 2012

Script to check if mysql is alive and restart if its not.

The script below is done in Bash, which can be used across all Linux platforms. The function of the script is to check if mysql is currently running properly else it will restart the service.


1. Navigate to /root.
$ cd /root

2. Create a new file
$ vim mysql.sh
or
$ vi mysql.sh
Also give the command below to check the logs
$ touch /root/mysql.log

3. Insert the following lines to the file
-----------------------------------
#!/bin/bash
/usr/bin/mysqladmin ping| grep 'mysqld is alive' > /dev/null 2>&1
if [ $? != 0 ]
then
        /etc/init.d/mysqld stop
        /etc/init.d/mysqld start
        echo Starting at `date`
fi
-----------------------------------

4.  Give execute permission.
$ chmod 755  mysql.sh

5. Set a cron to check it at regular intervals.
$ crontab -e
*/5 * * * * sh -x mysql.sh
(This will check every 5 minutes)

Script to Check the load of webserver and Restart Services

The script below is done in Bash, which can be used across all Linux platforms. The function of the script is to check the web-server load and restart the normal services which reduces the load. It restarts apache and mysql. You can custom this to restart your services.

1. Navigate to /root.
$ cd /root

2. Create a new file
$ vim load.sh
or
$ vi load.sh

3. Insert the following lines to the file
-----------------------------------
LOAD=`uptime|awk '{print $10}'|cut -d, -f1 |cut -d. -f1`
if [ $LOAD -ge 50 ]
then
killall -9 mysqld
killall -9 mysqld
killall -9 mysqld
killall -9 httpd
killall -9 httpd
killall -9 httpd
for SEMID in `ipcs -s httpd| awk '{print $2}'| grep -iv sem`; do ipcrm -s $SEMID; done
/etc/init.d/httpd start
sleep 20
/etc/init.d/mysqld start
echo high load $LOAD at `date`, mysql/apache restarted >> /usr/monitor/mysql.log
fi
-----------------------------------

4.  Give execute permission.
$ chmod 755  load.sh

5. Set a cron to check it at regular intervals.
$ crontab -e
*/5 * * * * sh -x load.sh
(This will check every 5 minutes)

Script to Check Disk Space and Send Mail if its Full

The script below is done in Bash, which can be used across all Linux platforms. The function of the script is to check disk space at regular intervals and report back through e-mail if the server is running out of disk space. We can also specify multiple e-mail addresses.

1. Navigate to /root.
$ cd /root

2. Create a new file
$ vim diskcheck.sh
or
$ vi diskcheck.sh

3. Insert the following lines to the file
-----------------------------------
#!/bin/sh
df -HlP | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
  echo $output
usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
 partition=$(echo $output | awk '{ print $2 }' )
if [ $usep -ge 95 ]; then
echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" |
mail -s "Alert: Almost out of disk space $usep%" xyz@domain.com
echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" |
mail -s "Alert: Almost out of disk space $usep%" xyz1@domain.com
echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" |
mail -s "Alert: Almost out of disk space $usep%" xyz2@domain.com
fi
done
-----------------------------------

4.  Give execute permission.
$ chmod 755  diskcheck.sh

5. Set a cron to check it at regular intervals.
$ crontab -e
*/5 * * * * sh -x diskcheck.sh
(This will check every 5 minutes)

Monday, November 28, 2011

Script to Alert when server is UP and running

The inspiration which led me to making this script was because of a couple of reasons. Firstly because the internet connectivity is intermittent here, so i run this script and check for google.com hence I will get notified when connection is back unless google is down ;) .

The second reason is that I work on a lot of servers and when a server is gone for a reboot, I can run this script and give the server IP as input hence I will get notified when server is back online.

++++++++++++++++++++++++++++++++++++++
#!/bin/bash
echo "Enter ip/domain name"
read a
HOSTS=$a
COUNT=4
for myHost in $HOSTS
do
count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
if [ $count -eq 4 ]; then
# 100% Up
echo "Host : $myHost is Working (ping Sucess) at $(date)"
fi
done
++++++++++++++++++++++++++++++++++++++

Change Wallpaper Script

The script below will change the wallpaper by randomly selecting a picture from a directory specified. For the script to work you will have to add it as a cron and set the interval of your choice for the wallpaper change.
 
Open your favourite editor and paste the codes below:
+++++++++++++++++++++++++
#!/bin/bash

# Directory Containing Pictures
DIR=/home/hari/Pictures
LOG=/home/hari/tapeta.log

# Command to Select a random file from directory
PIC="$(
for p in [jJ][pP][gG] [pP][nN][gG] ; do
ls $DIR/*.$p
done | shuf -n1
)"

# Command to set Background Image
if [ -z "$DBUS_SESSION_BUS_ADDRESS" ] ; then

# this is because of gconftool bug in cron
TMP=~/.dbus/session-bus
export $(grep -h DBUS_SESSION_BUS_ADDRESS= $TMP/$(ls -1t $TMP | head -n 1))
echo $DBUS_SESSION_BUS_ADDRESS >> $LOG
fi

gconftool-2 -t string -s /desktop/gnome/background/picture_filename "$PIC"
+++++++++++++++++++++++++

Note: Tested in Fedora 14 and working fine. Should work in centos and other red hat flavours.
Reffered: http://stackoverflow.com/questions/2182468/run-cronjob-as-user-to-change-desktop-background-in-ubuntu

Monday, January 3, 2011

Script to replace a string with another string in a file.

Lets assume that the file name is test. The file test contains many lines and you want to replace a line with another line.

1) Make a file abc,sh.
$ touch abc.sh

2) Insert the script into abc,sh.
$ vi abc.sh press i, then paste

#/bin/bash
cat filename
echo "enter the newstring"
read new
echo "enter the oldstring"
read old
sed -i "s/$old/$new/g" test

3) sh abc.sh

Output:
------
enter the newstring
new
enter the oldstring
old
------

Script to Restore multiple accounts from another server. Cpanel.

Copy the backup files of cpanel in *.tar.gz to the new servers /home. After that open a screen.
1) $ screen

2)Then run the following script in the screen.
$ for i in `cat file1`; do /scripts/restorepkg $i; done
Note:in file1 the usernames must be present. For that run this script.
(for i in `cat /etc/trueuserdomains`; do echo $i|cut -d . -f1; done > file1)

3)Then exit from screen.
press ctrl + a + d.

Friday, September 17, 2010

List top 10 Files or Directories Size Wise

Type the command below to see the files listed size wise from higher size files to lower size files.

-----------------
for X in $(du -s * | sort -nr | cut -f 2); do du -hs $X ; done
-----------------

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Affiliate Network Reviews