LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-26-2014, 06:08 AM   #1
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 537

Rep: Reputation: 33
How to add timestamp to the output string?


Hi,

I have a script which calls a java program as follow:

...
exec 5 > /var/log/mylog.log
...
java -cp $CLASSPATH -Xms64m -Xmx512m $JAVA_OPTS $MAINCLASS >&5 2>&1
...

The problem is, there is no any timestamp writen into the log file. How can I add the timestamp?
 
Old 05-26-2014, 06:39 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,501

Rep: Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314
for example:
date >> /var/log/mylog.log
and you need to use >> instead of single >
java -cp $CLASSPATH -Xms64m -Xmx512m $JAVA_OPTS $MAINCLASS >>&5 2>&1
 
Old 05-26-2014, 06:59 AM   #3
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 537

Original Poster
Rep: Reputation: 33
Hi,

As I use the
Code:
...
java -cp $CLASSPATH -Xms64m -Xmx512m $JAVA_OPTS $MAINCLASS >>&5 2>&1
...
I got error message:

Code:
...
./myscript.sh: line 92: syntax error near unexpected token `&'
./myscript.sh: line 92: `java -cp $CLASSPATH -Xms64m -Xmx512m $JAVA_OPTS $MAINCLASS >>&5 2>&1'
...
 
Old 05-26-2014, 07:10 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,501

Rep: Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314
so you need a filename:
Code:
logfilename=/var/log/mylog.log
date >> $logfilename
java -cp ..... >>$logfilename 2>&1
 
Old 05-26-2014, 08:54 AM   #5
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 537

Original Poster
Rep: Reputation: 33
Hi,

This works.

But how can I print the mini-second as weill?
 
Old 05-26-2014, 09:03 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,501

Rep: Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314Reputation: 4314
see man date, probably date '+%D %T.%N' >> $logfilename works for you
 
Old 05-26-2014, 03:10 PM   #7
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,756

Rep: Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524
Quote:
Originally Posted by thomas2004ch View Post
Hi,

This works.

But how can I print the mini-second as weill?
man date or date --help to get the syntax for the date command. powerful tool the date command.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] log bash script output to file with timestamp daisychick Programming 11 05-08-2013 02:50 AM
How to use Vi to match a string of text, add a new line, insert string... Slycraft Linux - Newbie 7 07-20-2012 11:24 AM
Bash output with timestamp removing duplicates sociopathichaze Programming 3 11-21-2010 06:10 AM
Add a timestamp to photos by reading the file date...? FSHero Linux - Software 4 09-14-2007 01:15 AM
add timestamp to name of file how? supafly Linux - General 3 11-14-2001 12:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration