Linux and Unix Scripting

Is Shell scripting Useful ?

Here I am discussing the use of shell scripting in the linux server administration. A shell script is a computer program created for running in a shell. Here I am using “bash” shell, which is the normal shell used in linux based operating systems.   In day to day server administration shell scripting will help the system administrators to do the tasks with ease and complete them with less amount of time. Shell scripts will help system administrators in migrations and crash recovery too.

Shell scripting

It is a group of commands with piping results of one command to another and properly arranges the results of commands by adding some programming logic to accomplish some task. It needs a good familiarization of piping results of one command to another and basic programming skill to write shell scripts. Oneliners are another important elements while talking about shell scripting. Oneliners are linux commands combined together using individual commands to accomplish some tasks. Example of some one-liners are:

The following command lists all the process sorted based on the used memory size.

$ ps aux | awk '{if ($5 != 0 ) print $2,$5,$6,$11}' | sort -k2n

Use of shell scripting

Following are the areas where the use of shell scripting in server administration.

System scripts

You can see the shell scripts in the linux operating system. Most of the system scripts are shell scripts. You can review these scripts as a reference while studying shell scripting. You can see the shell scripts under the following directories;


Day-to-day of the system administrator

In day to day checks, the system administrator needs to run some commands repeatedly. Create a script using these commands and add it to cron easy administration. We can create scripts for generating server security reports too.

Migration assistance

In migration process sometimes it needs to make some custom scripts to make it easier or in the case of custom server migrations with a large number of accounts. Final syncing of accounts before the dns change also need some scripting assistance.

Crash recovery

In the case of operating system crash, the system administrator needs to restore the files from different locations. The paths are well known for control panel based servers. Restoring the files from few accounts us easy, but if the number of accounts are more than 1000, the manual restoration is time-consuming and it is impossible. Shell scripting will help to complete this task in limited time. A scripting expert can easily create a script for the restore and recover the system with lightning speed.

Example shell script

Hello world

Hello world is a common script while we used for testing.

echo "Hello World"

Normally the scripts are saved in “.sh” extension. Here the above script is naming as and you can execute the above script using the following steps.

Permission change:
change the permission of the shell script to 755 before running.

bash # using bash command
/home/suhesh/ #using full path
./ #run from directory where the script running.

The above three methods are using for running the script. First method use “bash” command to execute the script. There is no need to set “755” permission for the script, while we run the script. The second and third method requires “755” permission. In the second method full path of the script used for running the script. We can run the script without bothering about the present working directory. The third method is another option and in that case, the scripts need to be run from the directory where the script present.

Monitor script.

The following script will monitor the server load, memory usage, and processes. By adding the script in the cron with some frequency will help the system administrators to verify it if any outages occur with the server. The system administrator can use more commands to monitor the resources on the server if needed. Following script will log the data for one day only and it overwrites after every day to avoid disk space issues. If need more data, needs to adjust the script to archive the data after every day.

# Shell script to monitor the process
mkdir -p /var/log/monitor
date=`date +%H_%M`
top -b -n 1 > /var/log/monitor/top_$date
free -m > /var/log/monitor/free_$date
w > /var/log/monitor/w_$date

In my opinion, all system administrators should learn bash shell scripting to help their day to day activities and improve the career.