《Linux就该这么学》是由全国多名红帽架构师(RHCA)基于最新Linux系统共同编写的高质量Linux技术自学教程,极其适合用于Linux技术入门教程或讲课辅助教材。
分类: LINUX
2016-04-10 11:12:57
# mkdir scripts # cd scripts新建一个文本文件system_info.sh,在头部插入一些注释以及一些命令:
#!/bin/bash # 该脚本会返回以下这些系统信息: # -主机名称: echo -e "\e[31;43m***** HOSTNAME INFORMATION *****\e[0m" hostnamectl echo "" # -文件系统磁盘空间使用: echo -e "\e[31;43m***** FILE SYSTEM DISK SPACE USAGE *****\e[0m" df -h echo "" # -系统空闲和使用中的内存: echo -e "\e[31;43m ***** FREE AND USED MEMORY *****\e[0m" free echo "" # -系统启动时间: echo -e "\e[31;43m***** SYSTEM UPTIME AND LOAD *****\e[0m" uptime echo "" # -登录的用户: echo -e "\e[31;43m***** CURRENTLY LOGGED-IN USERS *****\e[0m" who echo "" # -使用内存最多的 5 个进程 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"然后,给脚本可执行权限,并运行脚本:
# chmod +x system_info.sh ./system_info.sh为了更好的可视化效果各部分标题都用颜色显示:
echo -e "\e[COLOR1;COLOR2m\e[0m"其中 COLOR1 和 COLOR2 是前景色和背景色,是你想用颜色显示的字符串。
#!/bin/bash
# 自动化任务示例脚本:
# -更新本地文件数据库:
echo -e "\e[4;32mUPDATING LOCAL FILE DATABASE\e[0m"
updatedb
if [ $? == 0 ]; then
echo "The local file database was updated correctly."
else
echo "The local file database was not updated correctly."
fi
echo ""
# -查找 和/或 删除有 777 权限的文件。
echo -e "\e[4;32mLOOKING FOR FILES WITH 777 PERMISSIONS\e[0m"
# 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 ""
# -文件系统使用率超过定义的阀值时发出警告
echo -e "\e[4;32mCHECKING FILE SYSTEM USAGE\e[0m"
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"
fi
done < <(df -h --total | grep -vi filesystem)
请注意该脚本最后一行两个 < 符号之间有个空格。 #!/bin/bash
# 演示使用 shell 脚本创建 HTML 报告的示例脚本
# Web directory
WEB_DIR=/var/www/html
# A little CSS and table layout to make the report look a little nicer
echo "
.titulo{font-size: 1em; color: white; background:#0863CE; padding: 0.1em 0.2em;}
table
{
border-collapse:collapse;
}
table, td, th
{
border:1px solid black;
}
" > $WEB_DIR/report.html
# View hostname and insert it at the top of the html body
HOST=$(hostname)
echo "Filesystem usage for host $HOST Last updated: $(date) " >> $WEB_DIR/report.html
# Read the output of df -h line by line
while read line; do
echo "" >> $WEB_DIR/report.html
done < <(df -h | grep -vi filesystem) echo "
| Filesystem | Size | Use % |
|---|---|---|
| " >> $WEB_DIR/report.html echo $line | awk '{print $1}' >> $WEB_DIR/report.html echo " | " >> $WEB_DIR/report.html echo $line | awk '{print $2}' >> $WEB_DIR/report.html echo " | " >> $WEB_DIR/report.html echo $line | awk '{print $5}' >> $WEB_DIR/report.html echo " |
30 13 * * * /root/scripts/filesystem_usage.sh
本文转载自:
免费提供最新Linux技术教程书籍,为开源技术爱好者努力做得更多更好: