- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Script issue with HISTFILE
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
Forums
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
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
тАО10-19-2004 01:08 AM
тАО10-19-2004 01:08 AM
I want a script that I can run from root's cron, that will timestamp the .sh_history files of all admins still logged in as root once a day. echo `date` >> sh_history _$USER is not an option as it does NOT issue a newline correctly - IE, the next command a user enters is appened to the line of the date command...
Findings:
Whether I use a function for the routine that sets the HISTFILE or have it striaght in the flow of the srcript, even though the HISTFILE appears to be set (from testing with logging and set -x), the timestamp of each USER only goes into the first USER's .sh_history.
The only work around I found was to have the routine in a seperate script. That just seems silly to me - must be a way to incorporate this all in a single script.
Attached is a text document with results and the script(s).
Rgds...Geoff
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-19-2004 01:23 AM
тАО10-19-2004 01:23 AM
Re: Script issue with HISTFILE
try using print -s $(date)
i beleive the -s option will print to the shell history file instead of to standard out.
The solution was presented previously on the forum. Not sure how to seach for it though.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-19-2004 01:28 AM
тАО10-19-2004 01:28 AM
Re: Script issue with HISTFILE
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-19-2004 01:58 AM
тАО10-19-2004 01:58 AM
Re: Script issue with HISTFILE
Have you tried
echo `date\n` >> .sh_history
Hope this helps.
Regds
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-19-2004 02:05 AM
тАО10-19-2004 02:05 AM
Re: Script issue with HISTFILE
print -s only way to go - just would like to do it all in a single script - instead of having to call another script from the first one...
It's almost as if once you set the HISTFILE, it can't be unset (even though the export seems to work) for the duration of that shell?
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-19-2004 02:17 AM
тАО10-19-2004 02:17 AM
Re: Script issue with HISTFILE
I tried a simple one as,
# sh -x hist.sh
+ HISTFILE=/home/muthu/.sh_history
+ TEST=/home/muthu/.sh_history
+ export HISTFILE=/home/muthu/.sh_history_
+ echo HISTFILE is /home/muthu/.sh_history_
HISTFILE is /home/muthu/.sh_history_
+ /usr/bin/date
+ print -s Tue Oct 19 01:42:17 MDT 2004 still logged in as root...
+ unset HISTFILE
+ export HISTFILE=/home/muthu/.sh_history
+ echo HISTFILE after unset is /home/muthu/.sh_history
HISTFILE after unset is /home/muthu/.sh_history
# cat /home/muthu/.sh_history_
Tue Oct 19 01:42:17 MDT 2004 still logged in as root...
script
======
# point to their .sh_history file
TEST=$HISTFILE
export HISTFILE=/home/muthu/.sh_history_
# time stamp it
echo "HISTFILE is $HISTFILE"
print -s "`/usr/bin/date` $USER still logged in as root..."
unset HISTFILE
export HISTFILE=$TEST
echo "HISTFILE after unset is $HISTFILE"
Geoff,
Try to revert old history file there. Else, if you try with your script again, it will not having default HISTFILE another more there.
TEST=$HISTFILE
<.... snip ....>
export HISTFILE=$TEST
It will work there.
I tried on 11.0.
HTH.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-19-2004 02:17 AM
тАО10-19-2004 02:17 AM
Re: Script issue with HISTFILE
Interesting problem. Here's a crazy idea. Maybe you could 'su' to the user and then do the 'print -s', so you would pick the HISTFILE for free and not have to worry about unsetting it. Something like this:
/bin/su - $USER -c 'print -s "`/usr/bin/date` $USER still logged in as root..."
JP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-19-2004 02:23 AM
тАО10-19-2004 02:23 AM
Re: Script issue with HISTFILE
Jeff Traigle
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-19-2004 02:28 AM
тАО10-19-2004 02:28 AM
Re: Script issue with HISTFILE
HISTFILE=${HOME}/.sh_history_$USER print -s "`/usr/bin/date` $USER still logged in as root..."
JP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-19-2004 03:13 AM
тАО10-19-2004 03:13 AM
Re: Script issue with HISTFILE
Muthukumar - no luck:
tail /.sh_history_gwild
vi datestamp-root-history
datestamp-root-history
Tue Oct 19 09:04:17 MDT 2004 gwild still logged in as root...
Tue Oct 19 09:04:17 MDT 2004 user2 still logged in as root...
tail /.sh_history_gwild
Still adds the timestamp to only my (first) .sh_history...
John,
The su - USER won't work - as then I would have to su - again to root - and would need pin/fob number...this script is to only timestamp the root's .sh_history file for each admin...
Jeff - has to be HISTFILE - else how does it know which .sh_history to go to? If not, it will default to root's .sh_history - which we only have for console access..
Just an FYI - here's a snip from root's .profile:
# Set up logging
HISTFILE=${HOME}/.sh_history_`who am i|awk '{ print $1}'`
export HISTFILE
print -s "login at `/usr/bin/date`"
#date >>$HISTFILE
HISTSIZE=500
export HISTSIZE
And last post by John, that's what's in the script currently...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-19-2004 03:31 AM
тАО10-19-2004 03:31 AM
Re: Script issue with HISTFILE
The new users are not having environment variable HISTFILE set there while executing there.
we can reproduce with this,
#!/bin/sh
#
# script to add a date stamp to the /.sh_history_$USER
# for those su'ed to root
# Only run from cron once a day
# gwild 2004-10-15
#set -x
#===========================================================
# Function: TimeStamp
# Description: timestamp the /.sh_history_$USER
# Arguments: none
# Returns: none
#===========================================================
function TimeStamp {
# point to their .sh_history file
TEMP=$(su -c $USER -c "echo $HISTFILE")
if [[ $? -ne 0 ]]
then
echo "$USER: Not having history file"
exit 1
fi
echo "$USER history file $HISTFILE
export HISTFILE=${HOME}/.sh_history_$USER
# time stamp it
echo "HISTFILE is $HISTFILE"
print -s "`/usr/bin/date` $USER still logged in as root..."
unset HISTFILE
HISTFILE=$TEMP
echo "HISTFILE after unset is $HISTFILE"
}
#===================================================================
# BEGIN MAIN CODE
#===================================================================
# find parent process of all users signed in as root
for i in `ps -ef |grep "\-sh"|awk '{print $3}'`
do
# just grab the user name
USER=`ps -ef |grep $i |grep -v root|awk '{print $1}'`
# TimeStamp
TimeStamp $USER
done
exit 0
HTH.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-19-2004 03:49 AM
тАО10-19-2004 03:49 AM
Re: Script issue with HISTFILE
Your attachment includes the output of your script running with 'set -x' and using the external script to do your time stamp function. Can you post the output with 'set -x' for trying it with the function? I'd be curious to see what the output is. Specifically, I'd like to see if the $USER variable is getting inside the function correctly each time. If not, you might try passing it to the function as a parameter.
I realized after I posted my 'su' idea that it wasn't quite what I had in mind. My second post is different from your script in that it sets the HISTFILE on the command line as opposed to exporting it and then unsetting it.
JP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-19-2004 03:57 AM
тАО10-19-2004 03:57 AM
Re: Script issue with HISTFILE
Here's the output using the function:
+ ps -ef
+ grep \-sh
+ awk {print $3}
+ + ps -ef
+ grep 11964
+ grep -v root
+ awk {print $1}
USER=tstewart
+ TimeStamp
HISTFILE is //.sh_history_tstewart
HISTFILE after unset is
+ + ps -ef
+ grep 8983
+ grep -v root
+ awk {print $1}
USER=gwild
+ TimeStamp
HISTFILE is //.sh_history_gwild
HISTFILE after unset is
+ + ps -ef
+ grep 737
+ grep -v root
+ awk {print $1}
USER=elocken
+ TimeStamp
HISTFILE is //.sh_history_elocken
HISTFILE after unset is
+ exit 0
Script is:
#!/bin/sh
#
# script to add a date stamp to the /.sh_history_$USER
# for those su'ed to root
# Only run from cron once a day
# gwild 2004-10-15
set -x
#===========================================================
# Function: TimeStamp
# Description: timestamp the /.sh_history_$USER
# Arguments: none
# Returns: none
#===========================================================
function TimeStamp {
# point to their .sh_history file
export HISTFILE=${HOME}/.sh_history_$USER
# time stamp it
echo "HISTFILE is $HISTFILE"
print -s "`/usr/bin/date` $USER still logged in as root..."
unset HISTFILE
echo "HISTFILE after unset is $HISTFILE"
}
#===================================================================
# BEGIN MAIN CODE
#===================================================================
# find parent process of all users signed in as root
for i in `ps -ef |grep "\-sh"|awk '{print $3}'`
do
# just grab the user name
USER=`ps -ef |grep $i |grep -v root|awk '{print $1}'`
TimeStamp
# /usr/local/bin/datestamp-root-history-TimeStamp $USER
done
exit 0
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-19-2004 04:03 AM
тАО10-19-2004 04:03 AM
Re: Script issue with HISTFILE
Tried your way:
#!/bin/sh
#
# script to add a date stamp to the /.sh_history_$USER
# for those su'ed to root
# Only run from cron once a day
# gwild 2004-10-15
set -x
#===========================================================
# Function: TimeStamp
# Description: timestamp the /.sh_history_$USER
# Arguments: none
# Returns: none
#===========================================================
function TimeStamp {
# point to their .sh_history file
# export HISTFILE=${HOME}/.sh_history_$USER
HISTFILE=${HOME}/.sh_history_$USER
# time stamp it
echo "HISTFILE is $HISTFILE"
print -s "`/usr/bin/date` $USER still logged in as root..."
unset HISTFILE
echo "HISTFILE after unset is $HISTFILE"
}
#===================================================================
# BEGIN MAIN CODE
#===================================================================
# find parent process of all users signed in as root
for i in `ps -ef |grep "\-sh"|awk '{print $3}'`
do
# just grab the user name
USER=`ps -ef |grep $i |grep -v root|awk '{print $1}'`
TimeStamp
# /usr/local/bin/datestamp-root-history-TimeStamp $USER
done
exit 0
Same thing:
+ ps -ef
+ grep \-sh
+ awk {print $3}
+ + ps -ef
+ grep 11964
+ grep -v root
+ awk {print $1}
USER=tstewart
+ TimeStamp
HISTFILE is //.sh_history_tstewart
HISTFILE after unset is
+ + ps -ef
+ grep 8983
+ grep -v root
+ awk {print $1}
USER=gwild
+ TimeStamp
HISTFILE is //.sh_history_gwild
HISTFILE after unset is
+ + ps -ef
+ grep 737
+ grep -v root
+ awk {print $1}
USER=elocken
+ TimeStamp
HISTFILE is //.sh_history_elocken
HISTFILE after unset is
+ exit 0
Yet, only .sh_history_tstewart has the timestamp(s):
# tail /.sh_history_tstewart
Tue Oct 19 09:58:55 MDT 2004 tstewart still logged in as root...
Tue Oct 19 09:58:55 MDT 2004 gwild still logged in as root...
Tue Oct 19 09:58:55 MDT 2004 elocken still logged in as root...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-19-2004 04:03 AM
тАО10-19-2004 04:03 AM
Re: Script issue with HISTFILE
Well, I did some testing from the command line and it appears that the shell will only use the first setting of HISTFILE as you've observed in your script. (I tried both having it set initially from /.profile and unset initially... both cases print -s would only use the first set value.)
It works when timestamp is a separate script because you unset HISTFILE (or it's not set initially actually since it's running from cron in your case) before calling it. Thus each invocation of the timestamp script sets it and, since it's the first setting for that invocation, the print -s writes to the file you intend. When it's a function within the same script, you're restricted to writing to the file specified in the first setting of HISTFILE.
Given the shell behavior, it doesn't look like you can avoid having separate scripts (or I'm just not ingenius enough today to figure out how to work around it).
Jeff Traigle
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-19-2004 04:08 AM
тАО10-19-2004 04:08 AM
Re: Script issue with HISTFILE
Had to modify slightly - as it errored on second root user with:
/usr/local/bin/datestamp-root-history.Muthukumar
+ ps -ef
+ grep \-sh
+ awk {print $3}
+ + ps -ef
+ grep 11964
+ grep -v root
+ awk {print $1}
USER=tstewart
+ TimeStamp tstewart
tstewart history file //.sh_history_gwild
HISTFILE is //.sh_history_tstewart
HISTFILE after unset is (c)Copyright 1983-2000 Hewlett-Packard Co., All Rights Reserved.
(c)Copyright 1979, 1980, 1983, 1985-1993 The Regents of the Univ. of California
(c)Copyright 1980, 1984, 1986 Novell, Inc.
(c)Copyright 1986-1992 Sun Microsystems, Inc.
(c)Copyright 1985, 1986, 1988 Massachusetts Institute of Technology
(c)Copyright 1989-1993 The Open Software Foundation, Inc.
(c)Copyright 1986 Digital Equipment Corp.
(c)Copyright 1990 Motorola, Inc.
(c)Copyright 1990, 1991, 1992 Cornell University
(c)Copyright 1989-1991 The University of Maryland
(c)Copyright 1988 Carnegie Mellon University
(c)Copyright 1991-2000 Mentat Inc.
(c)Copyright 1996 Morning Star Technologies, Inc.
(c)Copyright 1996 Progressive Systems, Inc.
(c)Copyright 1991-2000 Isogon Corporation, All Rights Reserved.
RESTRICTED RIGHTS LEGEND
Use, duplication, or disclosure by the U.S. Government is subject to
restrictions as set forth in sub-paragraph (c)(1)(ii) of the Rights in
Technical Data and Computer Software clause in DFARS 252.227-7013.
Hewlett-Packard Company
3000 Hanover Street
Palo Alto, CA 94304 U.S.A.
Rights for non-DOD U.S. Government Departments and Agencies are as set
forth in FAR 52.227-19(c)(1,2).
//.sh_history_gwild
logout
+ + ps -ef
+ grep 8983
+ grep -v root
+ awk {print $1}
USER=gwild
+ TimeStamp gwild
ksh: syntax error at line 1 : `(' unexpected
gwild: Not having history file
So tried without the -:
TEMP=$(su $USER -c "echo $HISTFILE")
# /usr/local/bin/datestamp-root-history.Muthukumar
+ ps -ef
+ grep \-sh
+ awk {print $3}
+ + ps -ef
+ grep 11964
+ grep -v root
+ awk {print $1}
USER=tstewart
+ TimeStamp tstewart
tstewart history file //.sh_history_gwild
HISTFILE is //.sh_history_tstewart
HISTFILE after unset is //.sh_history_gwild
+ + ps -ef
+ grep 8983
+ grep -v root
+ awk {print $1}
USER=gwild
+ TimeStamp gwild
gwild history file //.sh_history_gwild
HISTFILE is //.sh_history_gwild
HISTFILE after unset is //.sh_history_gwild
+ + ps -ef
+ grep 737
+ grep -v root
+ awk {print $1}
USER=elocken
+ TimeStamp elocken
elocken history file //.sh_history_gwild
HISTFILE is //.sh_history_elocken
HISTFILE after unset is //.sh_history_gwild
+ exit 0
And yet again - it only updates the first /.sh_history_$USER
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-19-2004 04:15 AM
тАО10-19-2004 04:15 AM
Re: Script issue with HISTFILE
su - ${USER} -c "print -s $(date)"
for each user in this script? That would start a new shell with their login environment set (HISTFILE included) and should write to the appropriate file with each invocation.
Jeff Traigle
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-19-2004 04:24 AM
тАО10-19-2004 04:24 AM
Re: Script issue with HISTFILE
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-19-2004 04:32 AM
тАО10-19-2004 04:32 AM
SolutionI did some playing around with some test scripts. It appears that if I group the export of HISTFILE and the print -s into a subshell, it works. Here is an example:
#!/bin/sh
echo "HISTFILE is $HISTFILE"
HISAVE=$HISTFILE
(export HISTFILE=/root/.sh_this3; echo "HISTFILE is $HISTFILE"; print -s "this3"
)
(export HISTFILE=/root/.sh_this4; echo "HISTFILE is $HISTFILE"; print -s "this4
")
HISTFILE=$HISAVE
echo "HISTFILE is $HISTFILE"
HTH, John
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-19-2004 04:36 AM
тАО10-19-2004 04:36 AM
Re: Script issue with HISTFILE
Jeff Traigle
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-19-2004 04:45 AM
тАО10-19-2004 04:45 AM
Re: Script issue with HISTFILE
#!/bin/sh
#===========================================================
# Function: TimeStamp
# Description: timestamp the /.sh_history_$USER
# Arguments: none
# Returns: none
#===========================================================
function TimeStamp {
(
# point to their .sh_history file
export HISTFILE=${HOME}/.sh_history_$USER
# time stamp it
echo "HISTFILE is $HISTFILE"
print -s "`/usr/bin/date` $USER still logged in as root..."
)
}
#===================================================================
# BEGIN MAIN CODE
#===================================================================
# find parent process of all users signed in as root
for i in `ps -ef |grep "\-sh"|awk '{print $3}'`
do
# just grab the user name
USER=`ps -ef |grep $i |grep -v root|awk '{print $1}'`
TimeStamp
done
exit 0
Jeff Traigle
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-19-2004 04:46 AM
тАО10-19-2004 04:46 AM
Re: Script issue with HISTFILE
Here's the final script (embedded and attached):
#!/bin/sh
#
# script to add a date stamp to the /.sh_history_$USER
# for those su'ed to root
# Only run from cron once a day
# gwild 2004-10-15 with help from jkittle
#===========================================================
# Function: TimeStamp
# Description: timestamp the /.sh_history_$USER
# Arguments: none
# Returns: none
#===========================================================
function TimeStamp {
# point to their .sh_history file
# time stamp it
echo "HISTFILE is $HISTFILE"
(export HISTFILE=${HOME}/.sh_history_$USER; echo "HISTFILE is $HISTFILE"; print -s "`/usr/bin/date` $USER still logged in as root.
..")
#print -s "`/usr/bin/date` $USER still logged in as root..."
unset HISTFILE
echo "HISTFILE after unset is $HISTFILE"
}
#===================================================================
# BEGIN MAIN CODE
#===================================================================
# find parent process of all users signed in as root
for i in `ps -ef |grep "\-sh"|awk '{print $3}'`
do
# just grab the user name
USER=`ps -ef |grep $i |grep -v root|awk '{print $1}'`
TimeStamp
done
exit 0
Thanks all for helping.
Rgds...Geoff