Shell Script is nothing but sequence of commands that are stored in a single file (same like a text file) . The shell is the operating system's command interpreter and the set of commands you use to communicate with the system. A shell script is usually created for command sequences for which a user has a repeated need.
The shell script extension is ".sh". You need to save the file as filename.sh and give it 755 permission for execute.
The simple term for writing the shell script is collect all those task you need to perform in sequence and execute those line by line.
For example if you want to kill the CPU consuming processes or some other type of processes then you should use below shell script.
=======
#!/bin/bash
#Purpose: To kill the running process (backup or cpu consuming ) and record those processes in file upto certain size.
#Author: Mahesh Kale
#Date/Time: 06-06-2018 2:00
ps=($(ps -auxf | grep processname | awk '{print $2}'))
ps -auxf | grep processname >> log.txt
for i in "${ps[@]}"
do
echo "process killed" $i
kill -9 $i
echo "process killed" $i
done;
if [ $(stat -c%s log.txt) -ge 314572800 ]; then
mv log.txt log_back.txt
else
echo "File Size is less than 300MB"
fi
=========
=========
#Purpose: Shell Scipt to check the load and manage it automatically.
#Author: Mahesh Kale
#Date/Time: 08-08-2018.17:00
=========
=========
#Purpose: mysql dump backup
#Author: Mahesh Kale
#Date/Time: 09-09-2018. 19:30
=========
The shell script extension is ".sh". You need to save the file as filename.sh and give it 755 permission for execute.
The simple term for writing the shell script is collect all those task you need to perform in sequence and execute those line by line.
For example if you want to kill the CPU consuming processes or some other type of processes then you should use below shell script.
=======
#!/bin/bash
#Purpose: To kill the running process (backup or cpu consuming ) and record those processes in file upto certain size.
#Author: Mahesh Kale
#Date/Time: 06-06-2018 2:00
ps=($(ps -auxf | grep processname | awk '{print $2}'))
ps -auxf | grep processname >> log.txt
for i in "${ps[@]}"
do
echo "process killed" $i
kill -9 $i
echo "process killed" $i
done;
if [ $(stat -c%s log.txt) -ge 314572800 ]; then
mv log.txt log_back.txt
else
echo "File Size is less than 300MB"
fi
=========
=========
#!/bin/bash
#Author: Mahesh Kale
#Date/Time: 08-08-2018.17:00
load=$(cat /proc/loadavg | awk '{print $2}' | cut -d"." -f1)
echo "$load"
if [ $load -gt 10 ]
then
ps -auxf ; exim -bpc > /root/load.txt
killall -9 httpd
killall -9 php-cgi
sleep 10
service httpd restart
service httpd status
else
echo " The load is normal and http service is running on server"
fi
=========
=========
#!/bin/bash
#Author: Mahesh Kale
#Date/Time: 09-09-2018. 19:30
cd /var/lib/mysql/
ps=($(ls /var/lib/mysql/))
for i in "${ps[@]}"
do
mysqldump $i > $i.sql
done;
mv *.sql /backup/MYSQL_DUMPS/

Comments
Post a Comment