LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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-20-2004, 05:19 PM   #1
michael_util
Member
 
Registered: Feb 2004
Posts: 47

Rep: Reputation: 15
fastest way to execute another bash script ??


Hello,

I have a bash script the checks a directory ever 1 (for testing) and if a file(s) is found if gets the old file and then executes another script by"

if condition

/path/postscript arguments ....

fi

I seem to hit a bottleneck of some short ... my logs show that every 1-2 seconds I am calling the other script.

But I can never get more then 40-45 instances of the postscript to run ... I have a max value of 99 for testing so I want to try and hit that mark.

Any suggestions on how I could increase the execution time ??

Michael.
 
Old 08-20-2004, 10:19 PM   #2
foo_bar_foo
Senior Member
 
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 53
the only think i can think of is to redirect stderr and stdout from the second script to /dev/null
 
Old 08-21-2004, 10:57 AM   #3
cracauer
Member
 
Registered: Jul 2004
Location: Boston, MA, USA
Distribution: FreeBSD, Debian/AMD64, Ubuntu/i386
Posts: 59

Rep: Reputation: 15
There must be something inside that script making it slow. Calling a minimal script on my 450 MHz Pentium-II with FreeBSD-4.x one tousand times takes less than two seconds.

In general, you can speed things up by not calling, but sourcing in new script. But watch for side-effects, variable settings are effective in the parent script and exit() will exit the outer shell.

That means you write
. foobar/newscript.sh
instead of
sh foobar/newscrip.sh

That is called "sourcing" the new script instead of "executing". Doing so is making it a factor of 16 faster for me.

But obviously the target script needs to be written with this in mind because of the side effects.

Now, here's the real MyCoy. You can shield yourself from the side effects by executing a subshell first:
(. foobar/newscript.sh)

That is much slower than just sourcing, but it is still twice as fast as just calling the script. The reason is that you have to fork() which is expensive but at least you don't exec(). Since the minimal script is almost empty this cuts the systemcalls in half.

Again, here's the timings:
executing: 500 calls/second
sourcing: 8000 calls/second
sourcing in subshell: 1000 calls/second
 
Old 08-23-2004, 10:04 AM   #4
michael_util
Member
 
Registered: Feb 2004
Posts: 47

Original Poster
Rep: Reputation: 15
Hello,

After placing the script in debig mode I found a performance problem and corrected the issue. It is much better now. Thanks for all the pointers.

Michael.
 
  


Reply



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
how to execute bash scripts from C????? :( cho Programming 2 06-19-2005 07:15 PM
Execute bash through a symlink? Agento- Linux - General 10 04-18-2005 01:51 PM
Bash command $? failed to execute. Linh Programming 7 05-14-2004 11:11 AM
Trying to execute bash script upon login?? mr-d-2003 Linux - Newbie 4 10-30-2003 03:15 PM
execute bash scripts vapor Programming 9 07-14-2003 02:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:23 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
Open Source Consulting | Domain Registration