Skip to main content

Shell Scripts for daily system admin use

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

=========


 =========


#!/bin/bash

#Purpose: Shell Scipt to check the load and manage it automatically.

#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

#Purpose: mysql dump backup

#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

Popular posts from this blog

Shell script with functions

Shell Script with Function: You can define a function and the commands/code in it. Then you can call that function at last line which will execute all commands mention in that block for you. f1() { var="Welcome to the geekstuff" echo ${#var} } f1 Output : ./test.sh 24

Ansible PlayBooks

Ansible playbooks are a way to send commands to remote computers in a scripted way. Instead of using Ansible commands individually to remotely configure computers from the command line, you can configure entire complex environments by passing a script to one or more systems. Ansible playbooks are written in the YAML data serialization format. If you don't know what a data serialization format is, think of it as a way to translate a programmatic data structure (lists, arrays, dictionaries, etc) into a format that can be easily stored to disk. The file can then be used to recreate the structure at a later point. JSON is another popular data serialization format, but YAML is much easier to read. Each playbook contains one or more plays, which map hosts to a certain function. Ansible does this through something called tasks, which are basically module calls. https://docs.ansible.com/ansible/devel/user_guide/playbooks.html Playbook example : You should write the code in file...