thimerion
asked on
Unix Script add date & time to redirect
Hi,
I run a program (egate, but does not matter in this) and I redirect the output using 1> and 2>
eg.
/pshrmcappkg1/app/egate/cl ient/bin/s tccb -rh gbdxgm04 -rs HP3000 -un Administrator -up STC -ln hp3000_cb -ss 1> /psmimcappkg1/app/log/egat e/HP3000st dout.log 2> /psmimcappkg1/app/log/egat e/HP3000st derr.log
This gives me 2, constantly growing log files, which is the whole idea.
BUT: the output gives NO indication of date/time whatsoever. so this log file is getting really fustrating if I need to find something.
So I want to add a date/time stamp to this log.
I tryed to do this using unix cron:
00,10,20,30,40,50 * * * *
echo "\r\nDATE & TIME: "`date -u '+%Y%m%d%H%M%S'` >> HP3000stdout.log
BUT: it seems so, that unix keep its fileposition, and overwrites the DATE & TIME again with its next log messages.
Now I am looking to a solution where we redirect to a script that adds date & time before writing the log, but I do not know if that is possible.
Any other ideas.
Tim
I run a program (egate, but does not matter in this) and I redirect the output using 1> and 2>
eg.
/pshrmcappkg1/app/egate/cl
This gives me 2, constantly growing log files, which is the whole idea.
BUT: the output gives NO indication of date/time whatsoever. so this log file is getting really fustrating if I need to find something.
So I want to add a date/time stamp to this log.
I tryed to do this using unix cron:
00,10,20,30,40,50 * * * *
echo "\r\nDATE & TIME: "`date -u '+%Y%m%d%H%M%S'` >> HP3000stdout.log
BUT: it seems so, that unix keep its fileposition, and overwrites the DATE & TIME again with its next log messages.
Now I am looking to a solution where we redirect to a script that adds date & time before writing the log, but I do not know if that is possible.
Any other ideas.
Tim
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi !!!
@ahoffmann: you gave me great idea on how to do this! Unfortunately the perl functions strftime and systime did not compile on my system. Probably perl is not good initialized because we never use it.
Since I got the idea on how to do this, I could do the same with awk. And the working thing became:
/tsmkappkg1/app/egate/clie nt/bin/stc cb -rh tsmkappkg1 -rs HP3000 -un Administrator -up STC -ln hp3000_cb -ss 2>&1 | awk -F= '{cmd="date -u '+%c'";cmd|getline d; close(cmd);print d, $1}' >> /tsmkappkg1/app/log/egate/ HP3000out. log &
The processes it uses however afer looking very strange now, our sysadmin is particually worried about the process called <defunct>, does anyone knows where it comes from, what it does and how we get rid of it if it is harmfull.
egatest 4451 4450 5 May 12 ? 0:00 <defunct>
egatest 4450 1 0 May 12 ? 0:04 awk -F= {cmd="date -u +%c";cmd|getline d; close(cmd);print d, $
@NovaDenizen:
This solution is harder for me to try, because I am not the root administrator on the system. I have a few questions before I try:
How will the date be formatted in the logfile?
I need multiple logfiles, can I send to other channels as local6 as well?
Thanks!!
Tim
@ahoffmann: you gave me great idea on how to do this! Unfortunately the perl functions strftime and systime did not compile on my system. Probably perl is not good initialized because we never use it.
Since I got the idea on how to do this, I could do the same with awk. And the working thing became:
/tsmkappkg1/app/egate/clie
The processes it uses however afer looking very strange now, our sysadmin is particually worried about the process called <defunct>, does anyone knows where it comes from, what it does and how we get rid of it if it is harmfull.
egatest 4451 4450 5 May 12 ? 0:00 <defunct>
egatest 4450 1 0 May 12 ? 0:04 awk -F= {cmd="date -u +%c";cmd|getline d; close(cmd);print d, $
@NovaDenizen:
This solution is harder for me to try, because I am not the root administrator on the system. I have a few questions before I try:
How will the date be formatted in the logfile?
I need multiple logfiles, can I send to other channels as local6 as well?
Thanks!!
Tim
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I agree syslog is the cleanest approach, and the intermediate script the next best, but thought I'd clarify that your original approach *will* work, as long as all writers of the log file open it in append mode. The kernel guarantees the atomicity of the append operation, and the lines will not clobber each other. You just have to change your main log redirections to 1>> and 2>>, respectively. If you want to first clobber any existing file, first use a line like:
> logfile
Tim
> logfile
Tim
you may use awk too, simply like:
| gawk '{d=strftime("%T");print d" "$0}'
but keep in mind that this is with gawk only (probably mawk also)
According perl, you probaly just miss the POSIX module, see at http://www.cpan.org/
| gawk '{d=strftime("%T");print d" "$0}'
but keep in mind that this is with gawk only (probably mawk also)
According perl, you probaly just miss the POSIX module, see at http://www.cpan.org/
ASKER
@NovaDenizen : Thanks for the explaination, I have raised a request to the sysadmins to add a line in the syslog.conf. Curious of the result.
@TimEliseo : I don't understand what you are saying.
@ahoffmann : Is there any advantage of using gawk instead of awk, or is it just shorter in notation?
Regards,
Tim
@TimEliseo : I don't understand what you are saying.
@ahoffmann : Is there any advantage of using gawk instead of awk, or is it just shorter in notation?
Regards,
Tim
gawk is Gnu's version of awk
Most modern UNIX come with both versíons installed (you need to check your manuals or installation).
Even gawk's implementation is different to that of original awk, it is source compatible in most cases but with a lot of usefull extensions (like strftime:)
Most modern UNIX come with both versíons installed (you need to check your manuals or installation).
Even gawk's implementation is different to that of original awk, it is source compatible in most cases but with a lot of usefull extensions (like strftime:)
Using your original cron-based timestamp append method, if you change your application run from:
/pshrmcappkg1/app/egate/cl ient/bin/s tccb -rh gbdxgm04 -rs HP3000 -un Administrator -up STC -ln hp3000_cb -ss 1> /psmimcappkg1/app/log/egat e/HP3000st dout.log 2> /psmimcappkg1/app/log/egat e/HP3000st derr.log
to:
/pshrmcappkg1/app/egate/cl ient/bin/s tccb -rh gbdxgm04 -rs HP3000 -un Administrator -up STC -ln hp3000_cb -ss 1>> /psmimcappkg1/app/log/egat e/HP3000st dout.log 2>> /psmimcappkg1/app/log/egat e/HP3000st derr.log
Then the log it generates will not overwrite the timestamps being appended by the cron job. (This is only guaranteed to work properly if the log file is local, not a network file over NFS.)
/pshrmcappkg1/app/egate/cl
to:
/pshrmcappkg1/app/egate/cl
Then the log it generates will not overwrite the timestamps being appended by the cron job. (This is only guaranteed to work properly if the log file is local, not a network file over NFS.)
ASKER
@ahoffmann :
Cannot use gawk, it is not known and not planning to implement.
Thanks for the helpfull comments.
@NovaDenizen :
syslog is great, only pitty that it creates the log file as root. I tried to touch it first with the egate user, but then it does not write in it.
Thanks for the helpfull comments to you to
@TimEliseo :
Thanks for the advice, but the other solutions give much more detail in date and time.
I now have to choose between the awk and the logger solution. I have running them both, on a different logfile. So I can review in one week or so.
If someone still has an idea about the <defunct> process I mentioned earlier ... I am getting this with both awk and logger solutions.
Regards,
Tim
Cannot use gawk, it is not known and not planning to implement.
Thanks for the helpfull comments.
@NovaDenizen :
syslog is great, only pitty that it creates the log file as root. I tried to touch it first with the egate user, but then it does not write in it.
Thanks for the helpfull comments to you to
@TimEliseo :
Thanks for the advice, but the other solutions give much more detail in date and time.
I now have to choose between the awk and the logger solution. I have running them both, on a different logfile. So I can review in one week or so.
If someone still has an idea about the <defunct> process I mentioned earlier ... I am getting this with both awk and logger solutions.
Regards,
Tim
The process marked <defunct> is called a zombie process. It should not concern your sysadmin. This is really a separate question, and is a PAQ with many references (that I don't have handy). Just look up "zombie process".
zombies are what the name says: there but not existing
for more discussions and links see http:/Q_21367737.html
for more discussions and links see http:/Q_21367737.html
ASKER
Thanks for your response, I get following error while trying:
$ /tsmkappkg1/app/egate/clie
ksh: syntax error: `|' unexpected
Tim