1. Engineering
  2. Computer Science
  3. c or c write a shell script utility called psmonitor...

Question: c or c write a shell script utility called psmonitor...

Question details

C OR C++

Write a shell s­­­­­­cript utility called psmonitor that displays the list of all the processes running in the system, once every tseconds for count number of times.

psmonitor should repeatedly scan the system process table, displaying the list of processes that are running in the system. We can discuss how you can display all processes running in the system in the appropriate forum.

The usage of psmonitor is:

$ psmonitor [-t tseconds] [-n count]

Anything enclosed in square brackets is optional. Here's what each field means:

tseconds:

Tells psmonitor how many seconds to wait between each scan of the system process table. The default value is one.

count:


Tells psmonitor how many times the system process table should be scanned before quitting. The default value is five.

You should choose your favorite shell scripts (bash, ksh, csh, …) to implement this program. You need to use your favorite editor (pico, emacs, vi, …)to create and save your program. Your program should set the count and tseconds parameters appropriately (look up how arguments are set in your script, $#, $1, $2, … are used in some scripts). Then, Go through a loop for count iterations. During each iteration psmonitor displays the date (which includes time), lists all the current processes running (PS), and sleeps for tseconds. At termination, your program should display "Program is written by YOUR NAME".

For example:

$ psmonitor -t 10 -n 100

Would iterate 100 times at 10 second intervals.

Error Checking:

Your program should check for simple errors such as missing options (ie.$ psmonitor –n”, where -n option is used but count is missing). If an error is detected, display an appropriate message and quit. Most utilities present a usage reminder when an ill-formed command line is entered. For example the following error message is appropriate for the following command:

$ psmonitor -n

Missing Count Parameter
Usage: psmonitor [-t tseconds] [-n count]

Note:

Normally you'd execute psmonitor in the background and redirect its output into a file.

Handling Interrupts:

If your shell is interrupted (by a Ctrl-C for example), you want to exit the program by displaying an appropriate message, so ensure that interrupts are handled appropriately. You will find the built-in C shell command onintr or trap for bash very useful for this purpose.

Commands you may have to use:

Using your Linux book or man page check to see how the following commands are being used in your favorite script.  

  • Echo
  • Date
  • Arguments and number of arguments passed to a program
  • Setting variables and increment/decrementing variables
  • If statements
  • Loops (while) statements
  • Interrupts (traps)
  • Chmod

Executing your script:

  • First line of your program should be a directive, indicating which shell you are using. Example: if I am using bash, the first line of my program should be "/bin/bash".
  • Using chmod make your file executable.
  • Use PSMonitor like any command.
Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution