Use Shell scripts to automate Linux maintenance tasks
If a system administrator spends a lot of time solving problems and doing repetitive work, you should doubt whether this is correct. An efficient system administrator should make a plan to spend as little time as possible to do repetitive work. So although it seems that he didn't do a lot of work, it is because shell scripts help him complete most of the tasks, which is what we will discuss.
What is shell script?Simply put, shell scripts are a Program executed step by shell, and shell is another program that provides interfaces 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 informationFirst, let's create a directory to save our shell script:
# mkdir scripts# cd scripts
Create a text file system_info.sh and insert some comments and commands in the header:
#! /Bin/bash # The script returns the following system information: #-Host Name: echo-e "\ e [31; 43 m ****** hostname information ***** \ e [0 m "hostnamectlecho" #-Use of the file system disk space: echo-e "\ e [31; 43 m ***** file system disk space usage ***** \ e [0 m "df-hecho" #-memory in idle and in use: echo-e "\ e [31; 43 m ****** free and used memory ****** \ e [0 m" freeecho "" #-system startup time: echo-e "\ e [31; 43 m ****** system uptime and load ***** \ e [0 m" uptimeecho "" #-Login User: echo-e "\ e [31; 43 m ***** currently logged-in users ****** \ e [0 m "whoecho" "#-five processes with the most memory used echo-e "\ e [31; 43 m ***** TOP 5 MEMORY-consuming processes ***** \ e [0 m "ps-eo % mem, % cpu, comm -- sort =-% mem | head-n 6 echo "" echo-e "\ e [1; 32mDone. \ e [0 m"
Then, give the script executable permission and run the script:
# chmod +x system_info.sh./system_info.sh
For better visual effects, titles of all parts are displayed in color:
The color function is provided by the following command:
echo -e "\e[COLOR1;COLOR2m\e[0m"
Among them, COLOR1 and COLOR2 are the foreground color and background color, and are the strings you want to display in color.
Automate tasksThe tasks you want to automate may vary depending on the situation. Therefore, we cannot cover all possible scenarios in an article, but we will introduce three typical tasks that can be automated by using shell scripts:
1) Update the local file database
1) Find (or delete) files with 777 Permissions
2) When the file system exceeds the defined threshold value, a warning is issued.
Let's create a new file named auto_tasks.sh in the script directory and add the following content:
#! /Bin/bash # sample script for automated tasks: #-update local file database: echo-e "\ e [4; 32 mUPDATING local file database \ e [0 m "updatedbif [$? = 0]; then echo "The local file database was updated correctly. "else echo" The local file database was not updated correctly. "fiecho" #-find and/or delete files with 777 permissions. Echo-e "\ e [4; 32 mLOOKING for files with 777 PERMISSIONS \ e [0 m" # 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 Alibaba ss systems. find-type f-perm 0777-exec rm-I {}+; echo "" #-issue when the file system usage exceeds the defined threshold Warning echo-e "\ e [4; 32 mCHECKING file system usage \ e [0 m" THRESHOLD = 30 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. USAGE =$ {PERCENTAGE % ?} If [$ USAGE-gt $ THRESHOLD]; then echo "The remaining available space in $ FILESYSTEM is critically low. used: $ PERCENTAGE "fidone <(df-h -- total | grep-vi filesystem)
Note that there is a space between the two <symbols in the last line of the script.
Use CronTo further improve efficiency, you don't just want to sit in front of your computer and manually execute these scripts. Instead, you will use cron to schedule periodic execution of these tasks and send the results to pre-specified recipients by email, or save them to files that can be viewed in a web browser.
The following script (filesystem_usage.sh) runs the famous df-h command to format and output the file to an HTML table and save it to the report.html file:
#! /Bin/bash # demo script for creating HTML reports using shell scripts # Web directoryWEB_DIR =/var/www/html # A little CSS and table layout to make the report look a little nicerecho "<HTML> <HEAD> <style>. titulo {font-size: 1em; color: white; background: # 0863CE; padding: 0.1em 0.2em;} table {border-collapse: collapse;} table, td, th {border: 1px solid black ;}</style> <meta http-equiv = 'content-type' Content = 'text/html; charset = UTF-8 '/> </HEAD> <BODY> "> $ WEB_DIR/report.html # View hostname and insert it at the top of the html bodyHOST = $ (hostname) echo "Filesystem usage for host <strong> $ HOST </strong> <br> Last updated: <strong >$ (date) </strong> <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 linewhile read line; doecho "<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 (192.168.0.18), it looks like the following:
You can add any information you want to the report. Add the following crontab entries and run the script at every afternoon:
30 13 * * * /root/scripts/filesystem_usage.sh
Original article address: http://www.tecmint.com/using-shell-script-to-automate-linux...author: Gabriel cánepa
Reposted at: https://linux.cn/article-6526-1.html :ictlyh
This article Reprinted from: http://www.linuxprobe.com/shell-automation-maintenance-tasks/
Free to provide the latest Linux technology tutorial books, for open source technology enthusiasts to do more and better: http://www.linuxprobe.com/