Sponsored Content
Top Forums Shell Programming and Scripting Write a scripts to kill idle user for 60 min. & email user list to admin in text file Post 302510301 by guto on Saturday 2nd of April 2011 10:05:39 PM
Old 04-02-2011
sumit30,
if I was you, based on the output of 'whoidle', I would probably lunch it with the option to get all the idle users in the system and save it to a file within the same directory with the script. supposing the option is '-a', it would something like this: 'whoidle -a' >ALL_IDLE_USERS

this code has not been tested on an AIX system. I tested it on my laptop using the sample data you supplied and it's working like a charm.
Code:
------------------------beginning script-------------------------
DATE=`date +%D`
TIME=`date +%T`
HNAME=`uname -n`
LFILE=$(uname -n)_Idle_Users_$(date +%h)_$(date +%d)_$(date +%y).txt
SUBJECT="$HNAME Idle User List"
EMAIL="admin@yourdomain.com"
HEAD=`cat IDLE_USERS |sed q`
cat ALL_IDLE_USERS |sed '1d' |egrep -v "root|daemon|bin|sys|adm|nobody|abc|pqr" >IDLE_USERS
IDL=`cat IDLE_USERS |awk '{ print $6}'`
echo  -e "---------------------------------------------- \nDATE: $DATE\t\t  TIME:  $TIME\n-----------------------------------------------------------------------------------------"  > $LFILE
echo -e "$HEAD\t\tStatus" >> $LFILE

for DL in $IDL
  do
    H=`echo $DL |cut -d: -f1`; M=` echo $DL |cut -d: -f2`
    if [ $H -gt 0 ] || [ $M -eq 60 ]; then
       kill -15 $PID;
       CLINE=`grep -w $DL ALL_IDLE_USERS`
       RUNNING=`ps -p $PID |sed '1d'`
       if [ "$RUNING" == "" ]; then
          echo -e "$CLINE \tKilled" >> $LFILE
       else
          echo -e "$CLINE \tRunning" >> $LFILE
       fi
    fi
done
echo -e "-----------------------------------------------------------------------------------------" >> $LFILE

if [ -s $LFILE ]; then
   mail -s $SUBJECT $EMAIL < $LFILE
   touch $LFILE.txt
fi
----------------end script---------------

this is an elegant way of resolving your problem without the need for any complications: such as the 8th point.
in my opinion 8th point isn't necessary. thus, the code that I supplied to you the other time isn't needed because you are not dealing with a file or a variable that must continue growing.
for instance, if you were picking the idle users from a certain system-log-file which must continue growing as the system is running and logging various events, then you would need to have a way to loop through that file as long as it continues to increase. but your problem is much simple based on what I perceive from your explanation.
as far as I know, the technique that intend to use on point 8 is normally used to monitor specific regexs in a log-files.
NEXT TIME, I'M GONNA CHARGE YOU A FEE OR SOMETHING! lol; I am at home for over a year. I need to get paid....

Last edited by DukeNuke2; 04-03-2011 at 03:09 AM..
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

user & group read/write access question

folks; I created a new users on my SUSE box and i need to give this user/group a read write access to one specific folder. here's the details: - I created new user "funny" under group "users". - I need to give this user "funny" a read/write access to another directory that is owned by "root".... (3 Replies)
Discussion started by: Katkota
3 Replies

2. UNIX for Dummies Questions & Answers

Difference between : Locked User Account & Disabled User Accounts in Linux ?

Thanks AVKlinux (3 Replies)
Discussion started by: avklinux
3 Replies

3. UNIX for Advanced & Expert Users

Send email as a different user than the user logged in

Hi I am using mailx to send email and am wondering if there is a way I can send the email from a different user than the user logged in. something like do-not-reply@xyz.com Thank you. (1 Reply)
Discussion started by: rakeshou
1 Replies

4. Shell Programming and Scripting

How to kill process after x idle min?

I need a script to kill those process id whose idle time is more than 30min plz help me (3 Replies)
Discussion started by: salil2012
3 Replies

5. Homework & Coursework Questions

[Scripting]Find & replace using user input then replacing text after

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: (o) Checkout an auto part: should prompt the user for the name of the auto part and borrower's name: Name:... (2 Replies)
Discussion started by: SlapnutsGT
2 Replies

6. Shell Programming and Scripting

User idle time and kill

Hello.. I have many sleepy users on my Solaris box and need to kill them if they are idle for more than 45 minutes for example...I know who -u gives and the idle time but unable to make a awk line to get the condition perfect. Please help...:wall: (9 Replies)
Discussion started by: wimaxpole
9 Replies

7. Shell Programming and Scripting

Script to Send Email to a user when an admin kills process

Can anyone create or give me a script that I can use to email a user automatically when I kill one of their processes? Or Can you give me a script to allow me to email a user (entering email manually) when a process is killed? Like showing what the PID was and a reason the admin killed it? Is... (2 Replies)
Discussion started by: JoeGazz84
2 Replies

8. Shell Programming and Scripting

set only some command & scripts permission to a particular user

hi, i am new in unix.......i am using bash and i want to create a user which has only some command and scripts permission.........is it possible? thanx (1 Reply)
Discussion started by: rakeshtomar82
1 Replies

9. Homework & Coursework Questions

Need to write a script user.sh that parses name & password

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write a script called user.sh that parses the name and password of given in a file (user.txt) and creates the user... (1 Reply)
Discussion started by: Nygenesis
1 Replies

10. UNIX for Dummies Questions & Answers

Deletion of list of user based on a text file In LDAP UNIX server

Dear All, It would be really nice, if you could help me to write a script for deletion of list of user( more than 15000 users) stored in a file and sorted by email address( i need deletion of only a particular type of mail address). Is the any script to write and take the file as input and... (3 Replies)
Discussion started by: Chand
3 Replies
SHTOOL-ECHO.TMP(1)					      GNU Portable Shell Tool						SHTOOL-ECHO.TMP(1)

NAME
shtool-echo - GNU shtool echo(1) extensional command SYNOPSIS
shtool echo [-n|--newline] [-e|--expand] string DESCRIPTION
shtool echo is an echo(1) style command which prints string to stdout and optionally provides special expansion constructs (terminal bold mode, environment details, date, etc) and newline control. The trick of this command is that it provides a portable -n option and hides the gory details needed to find out the environment details under option -e. OPTIONS
The following command line options are available. -n, --newline By default, output is written to stdout followed by a "newline" (ASCII character 0x0a). If option -n is used, this newline character is omitted. -e, --expand If option -e is used, string can contain special "%x" constructs which are expanded before the output is written. Currently the following constructs are recognized: %B switch terminal mode to bold display mode. %b switch terminal mode back to normal display mode. %u the current user name. %U the current user id (numerical). %g the current group name. %G the current group id (numerical). %h the current hostname (without any domain extension). %d the current domain name. %D the current day of the month. %M the current month (numerical). %m the current month name. %Y the current year. EXAMPLE
# shell script shtool echo -n -e "Enter your name [%B%u%b]: "; read name shtool echo -e "Your Email address might be %u@%h%d" shtool echo -e "The current date is %D-%m-%Y" HISTORY
The GNU shtool echo command was originally written by Ralf S. Engelschall <rse@engelschall.com> in 1998 for Website META Language (WML) under the name buildinfo. It was later taken over into GNU shtool. SEE ALSO
shtool(1), echo(1). 18-Jul-2008 shtool 2.0.8 SHTOOL-ECHO.TMP(1)
All times are GMT -4. The time now is 12:07 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy