LinuxQuestions.org
Review your favorite Linux distribution.
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 06-21-2007, 08:33 PM   #1
guttersnipe
LQ Newbie
 
Registered: Jun 2007
Distribution: Gentoo, Ubuntu
Posts: 23

Rep: Reputation: 0
bash script backgrounding


I've been experimenting with backgrounding in bash scripts, and I cannot seem to get it to call a script, background it, and move on without the completion of that script.

I have the following files:

caller.sh:
Code:
#!/bin/bash
`./called1.sh &`
`./called2.sh &`
exit
called1.sh:
Code:
#!/bin/bash
i=0
while [ "$i" -lt 5 ]
do
  echo "ONE" > /dev/tty
  sleep 5
  i=`expr $i + 1`
done
called2.sh:
Code:
#!/bin/bash
i=0
while [ "$i" -lt 5 ]
do
  echo "TWO" > /dev/tty
  i=`expr $i + 1`
done
The intention was such that when I execute caller.sh, it would then execute called1.sh in the background and THEN execute called2.sh immediately (without waiting for called1.sh to finish). If this would happen, I should expect the following result:

Code:
#./caller.sh
ONE
TWO
TWO
TWO
TWO
TWO
ONE
ONE
ONE
ONE
..but instead, I receive the following output:

Code:
#./caller.sh
ONE
ONE
ONE
ONE
ONE
TWO
TWO
TWO
TWO
TWO
This indicates to me that the caller.sh script is not backgrounding the called1.sh script. Instead, it waits until the caller1.sh script finishes and THEN it calls caller2.sh.

What must I do to make it run the two scripts at once such that I obtain the former of the two outputs? Any help will be appreciated.

Thanks in advance.

Last edited by guttersnipe; 06-21-2007 at 08:37 PM.
 
Old 06-21-2007, 08:48 PM   #2
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
The trouble is your use of backticks. The `some_command` notation means "take the output of some_command and use it like it was part of the script". Thus, bash is waiting on some_command to complete, as it needs it output for that line. Just remove the backticks and you should be okay.
 
Old 06-21-2007, 08:55 PM   #3
guttersnipe
LQ Newbie
 
Registered: Jun 2007
Distribution: Gentoo, Ubuntu
Posts: 23

Original Poster
Rep: Reputation: 0
I changed the contents of caller.sh to:
Code:
#!/bin/bash
  ./called1.sh &
  ./called2.sh &
exit
...and I got the output of:
Code:
#.caller.sh
#ONE
TWO
TWO
TWO
TWO
TWO
ONE
ONE
ONE
ONE
The script works beautifully. Thank you!
 
Old 06-21-2007, 08:56 PM   #4
3point2
LQ Newbie
 
Registered: Jan 2006
Distribution: Slackware 11.0
Posts: 23

Rep: Reputation: 15
why do you have backticks around your calls to called1.sh and called2.sh? backticks to bash mean "substitute with the output from the command". Try
Code:
#!/bin/bash
./called1.sh &
./called2.sh &
exit
and it'll work fine.
 
  


Reply

Tags
bash, linux, nix, script, shell, unix


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 05:11 AM
[bash] having trouble debugging this bash script. jons Programming 4 02-08-2007 07:51 AM
Bash script hangs upon starting another script in the background masea2 Linux - Software 4 11-13-2006 06:18 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 01:20 AM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 06:34 AM


All times are GMT -5. The time now is 01:12 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration