How will these subshell commands behave?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How will these subshell commands behave?
# 1  
Old 01-14-2014
How will these subshell commands behave?

Hello,

I am firing off some scripts from a main script,
Code:
   cd B/
   ./EV_B_m0-m200_hex1.sh &
   ./EV_B_m0-m200_hex2.sh &
   wait
...more

It would be useful to put a bit of time between the two to clean up the output to the terminal.

I think this would work,
Code:
   cd B/
   ./EV_B_m0-m200_hex1.sh &
   sleep 5
   ./EV_B_m0-m200_hex2.sh
...more

but this would not guarantee that the second command would finish before the script continues.

I'm not sure if this would work,
Code:
   cd B/
   ./EV_B_m0-m200_hex1.sh &
   sleep 5
   ./EV_B_m0-m200_hex2.sh &
   wait
...more

As I read it, the first command would execute in a sub-shell and the script would continue and sleep for 5. After that, the third command will execute in a subshell and then the script will wait for both sub-shells to return before it moves on.

Is this right??? Is there a better way to do this kind of thing??

Thanks,

LMHmedchem
# 2  
Old 01-14-2014
When you put a command in background you lose control of exactly when it runs. Meaning no guarantees of order, ever.

If you want some of its output to come first, how about you script it so the bits you want to come first don't run in background?
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 01-14-2014
These scripts don't need to run in any particular order, there output is to files and each script is independent. They just need a bit of time in between when they start, and they both need to finish before the script moves on. The issue is that these two scripts (hex1, hex2) fire off six instances of an application and each instance reports some start up data back to the terminal. Because of the way that they are started, all of that start up data is mixed up and garbled. I am trying to debug a bit and can't read printout to the terminal that I need to look at.

Each script starts three instances of the app and there is a bit of sleep time between each, so if I run just one script, that output is clear. It is only when I run both scripts at the same time that I get this mess. I could not run the second script in the background and wait a bit before starting it, but I need to make sure that both scripts finish before moving on.

If I do what I posted,
Code:
   cd B/
   ./EV_B_m0-m200_hex1.sh &
   sleep 30
   ./EV_B_m0-m200_hex2.sh &
   wait

is this legal syntax? Will this start up hex1, wait for 30, start hex2, and then wait for both hex1 and hex2 to finish before moving on?

LMHmedchem
# 4  
Old 01-14-2014
Yes your syntax is correct and typically hex2 will be run 30 seconds after hex1 was started.

As Corona688 pointed out you are at the mercy of the scheduler and system resources, etc. as to when the jobs will actually be allocated CPU and move into a running state. But since you are only worried about terminal overrun you can safely ignore this, the system would have to be in a pretty bad way for a background job to take any more than a second or two to start.
This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 01-15-2014
Well the code above did work and I got clear printout from all six instances of my app. I was able to read the path that couldn't be found and correct the bug.

Thanks for the help!!!

LMHmedchem
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Why do awk command line vars behave the way they do?

This came up a little in another thread. Can someone explain some why awk (I happen to use gawk) behaves as follows: $ cat file aaa $ awk 'BEGIN {print x}' x=1 $ awk x=1 'BEGIN {print x}' awk: fatal: cannot open file `BEGIN {print x}' for reading (No such file or directory) $ awk -v... (6 Replies)
Discussion started by: hanson44
6 Replies

2. Shell Programming and Scripting

Basename in subshell

Hi All, I would like to improve my bash scripting skill and found a problem which I do not understand. Task is to search and print files in directory (and subdirecories) which contains its own name. Files can have spaces in name. This one works fine for files in main directory, but not for... (4 Replies)
Discussion started by: new_item
4 Replies

3. Shell Programming and Scripting

getopts in a subshell

Hello, I've a little problem with one of my ksh scripts and I manage to narrow it to the script here: #!/bin/ksh writeLog() { paramHandle="unknown" OPTIND=1 while getopts :i: option $* do case $option in i) paramHandle=${OPTARG} ;; esac done echo... (2 Replies)
Discussion started by: Dahu
2 Replies

4. Shell Programming and Scripting

Killing a subshell

I am calling a script from with another script and reading its output one line at a time (using <childscript> | while read line) in the parent script. If the output exceeds a predefined number of lines I want to kill the child shell from within the parent shell. I decided to print the process ID... (2 Replies)
Discussion started by: slash_blog
2 Replies

5. Shell Programming and Scripting

mail command behave odd

hi, The following mail cmd executed successfully. mailx -s 'subject' user@company.com < testfile.dat However When i include this mail cmd in shell script it behave odd. Getting an error message mailx comand not found. (2 Replies)
Discussion started by: zooby
2 Replies

6. Shell Programming and Scripting

Why does IF loop behave differently in different shells

Hi , I have a script that compares two string and prints the larger string , This is an extract of a biggers script that i have. #! /bin/ksh DT_STRING_CMP=20081221223440 DT_STRING=20071221223440 if ; then echo "20081221223440" fi echo... (5 Replies)
Discussion started by: amit1_x
5 Replies

7. Shell Programming and Scripting

Why does IF loop behave differently in different shells

Hi , I have a script that compares two string and prints the larger string , This is an extract of a biggers script that i have. #! /bin/ksh DT_STRING_CMP=20081221223440 DT_STRING=20071221223440 if ; then echo "20081221223440" fi echo... (1 Reply)
Discussion started by: amit1_x
1 Replies

8. UNIX for Dummies Questions & Answers

how to make ssh to behave as rsh

hi frnds, how to make the ssh service to behave as rsh.as we know ssh asks for passwd whereas the rsh doesnt.so how can i perform the followin operation without being asked for passwd. lets say i want to run the command "ls" on "remote_terminal" $ssh remote_terminal ls the above should work... (1 Reply)
Discussion started by: mxms755
1 Replies

9. Shell Programming and Scripting

Subshell Question

The profile of the user is empty. Then before I run the script I want I run a parameter file that populates the variables for oracle. ORACLE_HOME ORACLE_BASE ORACLE_SID PATH etc ... But it seems that these variables are not making it to the shell I am in because when I do an echo on... (6 Replies)
Discussion started by: lesstjm
6 Replies
Login or Register to Ask a Question