- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Problem with deleting utmp entries in script
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-07-2007 05:23 AM
тАО02-07-2007 05:23 AM
I have a script which deletes PIDs of a specified user, but it is not deleting the entries in the utmp file ... any ideas?
Here's the script:
#!/bin/ksh
PATH=/usr/local/bin:/usr/bin:/bin
kill_sessions()
{
ps -ef | grep "$username" | sort | awk '{ print $2 }' >> killfile
for i in `cat killfile`
do
kill "$i"
done
}
erase_entries()
{
/usr/sbin/acct/fwtmp < /etc/utmp > /tmp/utmp.out
ls -l /tmp/utmp.out
cat /tmp/utmp.out |
sed -e 's/"$username"//g' > /tmp/new_utmp.out
/usr/sbin/acct/fwtmp -ic < /tmp/new_utmp.out > /etc/utmp
}
while (true) do
echo ""
echo ""
echo "Do you wish to unhang a user session ( Please type Yes or No) ? \c"
read choice
case "$choice" in
"Yes") echo "Okay"
echo "Server? \c"
read server
echo "Username? \c"
read username
kill_sessions
erase_entries
;;
"No") echo "Exiting program."
PATH=$HOME
exit 1
;;
*) echo "Please type Yes or No at the prompt"
;;
esac
done
PATH=$HOME
Thanks in advance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-07-2007 05:30 AM
тАО02-07-2007 05:30 AM
Re: Problem with deleting utmp entries in script
I am intentionally not going to fix your problem (because that is really not doing you a favor) but I will point out your fundamental error:
's/"$username"//g'
NOTE: You are expecting variable instantiation to take place inside single quotes --- and that ain't gonna happen.
Now, a better question is why you are doing this? At best, who and utmp/wtmp represent a "best guess" of who is on the system. A far better and more trustworthy resource is the process table (ps -e).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-07-2007 05:40 AM
тАО02-07-2007 05:40 AM
Re: Problem with deleting utmp entries in script
Hi,
I am intentionally not going to fix your problem (because that is really not doing you a favor) but I will point out your fundamental error:
's/"$username"//g'
NOTE: You are expecting variable instantiation to take place inside single quotes --- and that ain't gonna happen.
Now, a better question is why you are doing this? At best, who and utmp/wtmp represent a "best guess" of who is on the system. A far better and more trustworthy resource is the process table (ps -e).
Hey Clay,
The reason I am doing this is to 'unhang' user sessions. I am also making another error, the uer with multiple sessions is to have all but the first removed from the process table and all but the first entry removed from utmp. I can't go into more detail than that, but it is on the up and up.
Now, one approach I could take is to use awk and manipulate the killfile with the getline function to be able to skip the first process. The only reason I used sed was to try and 'null' the username entries from the utmp file, in effect logging them out of the system.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-07-2007 06:45 AM
тАО02-07-2007 06:45 AM
SolutionNote this line:
's/"$username"//g'
when the shell hands off this line to sed
"$username" is literally just that: "$username" --- when you probably meant for $username to be "joe".
The fix is to do away with the single quotes:
username="Joe"
sed "s/${username}//g" < infile > outfile
This will replace every occurence of "Joe" with a null string. What you probably intended was deleting the line when the username was found.
sed "/${username}/d" < infile > outfile
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-07-2007 07:04 AM
тАО02-07-2007 07:04 AM
Re: Problem with deleting utmp entries in script
Thanks Clay .. I was missing a few things on the sed statement there.
You actually helped me quite a bit (even though I know you're right, it would have helped more if I figured it out myself, from your clue) .. anyway thanks.
Now all I have to do is figure out how to preserve the first entry in the process table and the utmp (don't ask me why) file..
You should've received some points on that .. let me know if they didn't get added on.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-07-2007 07:50 AM
тАО02-07-2007 07:50 AM