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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
06-16-2017, 08:21 AM
|
#1
|
Member
Registered: Aug 2012
Posts: 789
Rep: 
|
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
}
|
|
|
06-16-2017, 08:57 AM
|
#2
|
LQ Guru
Registered: Jan 2006
Location: Ireland
Distribution: Slackware, Slarm64 & Android
Posts: 17,428
|
Perhaps invoke the 'date' command? No options required.
|
|
|
06-16-2017, 09:10 AM
|
#3
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,718
|
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()
|
|
|
06-16-2017, 09:35 AM
|
#4
|
Member
Registered: Aug 2012
Posts: 789
Original Poster
Rep: 
|
Quote:
Originally Posted by Turbocapitalist
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?
|
|
|
06-16-2017, 09:42 AM
|
#5
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,718
|
Quote:
Originally Posted by NotionCommotion
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.
|
06-16-2017, 10:18 AM
|
#6
|
Member
Registered: Aug 2012
Posts: 789
Original Poster
Rep: 
|
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.
|
|
|
06-16-2017, 10:33 AM
|
#7
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,718
|
Quote:
Originally Posted by NotionCommotion
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.
|
06-16-2017, 10:46 AM
|
#8
|
Member
Registered: Aug 2012
Posts: 789
Original Poster
Rep: 
|
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"
|
|
|
06-16-2017, 10:57 AM
|
#9
|
Member
Registered: Aug 2012
Posts: 789
Original Poster
Rep: 
|
Quote:
Originally Posted by NotionCommotion
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?
|
|
|
06-16-2017, 11:01 AM
|
#10
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,718
|
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.
|
|
|
06-16-2017, 11:20 AM
|
#11
|
Member
Registered: Aug 2012
Posts: 789
Original Poster
Rep: 
|
Quote:
Originally Posted by Turbocapitalist
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.
|
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"
|
|
|
06-16-2017, 01:36 PM
|
#12
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,718
|
Quote:
Originally Posted by NotionCommotion
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.
|
|
|
All times are GMT -5. The time now is 08:59 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|