LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 05:08 AM   #1
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 539

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, 05:39 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,502

Rep: Reputation: 8055Reputation: 8055Reputation: 8055Reputation: 8055Reputation: 8055Reputation: 8055Reputation: 8055Reputation: 8055Reputation: 8055Reputation: 8055Reputation: 8055
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, 05:59 AM   #3
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 539

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, 06:10 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,502

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

Original Poster
Rep: Reputation: 33
Hi,

This works.

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

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

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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 01: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 10:24 AM
Bash output with timestamp removing duplicates sociopathichaze Programming 3 11-21-2010 05:10 AM
Add a timestamp to photos by reading the file date...? FSHero Linux - Software 4 09-14-2007 12:15 AM
add timestamp to name of file how? supafly Linux - General 3 11-14-2001 11:06 AM

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

All times are GMT -5. The time now is 07:25 PM.

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
Open Source Consulting | Domain Registration