Write a script named lab6s2 that writes the current time and date, lists logged-
ID: 3808593 • Letter: W
Question
Write a script named lab6s2 that writes the current time and date, lists logged-in users, and prints the system’s uptime to a log file. The time should be printed with the format of “03:55 am” as opposed to military time. The date should be printed like this: “09/10/12” Numbers should be zero-padded to 2 digits (e.g. 03 instead of 3).
(It is advisable to look at the date command’s many options to format your output. Also, use the uptime command to print system uptime.)
This output should be appended to a logfile named “lab6s2.log”. Create a cron job to execute this script every 2 minutes.
Explanation / Answer
#!/bin/bash
while true; do
# Writing current date and time to the file
date '+%d/%m/%y %I:%M %p' >> lab6s2.log;
# writing uptime to the file. Formatting output of "uptime command"
printf "uptime : ">> lab6s2.log;
uptime | sed -E 's/^[^,]*up *//; s/, *[[:digit:]]* users.*//; s/min/minutes/; s/([[:digit:]]+):0?([[:digit:]]+)/ hours, minutes/'>>lab6s2.log;
#Get a list of logged in users
printf "logged-in users " >> lab6s2.log;
who | cut -d' ' -f1 | sort | uniq >> lab6s2.log;
printf " " >> lab6s2.log;
#sleep for two minutes and repeat the loop
sleep 120; done
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.