HW 4 - CS 480 M01 Fall 2022
CS 480 M01 Fall 2022 - Homework Assignment #4
Due: 11:59 PM, Tuesday, Oct 4 2022
Assignment:
Points: 95 points total
- 15 pts : Read all man pages listed below (from sections 1 , 5, or 8; try the commands, look into the files, ... while reading) and in a short paragraph (3-5 lines per man page in your words - do not just copy parts of the man page) describe what is each of them for. Also, for the commands choose an extra option (not the help nor version options) and in few more lines describe situations in which you would use the selected option. In case of the commands with long man pages and many options choose at least two extra options and describe situations in which you would use them.
- at
- cron
- crontab ( config file )
- date
- journalctl
- ps
- sleep
- systemctl
- systemd
- systemd.service
- trap
- uptime
- w
Each exercise below is to be implemented on your cs480 virtual host.
Every question asked needs to be answered in your assignment report.
For every exercise below describe in sufficient detail the whole process and explain what you did to accomplish the task.
Make sure that you list and discuss all important steps made, commands used together with their options, and problems encountered.
- 7 pts
- Create a simple shell script state.sh
that will append info listed below into /tmp/state.log file:
- current date and time
- current load and users logged in
- all processes running on the machine (long listing that, among other info, includes PID and PPID of each process )
- Create a cron job that will execute this script every 5 minutes (the state info gets appended
into file /tmp/state.log every five minutes)
- 7 pts
Create a new script movelog.sh and another crontab entry so that
- The movelog.sh script will
- move the /tmp/state.log file into file named statelog.YYYYMMDD where YYYYMMDD
is the current date (statelog.20220930, statelog.20221001, ...) and
- removes old statelog.YYYYMMDD files that are 2 or more weeks old
- each statelog.YYYMMDD file has the state info (date, users, processes) stored every
5 minutes for the whole day (from midnight to the next midnight)
- 3 pts: Install GNOME
yast -> Software mngmt -> Filter : Patterns -> and add the GNOME Desktop Environment (X11)
- 3 pts: Without using yast (you cannot use yast/yast2) modify your system to have the graphical interface as the default target.
- 15 pts: Write a simple Bash shell script /root/trap.sh which will
- Write its process ID into the file
/var/run/trap.pid
.
- Append one line of a text into the file
/root/trap.log
.
The line will include the shell's current name and process ID followed by the current date as shown here:
./trap.sh [4441] started on Mon Sep 26 10:04:54 MDT 2022
- Trap all these signals: INT, QUIT, HUP, TERM , USR1, USR2 and write a message about it into the log file. Also,
- Once the TERM signal is received the shell will remove the /var/run/trap.pid and exit.
- The shell will continue to run when the other signals are received.
- Append the messsage
"./trap.sh [PID] time tick ... DATE"
into the /root/trap.log
every 10 seconds.
The DATE and PID are to be the actual current date and process ID of the running process.
Example of the log file content:
# cat /root/trap.log
./trap.sh [2984] started on Mon Sep 26 12:20:41 MDT 2022
./trap.sh [2984] time tick ... Mon Sep 26 12:20:51 MDT 2022
./trap.sh [2984] time tick ... Mon Sep 26 12:21:01 MDT 2022
./trap.sh [2984] time tick ... Mon Sep 26 12:21:11 MDT 2022
./trap.sh [2984] time tick ... Mon Sep 26 12:21:21 MDT 2022
./trap.sh [2984] INT received on Mon Sep 26 12:21:31 MDT 2022
./trap.sh [2984] time tick ... Mon Sep 26 12:21:31 MDT 2022
./trap.sh [2984] INT received on Mon Sep 26 12:21:38 MDT 2022
./trap.sh [2984] time tick ... Mon Sep 26 12:21:38 MDT 2022
./trap.sh [2984] time tick ... Mon Sep 26 12:21:48 MDT 2022
./trap.sh [2984] time tick ... Mon Sep 26 12:21:58 MDT 2022
./trap.sh [2984] QUIT received on Mon Sep 26 12:22:08 MDT 2022
./trap.sh [2984] time tick ... Mon Sep 26 12:22:08 MDT 2022
./trap.sh [2984] USR1 received on Mon Sep 26 12:22:18 MDT 2022
./trap.sh [2984] time tick ... Mon Sep 26 12:22:18 MDT 2022
./trap.sh [2984] TERM received - exiting ... Mon Sep 26 12:22:28 MDT 2022
Here is an example of a script that uses trap :
- 10 pts: Create and configure new "service" trap_test.service that will excute the trap.sh.
Create the /etc/systemd/system/trap_test.service unit file and use systemctl
command to add the service among services in the graphical and multi-user targets.
The command systemctl will start and stop the "service"
(
systemctl start trap_test.service
and systemctl stop trap_test.service
).
- 20 points
Write a shell (/bin/sh) script
check_arg.sh [ -i | -n ] USER
which checks its options / arguments and prints out the result of this check.
If the check fails then the usage message is also printed.
Make sure that the script follows the best practices including that it provides meaningful exit code (0=success, nonzero=failure).
-i and -n are optional but only one of these can be submitted (cannot be used together) and it has to be on the line prior to any user name
-i means interactive mode (this is the default mode if none is specified)
-n means non-interactive mode
USER parameter is mandatory but can repeat (more then one user names may be specified)
Script prints out if it runs in interactive or in non-interactive mode.
It also checks all user names and makes sure that every user name matches this regular expression: '^[a-z][a-z0-9_]*$'
(Write and execute the script on your cs480 virtual host and submit a transcript of the session showing your script responding correctly to all of the tests as shown in the example below.)
Example:
shell_prompt> ./check_arg.sh
Please provide correct arguments and options
Usage: ./check_arg.sh [-i|-n] USERS
Options (optional): -i or -n for interactive or non-interactive execution
Arguments: list of user names
Every user name must match this regular expression: '^[a-z][a-z0-9_]*$'
shell_prompt> echo $?
1
/shell_prompt> ./check_arg.sh -i joe
Otions and arguments okay
running in interactive mode
shell_prompt> echo $?
0
shell_prompt> ./check_arg.sh -n 2badusername
ERR: option after an argument or wrong argument or wrong option 2: 2badusername
Please provide correct arguments and options
Usage: ./check_arg.sh [-i|-n] USERS
Options (optional): -i or -n for interactive or non-interactive execution
Arguments: list of user names
Every user name must match this regular expression: '^[a-z][a-z0-9_]*$'
shell_prompt> echo $?
1
shell_prompt> ./check_arg.sh joeokaybut -i
ERR: option after an argument or wrong argument or wrong option 2: -i
Please provide correct arguments and options
Usage: ./check_arg.sh [-i|-n] USERS
Options (optional): -i or -n for interactive or non-interactive execution
Arguments: list of user names
Every user name must match this regular expression: '^[a-z][a-z0-9_]*$'
shell_prompt> echo $?
1
shell_prompt> ./check_arg.sh -n jim joe bob
Otions and arguments okay
running in NON-interactive mode
shell_prompt> echo $?
0
shell_prompt> ./check_arg.sh -n -i bob
ERR: option after an argument or wrong argument or wrong option 2: -i
Please provide correct arguments and options
Usage: ./check_arg.sh [-i|-n] USERS
Options (optional): -i or -n for interactive or non-interactive execution
Arguments: list of user names
Every user name must match this regular expression: '^[a-z][a-z0-9_]*$'
shell_prompt> echo $?
1
shell_prompt>
NOTE: Your solutions to all of the exercises above must be implemented, tested, and execute correctly on your cs480 virtual machine. Among your attachments (as part of the tar.bz2 file) submit also transcript files showing the correct execution of the commands / scripts as you executed them on your cs480 virtual machine. You can use the script command to record the session and submit the resulting files (4.4_trascript.txt, ...) with your homework assignment as part of your bz2 file submited into your directory under /home/CS480.
Submitted files, overall quality, length, and feedback:
- 5 pts : Submit all relevant files you modified during the lab (into the /home/CS480/yourcsloginname directory as part of the tar.bz2 file) . Exactly two files need to be submitted: One file being your report in PDF is to be submitted on the Blackboard. The other file is the tar.bz2 file containing everything else and it gets submitted into /home/CS480 (Pack the files together using tar and bzip2, and copy them using scp from your computer into your cs account )
- 10 pts - Submit your report as a PDF document through learn.nmsu.edu and all supporting documents, programs, and execution transcripts as part of the tar.bz2 file in your directory under /home/CS480. The report has to contain answers to all questions above, and for the practical problems on your computer describe in detail choices you made, difficulties you encountered, and how did you deal with them.
- 5 pts - Feedback:In the last paragraph of your report estimate how much time you spent on this assignment. Describe what was hard, easy, interesting, boring or confusing.
Please remember the submission requirements:
Two files need to be submitted for every assignment :
- The first file is your typed report .
It has to be submitted through http://learn.nmsu.edu as one file / document and it has to be in the PDF format.
No other format will be accepted. The name of the file has to contain your name and assignment/lab number separated by "_" (If your name is John Smith then the name of the document submitted for this assignment needs to be John_Smith_HW04.pdf).
- The second file to be submitted into your directory under /home/CS480 directory has to be a tar.bz2 file that contains all other files related to the assignment.
It will contain all relevant files that you modified for the assignment. Everything that was done to finish the lab, modify your system, write, run and verify submitted programs and scripts used needs to be included in the submitted tar.bz2 file. That includes all your system configuration files modified for the lab/assignment as well as programs and scripts written (including complete source code, supporting shell scripts, test data input files, ...). The name of the tar.bz2 file has to contain your CS login name and the lab number separated by "_" (if your cs login name was jsmith then the file's name for this assignment will be jsmith_Lab04.tar.bz2 ). The files submitted as part of the tar.bz2 file have to be exact copies as found on your cs480 lab system.
If you want to correct or add something to already submitted assignment than you need to do so before the deadline and resubmit the whole report again and also resubmit your new tar.bz2 file .
Please remember that late submissions are penalized 20% / day and NO submissions that are late 3 or more days will be accepted. You can be late at most 2 days to receive any points.