The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
background process vinod.thayil Shell Programming and Scripting 1 08-17-2007 02:56 AM
background process jerardfjay Shell Programming and Scripting 1 09-07-2005 08:03 AM
background process k_oops9 Shell Programming and Scripting 3 11-15-2004 01:15 AM
background process badshah High Level Programming 3 05-30-2003 08:01 AM
capture the process id when starting a background process jleavitt Shell Programming and Scripting 10 04-04-2002 05:04 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 12-02-2007
Registered User
 

Join Date: Oct 2007
Posts: 74
Help in background process

Hi,

I have a main script(main.ksh) within which I have called another script(sub.ksh). The sub.ksh script is made to run in the background using '&'. The main.ksh script logs the information in a logfile main_ddmmyy and the sub.ksh script also logs the information in the log file sub_ddmmyy. This sub.ksh script will take long time to execute.

My problem arises here. Since the sub.ksh script is executed at the background,I started another process(new.ksh) in the foreground. The new.ksh also logs the information in main_ddmmyy file . The new.ksh script is executed properly and logs the information in its log file, but the sub.ksh script is not logging the information in its logfile when I have process both in foreground and background. If I don't start the process(new.ksh),the information from the background script(sub.ksh) is logged properly.

Hope I have made you clear.

I am not able to figure out where the problem is. Please help me on it.

Thanks in advance
Chella
Reply With Quote
Forum Sponsor
  #2  
Old 12-03-2007
Smiling Dragon's Avatar
Disorganised User
 
Join Date: Nov 2007
Location: New Zealand
Posts: 734
It sounds like the two functions are logging to the same logfile?
If so, make sure both are using >> rather than > so the entries get interleaved.

Alternativly, give them different logs to write to.
Reply With Quote
  #3  
Old 12-03-2007
Registered User
 

Join Date: Oct 2007
Posts: 74
Quote:
Originally Posted by Smiling Dragon View Post
It sounds like the two functions are logging to the same logfile?
If so, make sure both are using >> rather than > so the entries get interleaved.

Alternativly, give them different logs to write to.
Thanks for the reply.

I don't have any problem in logging into same file. But the background process fails to log the information in its log file when I have a foreground process running which logs the information in different file. I want to make clear that the foreground and background process are not logging the information in the same file.
Main.ksh - Main_ddmmyy(Foreground)
Sub.ksh - Sub_ddmmyy(background process)
New.ksh - Main_ddmmyy(Foreground)
The problem is with logging the sub_ddmmyy file.

Please suggest me on it.

Thanks in advance.
Chella
Reply With Quote
  #4  
Old 12-03-2007
Registered User
 

Join Date: Dec 2007
Posts: 47
can we see some code as to how you are doing your logging??
Reply With Quote
  #5  
Old 12-03-2007
Registered User
 

Join Date: Oct 2007
Posts: 155
Note that this will not work:

Code:
sub.ksh & >/tmp/logfile 2>&1
but this will:

Code:
sub.ksh >/tmp/logfile 2>&1 &
Redirection can get tricky, which is why I add this code to any of my scripts that run in the background or unattended:

Code:
exec >/tmp/${0##*/}.log 2>&1
After this line, all output (stdout and stderr) will go to a file in /tmp using the program basename with a ".log" extension. Don't try this from the command line!

UPDATE: I actually use a base set of code for background jobs:

Code:
# Send all output to a logfile and supress input
typeset LOG="/tmp/${0##*/}.out"
mv $LOG ${LOG}.old >/dev/null 2>&1
[[ -t 1 ]] && echo "Writing to logfile '$LOG'."
exec > $LOG 2>&1
exec < /dev/null 2<&1
Note that the shell expression "${0##*/}" is the same as "basename $0". Also, the "[[ -t ]]" should return TRUE when the process is attached to a terminal. In this case, it just prints a message about the logfile (instead of having the user wonder if the script is even running).

I also close stdin since an unattended script should never need input. For instance, if you have "grep $FILENAME" or "cat $FILENAME" and a bug has left $FILENAME unset, then the script would normally hang waiting for input from stdin. If that input is already /dev/null then the script can continue.

Last edited by gus2000; 12-05-2007 at 01:19 PM.
Reply With Quote
  #6  
Old 12-03-2007
Registered User
 

Join Date: Oct 2007
Posts: 74
Quote:
Originally Posted by gus2000 View Post
Note that this will not work:

Code:
sub.ksh & >/tmp/logfile 2>&1
but this will:

Code:
sub.ksh >/tmp/logfile 2>&1 &
Redirection can get tricky, which is why I add this code to any of my scripts that run in the background or unattended:

Code:
exec >/tmp/${0##*/}.log 2>&1
After this line, all output (stdout and stderr) will go to a file in /tmp using the program basename with a ".log" extension. Don't try this from the command line!


Thanks a lot for the reply.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 07:30 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0