LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-16-2017, 08:21 AM   #1
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 789

Rep: Reputation: Disabled
Add timestamp to log


The following will append the output of /usr/local/monapp/monclient to /var/log/monclient.log. How can I add a timestamp before each entry?

Code:
SCRIPT=/usr/local/monapp/monclient
RUNAS=monclient
PIDFILE=/var/run/monclient.pid
LOGFILE=/var/log/monclient.log

start() {
  if [ -f "$PIDFILE" ] && kill -0 $(cat "$PIDFILE"); then
    echo "Service already running" >&2
    return 1
  fi
  echo "Starting $PROG…" >&2
  local CMD="$SCRIPT >&3 2>&1 & echo \$!"
  cd `dirname $SCRIPT`
  su -c "$CMD" $RUNAS  3>>"$LOGFILE" >"$PIDFILE"
  echo "Service started" >&2
}
 
Old 06-16-2017, 08:57 AM   #2
business_kid
LQ Guru
 
Registered: Jan 2006
Location: Ireland
Distribution: Slackware, Slarm64 & Android
Posts: 16,289

Rep: Reputation: 2322Reputation: 2322Reputation: 2322Reputation: 2322Reputation: 2322Reputation: 2322Reputation: 2322Reputation: 2322Reputation: 2322Reputation: 2322Reputation: 2322
Perhaps invoke the 'date' command? No options required.
 
Old 06-16-2017, 09:10 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Can you say a little more about the script /usr/local/monapp/monclient and its nature? It is what you would have to modify if you want the date-time to appear on every line of output that it produces.

Alternately, you might be able to wedge something in before your redirect.

Code:
. . . | awk '{ "date +\"%F %T\"" | getline datestamp; close( "date +\"%F %T\"" ); print datestamp "\t" $0; }' >> . . .

Last edited by Turbocapitalist; 06-16-2017 at 09:40 AM. Reason: close()
 
Old 06-16-2017, 09:35 AM   #4
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 789

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
Can you say a little more about the script /usr/local/monapp/monclient and its nature? It is what you would have to modify if you want the date-time to appear on every line of output that it produces.

Alternately, you might be able to wedge something in before your redirect.

Code:
. . . | awk '{ "date +\"%F %T\"" | getline datestamp; print datestamp "\t" $0; }' >> . . .
It is a client that monitors the local machine and send data to a remote server. It's logging functionality is limited to periodical writing to stdout debugging messages, and it does not include a timestamp with each message.

Looks like your script is searching for the date in each message sent to stdout?
 
Old 06-16-2017, 09:42 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by NotionCommotion View Post
It is a client that monitors the local machine and send data to a remote server. It's logging functionality is limited to periodical writing to stdout debugging messages, and it does not include a timestamp with each message.

Looks like your script is searching for the date in each message sent to stdout?
Almost. It gets the date and time from the date utility and appends that to each line sent to stdout. However, see the updated version, I missed a proper close() function to force awk to get a new time for each line.
 
1 members found this post helpful.
Old 06-16-2017, 10:18 AM   #6
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 789

Original Poster
Rep: Reputation: Disabled
Thanks,

I've tried a couple of iterations, but this one errors with 25: local: +"%F: bad variable name, and the commented out one errors with awk: cannot open 3 (No such file or directory) bash: 3: Bad file descriptor.

Code:
#  local CMD="$SCRIPT >&3 2>&1 & echo \$!"
  local CMD="$SCRIPT | awk '{ "date +\"%F %T\"" | getline datestamp; close( "date +\"%F %T\"" ); print datestamp "\t" $0; }' >&3 2>&1 & echo \$!"
  cd `dirname $SCRIPT`
  su -c "$CMD" $RUNAS  3>>"$LOGFILE" >"$PIDFILE"
#  su -c "$CMD" $RUNAS  | awk '{ "date +\"%F %T\"" | getline datestamp; close( "date +\"%F %T\"" ); print datestamp "\t" $0; }' 3 >> "$LOGFILE" >"$PIDFILE"

Last edited by NotionCommotion; 06-16-2017 at 10:24 AM.
 
Old 06-16-2017, 10:33 AM   #7
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by NotionCommotion View Post
I've tried a couple of iterations, but this one errors
Yep. You have nested quotes which have to be escaped properly. When you have only two levels, you can alternate single ' and double " quotes to come to a solution. With three levels, it's significantly harder to look at:

Code:
sh -c "echo \"a $(date +\\" %F %T \\") b\" "
 
1 members found this post helpful.
Old 06-16-2017, 10:46 AM   #8
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 789

Original Poster
Rep: Reputation: Disabled
Yes, it is hard to look at!. This is what I ended up doing. Thanks!
Code:
#  local CMD="$SCRIPT >&3 2>&1 & echo \$!"
#  local CMD="$SCRIPT | awk '{ "date +\"%F %T\"" | getline datestamp; close( "date +\"%F %T\"" ); print datestamp "\t" $0; }' >&3 2>&1 & echo \$!"
  prnt='{ "date +\"%F %T\"" | getline datestamp; close( "date +\"%F %T\"" ); print datestamp "\t" $0; }'
  local CMD="$SCRIPT | awk '$prnt' >&3 2>&1 & echo \$!"
  cd `dirname $SCRIPT`
  su -c "$CMD" $RUNAS  3>>"$LOGFILE" >"$PIDFILE"
#  su -c "$CMD" $RUNAS  | awk '{ "date +\"%F %T\"" | getline datestamp; close( "date +\"%F %T\"" ); print datestamp "\t" $0; }' 3 >> "$LOGFILE" >"$PIDFILE"
 
Old 06-16-2017, 10:57 AM   #9
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 789

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NotionCommotion View Post
Yes, it is hard to look at!. This is what I ended up doing. Thanks!
Hummm...

For some reason, some outputs from /usr/local/monapp/monclient are no longer being sent to the log. What might cause this?
 
Old 06-16-2017, 11:01 AM   #10
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
What is the result of the dirname action? Maybe you are not changing to the right directory?

You can try adding this line towards the beginning of your script during the debugging process.

Code:
set -ex
 
Old 06-16-2017, 11:20 AM   #11
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 789

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
What is the result of the dirname action? Maybe you are not changing to the right directory?

You can try adding this line towards the beginning of your script during the debugging process.

Code:
set -ex
Not sure about set -ex.
set=make variables available?
-e=exit?
-x=expand command?

The thing is I do get the entries I am expecting with my original script:
Code:
  local CMD="$SCRIPT >&3 2>&1 & echo \$!"
  cd `dirname $SCRIPT`
  su -c "$CMD" $RUNAS  3>>"$LOGFILE" >"$PIDFILE"
but not with this:
Code:
  prnt='{ "date +\"%F %T\"" | getline datestamp; close( "date +\"%F %T\"" ); print datestamp "\t" $0; }'
  local CMD="$SCRIPT | awk '$prnt' >&3 2>&1 & echo \$!"
  cd `dirname $SCRIPT`
  su -c "$CMD" $RUNAS  3>>"$LOGFILE" >"$PIDFILE"
 
Old 06-16-2017, 01:36 PM   #12
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by NotionCommotion View Post
set=make variables available?
-e=exit?
-x=expand command?
The -e will stop the script if an error is encountered.

The -x will show you verbatim the command which will run, including all options. Whatever that shows should give you an idea of what the problem would be. In addition to the previous guess, I'd say trying to run the program in the background with the & is going to throw the results off.
 
  


Reply



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
How to add timestamp to the output string? thomas2004ch Linux - Software 6 05-26-2014 02:10 PM
[SOLVED] squid access log timestamp klingoncowboy4 Linux - Server 2 09-21-2011 09:50 AM
vsftp log timestamp issues straks Linux - Software 5 03-30-2011 03:15 AM
How to log network connections with timestamp batbayar Linux - Networking 1 03-17-2010 02:21 AM
add timestamp to name of file how? supafly Linux - General 3 11-14-2001 11:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:05 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