Help in background process


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help in background process
# 1  
Old 12-03-2007
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
# 2  
Old 12-03-2007
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.
# 3  
Old 12-04-2007
Quote:
Originally Posted by Smiling Dragon
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
# 4  
Old 12-04-2007
can we see some code as to how you are doing your logging??
# 5  
Old 12-04-2007
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 04:19 PM..
# 6  
Old 12-04-2007
Quote:
Originally Posted by gus2000
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Make background process interact with fg process

Hi, I have written a menu driven shell script in which as per the choice, I run the another script on background. For eg: 1. get info 2)process info 3)modify info All the operations have different scripts which i schedule in background using &. However I wish to display the error... (0 Replies)
Discussion started by: ashima jain
0 Replies

2. Shell Programming and Scripting

How to put FTP process as a background process/job in perl?

Hi, I am using net::ftp for transferring files now i am trying in the same Linux server as a result ftp is very fast but if the server is other location (remote) then the file transferred will be time consuming. So i want try putting FTP part as a background process. I am unaware how to do... (5 Replies)
Discussion started by: vanitham
5 Replies

3. AIX

Background process

how to check the process running in background? how can i run a process in background? pls explain with commands (1 Reply)
Discussion started by: udtyuvaraj
1 Replies

4. UNIX for Dummies Questions & Answers

Script to start background process and then kill process

What I need to learn is how to use a script that launches background processes, and then kills those processes as needed. The script successfully launches the script. But how do I check to see if the job exists before I kill it? I know my problem is mostly failure to understand parameter... (4 Replies)
Discussion started by: holocene
4 Replies

5. UNIX for Dummies Questions & Answers

Background Process

I need to submit a script that will continue to run after logging out and after a reboot or shutdown. I entered the following: nohup script & The script continues to run in the background after logging off the system but is killed after a reboot or shutdown. Any help would be greatly... (1 Reply)
Discussion started by: powwm
1 Replies

6. Red Hat

Background process

If I run a process in the back ground like find . -name "abc" & after the process is complete we get a msg like + Done find . -name "abc" & Is it possible to redirect this msg to /dev/null Thanks for your help (1 Reply)
Discussion started by: Ujan
1 Replies

7. UNIX for Dummies Questions & Answers

background process

How, can I hide background process's output? (5 Replies)
Discussion started by: zylwyz
5 Replies

8. Shell Programming and Scripting

background process

Hi, In shell script when I use script1 >> filelog the echo statments of script1 gets printed in the filelog but when I try to run script in background i.e, script1 & >> filelog nothing gets printed in the filelog. Anybody knows whats going on here. thanks (3 Replies)
Discussion started by: k_oops9
3 Replies

9. Shell Programming and Scripting

process in background

Hi Guys, I am facing some weird problem with my shell script. The script shows up a menu and for every ontion internally calls a shell script which start/stop various servers. When I am using '&' while calling the internal shell script so that the server run in the background. For exiting... (1 Reply)
Discussion started by: agoyal
1 Replies

10. Shell Programming and Scripting

capture the process id when starting a background process

Hello all, How do I start a background process and save the process id to a file on my system. For example %wait 5 & will execute and print the process id. I can't figure out how to get it to a file. I've tried: > filename 0>filename 1>filename. Any assistance is most appreciated. Thanks, Jim... (10 Replies)
Discussion started by: jleavitt
10 Replies
Login or Register to Ask a Question