Bash script berikut akan menampilkan total memori yang digunakan dan yang tidak digunakan.
Buat file menggunakan editor dan simpan dengan nama mem.sh
Berikut source code-nya:
#! /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";
}'
Jalankan file : ./mem.sh
$ ./mem.sh
Memory Space Details
Total Memory space : 1000 MB
Used Memory Space : 31 MB
Free Memory : 968 MB
Swap memory Details
Total Swap space : 0 MB
Used Swap Space : 0 MB
Free Swap : 0 MB
Bash script ini berfungsi untuk menampilkan sedikit informasi mengenai user-user yang baru saja login dan apa yang mereka lakukan.
Source code : 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
Jalankan : ./loggedin.sh
Total number of unique users logged in currently
1
List of unique users logged in currently
tux-kecil
The user who is using high %cpu
0.00s tux-kecil ttyp1 118.96.149.36 05:38 0.00s 0.08s 0.00s /bin/bash ./log
List of users logged in and what they are doing
09:21:19 up 2 days, 23:37, 1 user, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
tux-kecil ttyp1 118.96.149.36 05:38 0.00s 0.08s 0.00s /bin/bash ./log
Bash scripts berikut digunakan untuk mengetahui daftar proses dalam CPU dan penggunaan memori
$ pico processes.sh
#! /bin/bash
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
Sebelum menjalankan scripts tersebut, patikan Anda mempunyai hak akses exceute (chmod +x). Cara menjalankan script tersebut sebagai berikut:
Read more…
Recent Comments