今天給大家整理一些實用的 Linux Shell 腳本案例
希望可以幫到大家,讓大家更熟悉 shell 編程
1.顯示系統(tǒng)一些基本信息
顯示信息如下:
系統(tǒng)版本
系統(tǒng)內(nèi)核
虛擬平臺
主機名
ip地址
開機信息有沒有報錯,有的話輸出到屏幕
可以將該腳本加入到開機自啟動里面,這樣開機就會輸出基本信息
#!/bin/bash info(){ system=$(hostnamectl|grepSystem|awk'{print$3}') kernel_release=$(hostnamectl|grepKernel|awk-F:'{print$2}') Virtualization=$(hostnamectl|grepVirtualization|awk'{print$2}') server_name=$(hostname) ipaddr=$(hostname-I) echo"當(dāng)前系統(tǒng)版本是:${system}" echo"當(dāng)前系統(tǒng)內(nèi)核是:${kernel_release}" echo"當(dāng)前虛擬平臺是:${Virtualization}" echo"當(dāng)前主機名是:${server_name}" echo"當(dāng)前ip地址:${ipaddr}" } checkerrror(){ error_info=$(dmesg|greperror) if[-e${error_info}] then echo"無錯誤日志!" else ehcho${error_info} fi } info checkerrror
2.關(guān)閉系統(tǒng)防火墻和SELinux
檢查防火墻狀態(tài),是否安裝防火墻,如果安裝則關(guān)閉 關(guān)閉SELinux 清空iptables規(guī)則
#!/bin/bash close_firewalld(){ code=$(systemctlstatusfirewalld) if[${code}-eq0] then systemctlstopfirewalld fi } close_selinux(){ sed-i'/^SELINUX/s/=.*/=disabled/'/etc/selinux/config setenforce0 } close_iptables(){ iptables-F serviceiptablessave serviceiptablesrestart } close_firewalld close_selinux close_iptables
3.定時任務(wù)計劃:歸檔備份
打包壓縮/var/log/nginx目錄下所有內(nèi)容,存放在/tmp/nginx目錄里
壓縮文件命名規(guī)范:yymmdd_logs.tar.gz,只保存七天內(nèi)的文件,超過七天的文件會進(jìn)行清理
#!bin/bash date="$(date+%Y%m%d)" dir='/tmp/nginx' backupfile='yymmdd_logs.tar.gz' #查看/tmp/nginx是否存在,不存在則創(chuàng)建 checkbak(){ if[!-e${dir}] then mkdir${dir} fi } #壓縮文件 backup(){ tar-zcvf${dir}/${backupfile}/var/log/nginx/>/dev/null2>&1 echo"${backupfile}Compressed and packaged successfully !" } #清除七天過期文件 cleanup(){ find${dir}-typef-mtime+7|xagrsrm-rf if[$?-eq0] then echo"Cleaned up successfully!" else echo"datacleaningfailederror,pleasepayattentionintime" fi } checkbak backup cleanup
4.自動批量創(chuàng)建用戶
批量創(chuàng)建user1、user2、user3.....
#!/bin/bash #檢查用戶是否存在,不存在則創(chuàng)建 checkuser(){ foriin$(seq120) do iduser${i}>/dev/null2>&1 if[$?-eq0] then echo"user${i}已存在!" else useradduser${i}&&echo"user${i}"|passwd--stdinuser${i}>/dev/null2>&1 fi done } checkuser
5.通過位置參數(shù)創(chuàng)建用戶
$1 是執(zhí)行腳本的第一個參數(shù)
$2 是執(zhí)行腳本的第二個參數(shù)
#!/bin/bash checkuser(){ id${1}>/dev/null2>&1 if[$?-eq0] then echo"${1}已存在!" else useradd"$1" echo"$2"|passwd‐‐stdin"$1" fi }
6.批量刪除用戶
批量刪除user1...user20
#!/bin/bash #檢查用戶是否存在,存在則刪除 checkuser(){ foriin$(seq120) do iduser${i}>/dev/null2>&1 if[$?-eq0] then userdel-ruser${i} else echo"user${i}不存在!" fi done } checkuser
7.更新系統(tǒng)時間,并寫入硬件時間里
查看是否安裝ntpdate工具
創(chuàng)建上海時區(qū)文件的軟鏈接
更新時間并寫入到硬件時間里
#!/bin/bash package="ntpdate" info=$(rpm-q${package}) check_pkgs(){ if[!-e${info}] then echo"ntpdate already exists!" else echo"start installation!" yumcleanall>/dev/null2>&1 fi yumupdate-y&&yuminstall-y${package}>/dev/null2>&1 fi } modify_time(){ echo"開始修改時間" rm-rf/etc/localtime&&ln-s/usr/share/zoneinfo/Asia/Shanghai/etc/localtime /usr/sbin/ntpdatecn.pool.ntp.org>/dev/null2>&1&&hwclock-w } check_pkgs modify_time
8.檢查服務(wù)運行狀態(tài)
檢查某一服務(wù)是否正常運行,執(zhí)行腳本的時候第一個參數(shù)為服務(wù)名
#!/bin/bash result=$(pidof$1|wc-l) echo${result} if[${result}-eq0] then echo"service does not exist !" else echo"Service is running normally !" fi
9.對目標(biāo)主機進(jìn)行心跳檢測
ping目標(biāo)主機看是否ping得通,三次ping通表示主機正常運行
將目標(biāo)主機的ip地址作為第一個參數(shù)傳進(jìn)去
#!/bin/bash ipaddr=$1 echo${ipaddr} ping_status(){ ifping-c1${ipaddr}>/dev/null2>&1 then echo"ping${ipaddr}issuccessful!" continue fi } foriin$(seq13) do ping_status echo"ping${ipaddr}isfailure!" done
進(jìn)階版:對ip地址池里的主機分別進(jìn)行心跳檢測
ipaddr=(192.168.149.131192.168.149.130192.168.149.132192.168.149.133) foriin${ipaddr[*]} do echo"....begintoping${i}....." ifping-c3${i}>/dev/null2>&1 then echo"ping${i}issuccessful!" else echo"ping${i}isfailure!" fi done
10.系統(tǒng)磁盤內(nèi)存容量告警
根分區(qū)剩余空間小于20%(即使用空間大于80%) 輸出告警信息 內(nèi)存使用空間大于80% 輸出告警信息 配合crond每5分鐘檢查一次
#!/bin/bash disk_letfspace=$(df-Th|grep-w/|awk'{print$6}'|cut-d%-f1) mem_used=$(free-m|grepMem|awk'{print$3}') mem_total=$(free-m|grepMem|awk'{print$2}') mem_letfspace=$[${mem_used}*100/${mem_total}] if[${disk_letfspace}-gt80] then echo"Diskfreespaceislessthan20%!" else echo"${disk_letfspace}%ofdiskspaceleft" fi if[${mem_letfspace}-gt80] then echo"memoryspaceislessthan20%!" else echo"${mem_letfspace}%ofmemoryspaceleft" fi
crontab-l */5****/root/check_space.sh
審核編輯:劉清
-
Linux
+關(guān)注
關(guān)注
87文章
11345瀏覽量
210402 -
磁盤
+關(guān)注
關(guān)注
1文章
380瀏覽量
25285 -
Shell
+關(guān)注
關(guān)注
1文章
366瀏覽量
23448
原文標(biāo)題:【干貨】10個實用 Linux Shell 腳本案例
文章出處:【微信號:釋然IT雜談,微信公眾號:釋然IT雜談】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
評論