GNU/Linux >> Linux Esercitazione >  >> Linux

3 Script della shell Unix:attività dell'utente, processi di visualizzazione, memoria di visualizzazione

Potresti trovare utili i seguenti tre script di shell Linux/Unix.

  • Visualizza i processi in base alla %CPU o all'utilizzo della memoria.
  • Mostra quale utente utilizza maggiormente la CPU.
  • Visualizza le informazioni sulla memoria del sistema:totale, utilizzata e gratuita.

1. Elenca i processi in base alla %CPU e all'utilizzo della memoria

Questo script elenca i processi basati su %CPU e sull'utilizzo della memoria, senza argomento (per impostazione predefinita). Se specifichi l'argomento (cpu o mem), elenca i processi in base all'utilizzo della CPU o dell'utilizzo della memoria.

$ cat processes.sh
#! /bin/bash
#List processes based on %cpu and memory usage

echo "Start Time" `date`
# By default, it display the list of processes based on the cpu and memory usage #
if [ $# -eq 0 ]
then

	echo "List of processes based on the %cpu Usage"
	ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu  # sorted based on %cpu
	echo "List of processes based on the memory Usage"
	ps -e -orss=,args= | sort -b -k1,1n # sorted bases rss value

# If arguements are given (mem/cpu)
else
	case "$1" in
	mem)
	 echo "List of processes based on the memory Usage"
 	 ps -e -orss=,args= | sort -b -k1,1n
	 ;;
 	cpu)
	 echo "List of processes based on the %cpu Usage"
	 ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu
	 ;;
 	*)
		echo "Invalid Argument Given \n"
		echo "Usage : $0 mem/cpu"
		exit 1
 	esac	

fi
echo "End Time" `date`
exit 0

Puoi eseguire lo script sopra come mostrato di seguito.

$ processes.sh

$ processes.sh mem

$ processes.sh cpu

2. Display Utenti che hanno effettuato l'accesso e che utilizzano un'elevata percentuale di CPU

Questo script mostra poche informazioni sugli utenti attualmente connessi e su cosa stanno facendo.

$ cat loggedin.sh
#! /bin/bash

w > /tmp/a

echo "Total number of unique users logged in currently"
cat /tmp/a|  sed '1,2d' | awk '{print $1}' | uniq | wc -l
echo ""

echo "List of unique users logged in currently"
cat /tmp/a | sed '1,2d'|  awk '{print $1}' | uniq
echo ""

echo "The user who is using high %cpu"
cat /tmp/a | sed '1,2d' | awk   '$7 > maxuid { maxuid=$7; maxline=$0 }; END { print maxuid, maxline }' 

echo ""
echo "List of users logged in and what they are doing"
cat /tmp/a
$ ./loggedin.sh
Total number of unique users logged in currently
4

List of unique users logged in currently
john
david
raj
reshma

The user who is using high %cpu
0.99s reshma   pts/5    192.168.2.1  15:26    3:01   1.02s  0.99s custom-download.sh

List of users logged in and what they are doing
 15:53:55 up 230 days,  2:38,  7 users,  load average: 0.19, 0.26, 0.24
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
john     pts/1    192.168.2.9   14:25    1:28m  0.03s  0.03s -bash
john     pts/2    192.168.2.9   14:41    1:11m  0.03s  0.03s -bash
raj      pts/0    192.168.2.6   15:07    9:08   0.11s  0.02s -bash
raj      pts/3    192.168.2.6   15:19    29:29  0.02s  0.02s -bash
john     pts/4    192.168.2.91  15:25    13:47  0.22s  0.20s vim error_log
reshma   pts/5    192.168.2.1   15:26    3:01   1.02s  0.99s custom-download.sh

3. Visualizza memoria totale, utilizzata e libera

Lo script seguente mostra lo spazio di memoria totale, utilizzato e libero.

$ cat mem.sh
#! /bin/bash

# Total memory space details

echo "Memory Space Details"
free -t -m | grep "Total" | awk '{ print "Total Memory space : "$2 " MB";
print "Used Memory Space : "$3" MB";
print "Free Memory : "$4" MB";
}'

echo "Swap memory Details"
free -t -m | grep "Swap" | awk '{ print "Total Swap space : "$2 " MB";
print "Used Swap Space : "$3" MB";
print "Free Swap : "$4" MB";
}'
$ ./mem.sh
Memory Space Details
Total Memory space : 4364 MB
Used Memory Space : 451 MB
Free Memory : 3912 MB
Swap memory Details
Total Swap space : 2421 MB
Used Swap Space : 0 MB
Free Swap : 2421 MB

Linux
  1. Consenti Setuid sugli script della shell?

  2. Array associativi negli script della shell?

  3. UNIX/Linux:cos'è una shell? Cosa sono le diverse conchiglie?

  4. Qual è il comando *nix per visualizzare la shell di login predefinita di un utente

  5. Eseguire uno script di shell come utente diverso

Array negli script di shell

Come utilizzare if-else negli script della shell?

Il ciclo while negli script della shell

Posso registrare l'attività di vi?

Ordinamento dei processi in base all'utilizzo della memoria

Esecuzione di un comando come utente nologin