Question
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started