LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 08-28-2004, 01:34 AM   #1
irfanhab
Member
 
Registered: Jan 2004
Location: Pakistan
Distribution: OpenSuse 10.2, Slackware 11, Solaris 10
Posts: 415

Rep: Reputation: 34
Parallel Execution Capable Scripts?


Hi,

Can you make shell scripts which can perform to tasks at the same time! something like a parallel execution capable scripts in bash scripting, or do you have to make a program for that.
 
Old 08-28-2004, 02:15 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Yes, you can, just like you can from a shell prompt. Inside your script, you can execute a command, and tell the script to execute it in the background with the '&' character at the end of the line. You can do that to spawn a specific program or to launch your own sub-scripts.

If the main script needs to wait until the spawned processes are finished, then you'll need to make some form of communication. It could be as simple as sitting in a loop waiting for a file to exist from each sub-process. Once they are all accounted for, then the sub-processes are finished, and the main script can continue.
 
Old 08-28-2004, 02:45 AM   #3
irfanhab
Member
 
Registered: Jan 2004
Location: Pakistan
Distribution: OpenSuse 10.2, Slackware 11, Solaris 10
Posts: 415

Original Poster
Rep: Reputation: 34
Any sample script Dark_Helmet?
 
Old 08-28-2004, 04:59 AM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Well.... pretty much like I said. Here's a very simple example.

Main script:
Code:
#!/bin/bash

# Spawn off two sub-scripts to perform some function.
# This script will wait until they both signal their job is complete
#   by creating an empty file named /var/run/sub_script1.done
#   and /var/run/sub_script2.done

# First, make sure the "done" files don't exist
# In case there was an abrupt end and we didn't
#   get a chance to cleanup the files
sub_script1_file="/var/run/sub_script1.done"
sub_script2_file="/var/run/sub_script2.done"

rm -f ${sub_script1_file} ${sub_script2_file}

# Launch the scripts
/path/to/directory/containing/sub_script1.bash ${sub_script1_file} &
/path/to/directory/containing/sub_script2.bash ${sub_script2_file} &

while [ ! -e ${sub_script1_file} -a ! -e ${sub_script2_file} ] ; do
  sleep 10
done

# Both sub-scripts are done, clean up the .done files
#   and continue processing
rm -f ${sub_script1_file} ${sub_script2_file}

# Do more stuff...
sub_script1.bash:
Code:
#!/bin/bash

# All this script does is write a list of all the files in the /etc
#   directory and all subdirectories
find /etc -type f > /var/run/sub_script1.data

# The find command is finished. Create an empty file to
#   signal we're done. We were given the path and the
#   name of the file to create as a command-line argument
touch $
sub_script2.bash:
Code:
#!/bin/bash

# This script grabs the slashdot.org homepage
cd /tmp
wget http://slashdot.org

# We're done, create the "done" file that was given as
#   a command-line argument
touch $1
 
Old 08-28-2004, 05:47 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
Dark_Helmet, your example can be strongly simplified and enhanced by the use of the wait shell builtin command.

1) Remove the touch $1 in both subscripts (temporary file no more needed)

2) The main script is modified like this:

Code:
#!/bin/bash

# Spawn off two sub-scripts to perform some function.
# This script will wait until they both signal their job is complete

# Launch the scripts
/path/to/directory/containing/sub_script1.bash &
/path/to/directory/containing/sub_script2.bash &

# wait for the scripts to complete
wait

# Do more stuff...
 
Old 08-28-2004, 07:04 AM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Yeah, that's hands-down better than mine. I should have looked up the documentation for the wait command. I'm used to putting "wait pid" that I didn't even think about it, and I figured grep'ing ps output for child process pid's would have been going overboard.

Thanks for pointing it out.
 
  


Reply


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
I want Fedora Core 3 be dual-monitor capable andersgt Linux - Hardware 2 02-24-2005 09:07 PM
802.1p capable NIC with Linux Drivers elrusso Linux - Wireless Networking 0 11-11-2004 03:59 AM
It's cron capable to do this? graveworm Linux - Software 1 09-27-2004 07:18 AM
Is Linare dual processor capable SuperDuper Linare 2 09-21-2004 09:29 AM
Execution of PERL scripts in Linux which is compiled in WINDOWs environment to_veera Linux - Software 3 06-18-2004 09:50 AM


All times are GMT -5. The time now is 10:27 PM.

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