≡ Menu

3 Unix Shell Scripts – User Activity, View Processes, Display Memory

You might find the following three Linux / Unix shell scripts helpful.

  • Display processes based on either %CPU or Memory Usage.
  • Display which user is utilizing the CPU the most.
  • Display system’s memory information – total, used and free.

1. List Processes based on %CPU and Memory Usage

This script list the processes based on %CPU and Memory usage, with out argument (by default), If you specify the argument (cpu or mem), it lists the processes based on CPU usage or memory usage.

$ 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

You can execute the above script as shown below.

$ processes.sh

$ processes.sh mem

$ processes.sh cpu

2. Display Logged in users and who is using high CPU percentage

This script displays few information about the currently logged in users and what they are doing.

$ 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. Display Total, Used and Free Memory

The following script displays the total, used and free memory space.

$ 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
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • beparas July 31, 2010, 1:09 am

    Thank you very much.
    These are very useful scripts.

  • Felix Frank August 2, 2010, 2:26 am

    Be aware that the CPU usage percentage as reported by ps is not what you may think it is. It says nothing about what the process is doing currently. Instead, it reports the ratio of time the process has spent in the “running” state vs. its “sleeping” time. So, if one of the users is running, say, a mysqld that has been doing stuff for a couple of hours, but has gone dormant some minutes ago, it will still report a high percentage of CPU usage, even though it is perfectly idle by now. If you care about current CPU usage, I’d recommend top instead of ps.

    Also, avoid gratuitous use of cat as the beginning of pipelines where it is utterly unneeded. It’s considered bad style.

    And while we’re at it, it’s a good habit to always use sort before uniq, because if the uniq input ends up being unsorted for any reason, uniq itself becomes rather pointless.

  • Bubnoff August 2, 2010, 4:01 pm

    Thanks for the yet another great article!

    I believe that you could get rid of an extra pipe in mem.sh by using awk’s built-in grep
    functionality [ tested on Debian Lenny ]:
    free -t -m | awk ‘/Swap/{ print “Total Swap space : “$2 ” MB”;
    print “Used Swap Space : “$3″ MB”;
    print “Free Swap : “$4″ MB”;

    Also, since sed takes files as arguments, one could get rid of the cats in …eg.,:
    cat /tmp/a | sed ‘1,2d’| awk ‘{print $1}’ | uniq

    Like so:
    sed ‘1,2d’ /tmp/a | awk ‘{print $1}’ | uniq

    Thanks again!

    Bubnoff

  • vikranth March 2, 2012, 9:32 am

    thanks a lot for this invaluable share of scripts..

  • Marcello August 27, 2013, 12:10 pm

    Thank you. you made a good job here.
    Best regards,
    Marcello Morettoni

  • Anonymous December 13, 2016, 6:51 am

    how can we monitor a normal users activites like what the user does e.g if he edits a file ,create a file,delete and running some services etc.