Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

in python Write a script named lab6s2 that writes the current time and date, lis

ID: 3603513 • Letter: I

Question

in python

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.

a) Print and attach as lab6s2.txt

b) Show the crontab file you have to create to execute this script every 2 minutes…

c) Show the crontab command you executed to create this cron job

Explanation / Answer

Hi, below is the code for this program.
Unfortunately I don't currently have Linux on my system, so I can't execute this program and show you the sample outputs.
However if you face any problems executing this program, please do let me know.
Make sure you have created an empty log file 'lab6s2.log' in the current directory before you run this program.

Code:-

import time #for time.strftime() to get current date and time in required format

from datetime import timedelta #to format the uptime from seconds into more readable format

import os #to get logged-in users

tm=time.strftime("%I:%M:%S %p") #get #time in 12 hour format with am/pm

dt=time.strftime("%m/%d/%Y") #get date with padding zeroes

logFile=open("lab6s2.log","a") #open log file for appending

logFile.write(dt+" ") #date with padding zeroes and an extra space at the end

logFile.write(tm+" ") #time in 12 hour format and an extra space at the end

logFile.write(" "+os.popen('who').read()) #get logged-in users and write on next line of the log file

sysFile=open('/proc/uptime'.'r') #open uptime file for reading

uptimeInSecs=float(f.readline().split()[0]) #get uptime in seconds

uptimeReadableStr=str(timedelta(seconds=uptimeInSecs)) #convert it into a more readable string

sysFile.close() #close the uptime file

logFile.write(" "+uptimeReadableStr+" ") #write the uptime on the next line and go to the next line after writing

logFile.close() #close the log file

The Cron:

To run this script every two minutes, your crontab file should look something like this:-

*/2 * * * * <your username> python /<the path where you have saved your python program>/lab6s2.py

The first * is for minutes on the hour. So simply putting a 2 there will cause the script to run two minutes after every hour. However, putting a /2 (divided by 2) will cause the script to run every 2 minutes.

The crontab command to create a new cron job is : crontab -e

The will open the vi editor. Add the line,

*/2 * * * * <your username> python /<the path where you have saved your python program>/lab6s2.py

(after making the required changes to put your actual username and the path to the python script) to the cron file.

Save and exit (<Esc> followed by :w and :q)

Hope this helps!