Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the practical Shell examples in linux

2025-03-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail about the practical examples of Shell in linux. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

1. Get the hostname from several machines concurrently, record the time it takes to return the information, redirect it to a file hostname.txt, and output the CPU information of the machine that takes the shortest time after all.

#! bin/bash # so the host Separate ALL_HOSTS= (IP address IP address) for host in ${ALL_HOSTS [*]} do {start_time=$ (date +'% s') ssh $host "hostname" & > / dev/null sleep 2 stop_time=$ (date +'% s') time_consuming=$ ((stop_time-start_time)) echo "$host: $time_consuming" > hostname.txt} & done wait host=$ ( Sort-n-k 2 hostname.txt | head-1 | awk-fanglu'{print $1}') ssh $host "top-b-n 1"

2. Count the number of Linux processes under the / proc category, and output the total number of processes, the number of running processes, the number of stoped processes, the number of sleeing processes, and the number of zombie processes.

Output all zombie processes to zombie.txt to kill all zombie processes.

#! / bin/bash ALL_PROCESS=$ (ls / proc/ | egrep'[0-9] +') running_count=0 stoped_count=0 sleeping_count=0 zombie_count=0 for pid in ${ALL_PROCESS [*]} do test-f / proc/$pid/status & & state=$ (egrep "State" / proc/$pid/status | awk'{print $2}') case "$state" in R) running_count=$ ((running_count+1)) T) stoped_count=$ ((stoped_count+1)); S) sleeping_count=$ ((sleeping_count+1)); Z) zombie_count=$ ((zombie_count+1)) echo "$pid" > > zombie.txt kill-9 "$pid" Esac done echo-e "total: $((running_count+stoped_count+sleeping_count+zombie_count))\ nrunning: $running_count\ nstoped: $stoped_count\ nsleeping: $sleeping_count\ nzombie: $zombie_count"

3. Change the suffix of all files under the current directory (including subdirectories) with the suffix ".sh" to ".shell", and then delete the second line of each file.

#! / bin/bash ALL_SH_FILE=$ (find. -type f-name "* .sh") for file in ${ALL_SH_FILE [*]} do filename=$ (echo $file | awk-Found.sh' {print $1}') new_filename= "${filename} .shell" mv "$file"$new_filename" sed-I '2d' "$new_filename" done

4. Determine whether the directory / tmp/jstack exists, create a new directory if it does not exist, and delete all the contents under the directory if it exists.

Print the jstack information of inceptor server every 1 hour and name the file with jstack_$ {current time}. Whenever there are more than 10 files in the directory, delete the oldest file.

#! / bin/bash DIRPATH='/tmp/jstack' CURRENT_TIME=$ (date +'% Flemish colors% HRV% MRV% S') if [!-d "$DIRPATH"] Then mkdir "$DIRPATH" else rm-rf "$DIRPATH" / * fi cd "$DIRPATH" while true do sleep 3600 # here you need to change your java process name pid=$ (ps-ef | grep 'inceptor' | grep-v grep | awk' {print $2}') jstack $pid > > "jstack_$ {CURRENT_TIME}" dir_count=$ (ls | wc-l) if ["$dir_count"-gt 10] Then rm-f $(ls-tr | head-1) fi done

5. Intercept all gc information logs for the day from test.log, and count the average gc time and the longest time.

#! / bin/bash awk'{print $2} 'hive-server2.log | tr-d':'| awk'{sum+=$1} END {print "avg:", sum/NR}'> capture_hive_log.log awk'{print $2} 'hive-server2.log | tr-d':'| awk'{max= 0} {if ($1x 0 > max+0) max=$1} END {print "Max:", max}'> capture_hive_log.log

6. Look for the top 20 IP addresses with the highest number of requests on port 80, and determine whether the minimum number of requests in the middle is greater than 500. if so, the output system activity will be reported to alert.txt. If not, try again after 600s until there is an output.

#! / bin/bash state= "true" while $state do SMALL_REQUESTS=$ (netstat-ant | awk-F'[:] +'/: 22 / {count [$4] + +} END {for (ip in count) print count [ip]}'| sort-n | head-20 | head-1) if ["$SMALL_REQUESTS"-gt 6 continue fi done; then sar-A > alert.txt state= "false" else sleep 6 continue fi done

7. Transfer the files greater than 10K under the current directory to the / tmp directory, and then output the file name from large to small according to the order of file size.

#! / bin/bash # Target directory DIRPATH='/tmp' # View directory FILEPATH='.' Find "$FILEPATH"-size + 10k-type f | xargs-I mv {} "$DIRPATH" ls-lS "$DIRPATH" | awk'{if (NR > 1) print $NF} 'this is the end of the article on "what are the practical Shell examples in linux". I hope the above content can be helpful to you so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report