Write a script named lab6s2 that writes the current time and date, lists logged-
ID: 3806547 • 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
echo -e "======================= " >>lab6s2.log
date +"%D %H:%M" >>lab6s2.log
who >>lab6s2.log
uptime >>lab6s2.log
echo -e "======================= " >>lab6s2.log
Note: once you create a script file with the above code, give the execute permission to the file.
Ex: vi lab6s2.sh
chmod +x lab6s2.sh
Note2: To add this as a crobjob and trigger for every two minutes, paste the following line in "crontab -e" command.
*/2 * * * * * /path/to/command
steps to add:
1) run the command "crontab -e". It will open the crobjob file and you press "i" to edit
2) paste the give line at the end
3) save and quit.
Your cronjob is added.
Output:
I have executed the script twice
=======================
03/29/17 03:16
naresh tty7 2017-03-29 00:08 (:0)
03:16:49 up 3:09, 1 user, load average: 0.41, 0.51, 0.53
=======================
=======================
03/29/17 03:16
naresh tty7 2017-03-29 00:08 (:0)
03:16:57 up 3:09, 1 user, load average: 0.38, 0.50, 0.53
=======================
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.