Installation and usage
A shell script refers to a program containing a series of OS commands that are executed by the shell or the command line interpreter in sequence (Techopedia, 2017). The commands in the script would otherwise have to be presented from a keyboard one by one (Allsup, 2005). However, shell scripts are not a complete replacement for programs in other languages (Seebach, 2008).
To install and use the script, one has to first give the script execution rights. To do this, open the terminal and navigate to the directory containing the script. Give the script executional rights using the command:
$ chmod +x mySysMonitor.sh
After that one can run the file.
To run the file, there are two different methods:
- Run the file manually once
- Set the file to run automatically after an interval of time
there are two different ways of doing that. First, one can run the script manually once to find the status of the system at a particular time.
Running the file once
This is done using a simple command on the terminal
$ ./mySysMonitor.sh &
The command will enable the script to run in the background
Running the file automatically
To run the file automatically, you have to use a Crontab. To do this use the following command to open the default editor
$ sudo crontab -e
After that set a cronjob for the script
The example above will schedule the script to be run every hour.
In designing the script, normal UNIX commands were used. The commands were meant to retrieve the different data required when monitoring the system these included commands to retrieve network information, devices information, currently logged in users and the current active user when the script is running.
The first task was to create the log file where the data retrieved would be written. This was done by specifying the route where the file will be and the name of the file. After that the touch command was used to create the file in the specified directory.
The next task was to retrieve the different information needed and writing them to the log file. One can the number of all the users who are currently logged into the system whether active or not and to get a list of unique users without any repetitions, the following command is used:
allUsers=$(who | sort --key=1,1 --unique | wc -l)
The current user is gotten by the command:
Therefore, to send the current user to the log file, assign the command to a variable and send the variable to the file
To view the logged in users or the current user on the terminal when running it manually, one can echo the variables
Get the current date and time and assign it to a variable.
The date and time will record the timestamp when an entry was written to the log file. The king of quotes used in the command above ensure that the date is executed to get the current timestamp and not assumed to be a string.
Then retrieve data for all disk partitions usage. After retrieving data for all the disks, loop through to get the used amount for each partition. Then write the amount for each partition into the log file.
In the recording, the hostname command is used to get the name of the computer that is being used.