Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a shell bash script called watchdog that you can run in the background that will periodically check to see if a user listed in

Write a shell bash script called watchdog that you can run in the background that will periodically check to see if a user listed in your .friends file in your home directory is logged on.

This script should run until on of your "friends" logs on. When one of your friends logs on, say bob, your program should issue an alert and print a message like

!!! Bob is logged on!

This message should appear in the center of a blank terminal. Your script should not stop running after it finds one of your friends has logged on. However, it should not keep reporting the fact that your friend is logged on. It should report if another of your friends logs on. But each logon should be reported only once. When one of your logged on friends logs off that should be reported as well. A friend who logs on and then logs off and then logs on again should have all three occurrences reported. The process will continue to run in the background. You will have to make your own .friends file. The format is just a list of login names.

HINT: You might want to maintain two lists in your program. One for those you are watching for who have not logged on and one for those who are logged on that you ware waiting to log off.

So far, i have this bash script which i think should work, but it's giving me an error at the end of the line. I also need this script to run in the background.

DONE=0 while [ $DONE = no ] do who="$(who | sed -e 's/[[:space:]].*//' | sort -u)" #Getting the usernames of current logged in users in the system cut -f1 -d ":" /etc/passwd > user.txt #Getting all users in the system including the ones who are not online listofusers=`cat user.txt` #putting all usernames content in user file to a variable listofusers for user in $listofusers #for loop to iterate through each username in the listofusers variable do for WHO in $who #for loop to iterate through each username that is online do if [ $WHO = $user ] #if statement to check if a username that is in who (online) matches a username that is in the user variable then # which contains all of the users that exist in the system. echo "$user is logged on!!" DONE=yes #break out of the for loop and sleep for 60 seconds, and continue running again the bash script break fi done for WHO in $who #for loop to iterate through each username in the who file do if [ $user != $WHO ] #comparing usernames in the noton file to the currently user that is online then echo "$user logged off!!" #if the if statement is right then it will display that a user who was online has logged off. DONE=yes break fi done done [ $DONE = no ] && sleep 60 done

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

2. Perform services only in areas of competence

Answered: 1 week ago

Question

Differentiate between hard and soft measures of service quality.

Answered: 1 week ago

Question

Be familiar with the different perspectives of service quality.

Answered: 1 week ago