LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-01-2011, 05:15 AM   #1
blrguest
LQ Newbie
 
Registered: Feb 2008
Posts: 3

Rep: Reputation: 0
How to make my own file as a running log.


Hi,

I have written a small script from that iam appending the output to a file.If multiple users invoke the same script or if i invoke the same script n number of times (using &), the output file(ZZ/OUT) contains messup information.
++++++++++++++++++++++++++++++++
#!/bin/bash
#

echo "Hello" >> /tmp/ZZ/OUT
sleep 10
echo "World" >> /tmp/ZZ/OUT
sleep 10
echo "---------end -----------" >> /tmp/ZZ/OUT
sleep 5
++++++++++++++++++++++++++++++++++
cat OUT ----->
+++++++++++++++++++++++++++++++++
Hello
Hello
Hello
World
World
World
---------end -----------
---------end -----------
---------end -----------

++++++++++++++++++++++++++++++++++++++++

I would like to know is there any possibility to get consistent output like log file (/var/log/message) for any number of time script invocation. like below.
+++++++++++++++++++++++++++++++++++++++

Hello
World
---------end -----------
Hello
World
---------end -----------

Thanks in advance.

-regards
guest.
 
Old 04-01-2011, 07:56 AM   #2
thund3rstruck
Member
 
Registered: Nov 2005
Location: East Coast, USA
Distribution: Fedora 18, Slackware64 13.37, Windows 7/8
Posts: 386

Rep: Reputation: 43
Can you create a separate log per user?

Code:
 &>> /tmp/ZZ/"$USER".log
 
Old 04-01-2011, 09:21 AM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by blrguest View Post
I would like to know is there any possibility to get consistent output like log file (/var/log/message) for any number of time script invocation.
Yes, use the flock (man 1 flock) command from the util-linux-ng package.

Since you wish to log multi-line reports -- I'm assuming as the script progresses, which may take a bit of time -- I'd recommend using a temporary file to store all output of the script, and append it to the actual log only when the script exits. That way the log file is only locked for the short moment it takes to append it. The trap bash built-in is perfect for this. In this example code, I used a five-second timeout for the lock: if it cannot lock the log file in five seconds, it will append its log without the lock.
Code:
#!/bin/bash
LOGFILE="/tmp/ZZ/OUT"

# Create a safe temporary directory $WORK for temporary files.
# $WORK/log is appended to $LOGFILE when the script exits, using flock(1).
# Finally, $WORK and all its contents are removed when the script exits;
# you can use $WORK for any and all other temporary files you might need.
WORK="`mktemp -d`" || exit $?
trap "flock -e -w 5 '$LOGFILE' bash -c 'cat \"$WORK/log\" >> \"$LOGFILE\" ; echo \"-----------------------\" >> \"$LOGFILE\"' || bash -c 'cat \"$WORK/log\" >> \"$LOGFILE\" ; echo \"-----------------------\" >> \"$LOGFILE\"'; rm -rf '$WORK'" EXIT

# Redirect output and error to $WORK/log.
exec &>"$WORK/log"

# Now your script can do it's work. No other commands are needed,
# the above code will take care of everything.

echo "This is process $$ at `date`."
sleep 1
echo "Hello"
sleep 1
echo "World"
Since the trap command is a bit hard to parse, here's the command it runs when the script exits:
Code:
flock -e -w 5 '$LOGFILE' \
   bash -c ' cat \"$WORK/log\" >> \"$LOGFILE\"
             echo \"-----------------------\" >> \"$LOGFILE\"
           ' \
|| bash -c ' cat \"$WORK/log\" >> \"$LOGFILE\"
             echo \"-----------------------\" >> \"$LOGFILE\"
           ' ; \
rm -rf '$WORK'
The flock command will fail if it cannot get the write lock (-e for exclusive lock) within the five-second timeout (-w 5). If it gets the lock, it'll cat the temporary log file to the actual log file, and append a horizontal line. If flock fails, the exact same command is run -- I assume you'd rather have messed output than none at all, if something weird happens. But remember, flock will usually get the lock, and that is what prevents the processes from messing with each others output.

Hope this helps.
 
1 members found this post helpful.
Old 04-05-2011, 08:20 AM   #4
blrguest
LQ Newbie
 
Registered: Feb 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Smile

Thanks for the reply, its working fine.

-guest
Quote:
Originally Posted by Nominal Animal View Post
Yes, use the flock (man 1 flock) command from the util-linux-ng package.

Since you wish to log multi-line reports -- I'm assuming as the script progresses, which may take a bit of time -- I'd recommend using a temporary file to store all output of the script, and append it to the actual log only when the script exits. That way the log file is only locked for the short moment it takes to append it. The trap bash built-in is perfect for this. In this example code, I used a five-second timeout for the lock: if it cannot lock the log file in five seconds, it will append its log without the lock.
Code:
#!/bin/bash
LOGFILE="/tmp/ZZ/OUT"

# Create a safe temporary directory $WORK for temporary files.
# $WORK/log is appended to $LOGFILE when the script exits, using flock(1).
# Finally, $WORK and all its contents are removed when the script exits;
# you can use $WORK for any and all other temporary files you might need.
WORK="`mktemp -d`" || exit $?
trap "flock -e -w 5 '$LOGFILE' bash -c 'cat \"$WORK/log\" >> \"$LOGFILE\" ; echo \"-----------------------\" >> \"$LOGFILE\"' || bash -c 'cat \"$WORK/log\" >> \"$LOGFILE\" ; echo \"-----------------------\" >> \"$LOGFILE\"'; rm -rf '$WORK'" EXIT

# Redirect output and error to $WORK/log.
exec &>"$WORK/log"

# Now your script can do it's work. No other commands are needed,
# the above code will take care of everything.

echo "This is process $$ at `date`."
sleep 1
echo "Hello"
sleep 1
echo "World"
Since the trap command is a bit hard to parse, here's the command it runs when the script exits:
Code:
flock -e -w 5 '$LOGFILE' \
   bash -c ' cat \"$WORK/log\" >> \"$LOGFILE\"
             echo \"-----------------------\" >> \"$LOGFILE\"
           ' \
|| bash -c ' cat \"$WORK/log\" >> \"$LOGFILE\"
             echo \"-----------------------\" >> \"$LOGFILE\"
           ' ; \
rm -rf '$WORK'
The flock command will fail if it cannot get the write lock (-e for exclusive lock) within the five-second timeout (-w 5). If it gets the lock, it'll cat the temporary log file to the actual log file, and append a horizontal line. If flock fails, the exact same command is run -- I assume you'd rather have messed output than none at all, if something weird happens. But remember, flock will usually get the lock, and that is what prevents the processes from messing with each others output.

Hope this helps.
 
  


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
want to make Log of system calls per process running on the machine sudhansu Linux - Kernel 7 02-16-2012 02:50 AM
Taking back up of log file for running process/ route Programming 3 02-18-2010 05:15 AM
Firewall log file, how to make several different log files with IPTables? newtovanilla Linux - Newbie 5 11-28-2008 12:39 PM
any ideas to reduce log file size or make log file size managed? George2 Programming 2 08-13-2006 06:55 AM
Samba wasn't running - checked log file today duality Red Hat 1 07-24-2004 01:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:23 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
Open Source Consulting | Domain Registration