Sponsored Content
Full Discussion: Trapping Background Process
Top Forums Shell Programming and Scripting Trapping Background Process Post 302959722 by Don Cragun on Thursday 5th of November 2015 01:06:50 PM
Old 11-05-2015
Quote:
Originally Posted by Showdown
I am calling one shell script from my wrapper shell script
The pseudo code for the same is

Code:
  nohup  sh script1.sh
  script.sh is  calling  script2.sh inside it  5 times followed by a  wait command 
  
  
  #1 /bin/bash
  nohup sh script2.sh 1 &
  nohup sh script2.sh 2 &
  nohup sh script2.sh 3 &
  nohup sh script2.sh 4 &
  nohup sh script2.sh 5 &
  wait.
  connect sql --> stmp success/fail status to error log table.

The sql statement at the bottom of the script should insert a success flag if all the background scriptts are successful.
If one or more background script fails then it should stamp the error message in the errorlog table with the error code and PID.

Can you guys focus some light on the above !!!
Many Thanks
Note that the line in red is just a comment. To have a line like that specify the shell to be used to run the script, you need #! instead of #1 and the #! must appear at the start of the line (with no leading spaces), and there shouldn't be any spaces between #! and the pathname of the shell you want to use.

For the rest of it, I'll let you form the sql, but the following will show you how to get save the PIDs and the exit status of each of the background jobs:
Code:
#!/bin/bash
nohup sh script2.sh 1 &
pid1=$!
nohup sh script2.sh 2 &
pid2=$!
nohup sh script2.sh 3 &
pid3=$!
nohup sh script2.sh 4 &
pid4=$!
nohup sh script2.sh 5 &
pid5=$!
wait $pid1
ec1=$?
echo "$pid1 exited with exit code $ec1"
wait $pid2
ec2=$?
echo "$pid2 exited with exit code $ec2"
wait $pid3
ec3=$?
echo "$pid3 exited with exit code $ec3"
wait $pid4
ec4=$?
echo "$pid4 exited with exit code $ec4"
wait $pid5
ec5=$?
echo "$pid5 exited with exit code $ec5"

 

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

cause a process to be in background

Hi My script - main.sh, execute program1.sh in background and program2.sh in foreground . program1.sh holds the process ID of program2. program1.sh after a while needs to move the process of program2.sh to background. How? (0 Replies)
Discussion started by: avnerht
0 Replies

3. 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

4. Shell Programming and Scripting

background process

can anybody plz tell me how can i find the background processes running. (2 Replies)
Discussion started by: Raom
2 Replies

5. Shell Programming and Scripting

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.... (5 Replies)
Discussion started by: chella
5 Replies

6. Shell Programming and Scripting

Background Process.

How to create a backgrond processes ? (5 Replies)
Discussion started by: anupdas
5 Replies

7. 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

8. 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

9. 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

10. 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
nohup(1)						      General Commands Manual							  nohup(1)

NAME
nohup - run a command immune to hangups SYNOPSIS
command [arguments] DESCRIPTION
executes command with hangups and quits ignored. If output is not redirected by the user, both standard output and standard error are sent to If is not writable in the current directory, output is redirected to otherwise, fails. If a file is created, the file's permission bits will be set to If output from is redirected to a terminal, or is not redirected at all, the output is sent to EXTERNAL INFLUENCES
Environment Variables determines the language in which messages are displayed. If is not specified in the environment or is set to the empty string, the value of is used as a default for each unspecified or empty vari- able. If is not specified or is set to the empty string, a default of "C" (see lang(5)) is used instead of If any internationalization variable contains an invalid setting, behaves as if all internationalization variables are set to "C". See environ(5). International Code Set Support Single- and multi-byte character code sets are supported. EXAMPLES
It is frequently desirable to apply to pipelines or lists of commands. This can be done only by placing pipelines and command lists in a single file, called a shell script. To run the script using features apply to the entire contents of file. If the shell script file is to be executed often, the need to type can be eliminated by setting execute permission on file. The script can also be run in the background with interrupts ignored (see sh(1)): file typically contains normal keyboard command sequences that one would want to continue running in case the terminal disconnects, such as: WARNINGS
Be careful to place punctuation properly. For example, in the command form: applies only to command1. To correct the problem, use the command form: Be careful of where standard error is redirected. The following command may put error messages on tape, making it unreadable: whereas puts the error messages into file EXIT STATUS
The following exit values are returned: The command specified by command was found but could not be invoked An error occurred in the nohup utility or the specified command could not be found Otherwise, the exit status of nohup will be that of the command specified. SEE ALSO
chmod(1), nice(1), sh(1), signal(5). STANDARDS CONFORMANCE
nohup(1)
All times are GMT -4. The time now is 06:40 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy