[详 细 介 绍] [详 细 介 绍] [详 细 介 绍] [详 细 介 绍] [详 细 介 绍]

The day before yesterday, there was a reader in the group who shared some examples of Shell scripts, so I would like to write them to consolidate the basic knowledge.

1. Obtain 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 completion.

` #! Bin /bash ' '# Separated by Spaces ` ` 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 -F':' '{print $1}')` `ssh $host "top -b -n 1"`Copy the code

2. Count the number of Linux processes under /proc, output the total number of processes, running processes, stoped processes, sleeing processes, 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"`Copy the code

3. Change the suffix of all “.sh” files in the current directory (including subdirectories) to “.shell” and 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 -F'.sh' '{print $1}')` `new_filename="${filename}.shell"` `mv "$file" "$new_filename"` `sed -i '2d' "$new_filename"`  `done`Copy the code

4. Check whether the/TMP /jstack directory exists. If no, create a directory.

The inceptor Server jStack information is printed every hour and the file is named jstack_${current time}. The oldest file is deleted when the directory has more than 10 files.

` #! /bin/bash` `DIRPATH='/tmp/jstack'` `CURRENT_TIME=$(date +'%F'-'%H:%M:%S')` `if [ ! -d "$DIRPATH" ]; then` `mkdir "$DIRPATH"` `else` `rm -rf "$DIRPATH"/*` `fi` `cd "$DIRPATH"` `while true` `do` `sleep 3600` `# Here need to inceptor change after your own 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`Copy the code

5. Collect all gc information logs of the current day from test.log and collect the average gc time and the longest GC 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 ($1+0 > max+0) max=$1} END {print "Max: ", max}'>>capture_hive_log.log`Copy the code

6. Search 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 yes, output a system activity report to alert. TXT.

` #! /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 500 ]; then` `sar -A > alert.txt` `state="false"` `else` `sleep 6` `continue` `fi` `done`Copy the code

7. Move files larger than 10 KB from the current directory to the/TMP directory and output file names in ascending order.

` #! / bin/bash ` ` # target directory ` ` DIRPATH = '/ TMP ` ` # directory ` ` FILEPATH ='. '` ` find "$FILEPATH" -type f | - size + 10 k xargs -i mv {} "$DIRPATH"` `ls -lS "$DIRPATH" | awk '{if(NR>1) print $NF}'`Copy the code

So there you have it: 7 practical Shell scripts.

I hope you can apply what you have learned through these cases and apply it in combination with your own actual scenes, so as to improve your work efficiency.

Original is not easy, code word is not easy. If you think this article is useful to you, please give it a thumbs up, leave a comment or forward it, because this will be my motivation to output more high-quality articles, thank you!