Automating Linux system maintenance tasks with Shell scripting

Source: Internet
Author: User

If a system administrator spends a lot of time solving problems and doing repetitive work, you should wonder if he is doing the right thing. An efficient system administrator should make a plan to spend as little time as possible on repetitive tasks. So while it looks like he didn't do a lot of work, it's because the shell script helped him do most of the tasks, which is what we're going to explore.

What is a shell script?

In short, a shell script is a program that the shell executes step-by-step, and the shell is another program that provides an interface between the Linux kernel and the end user. By default, the shell used by users in RHEL 7 is bash (/bin/bash).

Write a script to display system Information

First let's create a new directory to hold our shell script:

# mkdir scripts# CD scripts

Create a new text file, insert some comments in the header, and some commands:

# This script will return the following system information:
# -Host Name:
Echo -e "\e[31;43m***** HOSTNAME INFORMATION *****\e[0m"
Echo ""
# - File system disk space usage:
Echo -e "\e[31;43m***** FILE SYSTEM DISK SPACE USAGE *****\e[0m"
Df -h
Echo ""
# - System idle and in-use memory:
Echo -e "\e[31;43m ***** FREE AND USED MEMORY *****\e[0m"
Echo ""
# - System startup time:
Echo -e "\e[31;43m***** SYSTEM UPTIME AND LOAD *****\e[0m"
Echo ""
# -Login user:
Echo -e "\e[31;43m***** CURRENTLY LOGGED-IN USERS *****\e[0m"
Echo ""
# - 5 processes using the most memory
Echo -e "\e[31;43m***** TOP 5 MEMORY-CONSUMING PROCESSES *****\e[0m"
Ps -eo %mem,%cpu,comm --sort=-%mem | head -n 6
Echo ""
Echo -e "\e[1;32mDone.\e[0m"

Then, give the script executable permissions and run the script:

# chmod +x

For a better visualization, each part of the title is displayed in color:
The color feature is provided by the following command:

Echo-e "\E[COLOR1; Color2m\e[0m "

Where COLOR1 and COLOR2 are the foreground and background colors and are the strings you want to display in color.

Automating the Task

The tasks that you want to automate can vary depending on the situation. Therefore, it is not possible to cover all possible scenarios in an article, but we will describe three typical tasks that can be automated using shell scripts: 1) Update the local file database 1) find (or delete) files with 777 permissions 2) The file system warns when it uses more than the defined thresholds. Let's create a new file named in the script directory and add the following:

#Automated task example script:
# - Update the local file database:
If [ $? == 0 ]; then
        Echo "The local file database was updated correctly."
        Echo "The local file database was not updated correctly."
Echo ""
# - Find and/or delete files with 777 permissions.
# Enable either option (comment out the other line), but not both.
#Option 1: Delete files without prompting for confirmation. Assumes GNU version of find.
#find -type f -perm 0777 -delete
#Option 2: Ask for confirmation before deleting files. More portable across systems.
Find -type f -perm 0777 -exec rm -i {} +;
Echo ""
# - Warn when the file system usage exceeds the defined threshold
Echo -e "\e[4;32mCHECKING FILE SYSTEM USAGE\e[0m"
While read line; do
        # This variable stores the file system path as a string
        FILESYSTEM=$(echo $line | awk ‘{print $1}‘)
        # This variable stores the use percentage (XX%)
        PERCENTAGE=$(echo $line | awk ‘{print $5}‘)
        # Use percentage without the % sign.
        If [ $USAGE -gt $THRESHOLD ]; then
                Echo "The remaining available space in $FILESYSTEM is critically low. Used: $PERCENTAGE"
Done < <(df -h --total | grep -vi filesystem)

Note that there is a space between the last line of the script and two < symbols.

using Cron

To further improve your efficiency, you don't just want to sit in front of your computer and execute these scripts manually. Instead, you use cron to schedule these tasks to execute periodically and send the results to pre-specified recipients by mail, or save them to a file that you can view using a Web browser. The following script ( runs the well-known df-h command, formats the output to an HTML table and saves it to the report.html file:

# Demonstrate a sample script for creating an HTML report using a shell script
# Web directory
# A little CSS and table layout to make the report look a little nicer
Echo "<HTML>
.titulo{font-size: 1em; color: white; background:#0863CE; padding: 0.1em 0.2em;}
Table, td, th
Border:1px solid black;
<meta http-equiv=‘Content-Type‘ content=‘text/html; charset=UTF-8’ />
<BODY>" > $WEB_DIR/report.html
# View hostname and insert it at the top of the html body
Echo "Filesystem usage for host <strong>$HOST</strong><br>
Last updated: <strong>$(date)</strong><br><br>
<table border='1‘>
<tr><th class=‘titulo‘>Filesystem</td>
<th class=‘titulo‘>Size</td>
<th class=‘titulo‘>Use %</td>
</tr>" >> $WEB_DIR/report.html
# Read the output of df -h line by line
While read line; do
Echo "<tr><td align=‘center‘>" >> $WEB_DIR/report.html
Echo $line | awk ‘{print $1}‘ >> $WEB_DIR/report.html
Echo "</td><td align=‘center‘>" >> $WEB_DIR/report.html
Echo $line | awk ‘{print $2}‘ >> $WEB_DIR/report.html
Echo "</td><td align=‘center‘>" >> $WEB_DIR/report.html
Echo $line | awk ‘{print $5}‘ >> $WEB_DIR/report.html
Echo "</td></tr>" >> $WEB_DIR/report.html
Done < <(df -h | grep -vi filesystem) echo "</table></BODY></HTML>" >> $WEB_DIR/report.html

In our RHEL 7 server (, it looks like this:
You can add any information you want to that report. Add the following crontab entry to run the script every day at 1:30 PM:

30 13 * * * /root/scripts/

This article is reproduced from:

Provide the latest Linux technology tutorial books for free to do more and better for open source technology enthusiasts:

Use shell scripts to automate Linux system maintenance tasks

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.