Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 07-25-2011, 09:20 AM   #1
Registered: Nov 2010
Posts: 35

Rep: Reputation: 0
Run multiple bash and php scripts from a bash script

I have written quite a few separate bash & scripts and php scripts that up to now I have run from cron jobs.

However I have to estimate how long each takes to run, before running the next and so it probably takes much longer than necessary to run them all. They have to run in order.

Now there are so many I am thinking it would be better to have a master bash script that would run one after the other, but I am not sure how to get the master script to wait before starting to run the next script.

Is this possible and is there a command that will make the script wait between bash and php scripts , for them to finish, before running the next?
Old 07-25-2011, 11:09 AM   #2
Registered: Nov 2010
Posts: 35

Original Poster
Rep: Reputation: 0
ok, I did a test using the "wait" command and am now embararssed i had to ask, as it seems I just put wait between the calls to each script. i did a little test creating databases in php scripts, returning some records in a bash script, then updating with another php script, then another bash script to return the updated results and it seemed to work fine...

From what I have read, as long as the subscript doesnt call another subscript, the master script will wait if "wait" command is used between script calls.


/usr/bin/php test.php


/usr/bin/php test2.php


echo all done
Old 07-25-2011, 11:11 AM   #3
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,544

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
Well by default you can place them in order in a script and it will execute each as the lat one finishes. The question you need to ask yourself is what do you do if
the prior script fails, ie should the subsequent script get launched or not?
1 members found this post helpful.
Old 07-25-2011, 11:14 AM   #4
Senior Member
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 2,318

Rep: Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947
Run several scripts from one

I am amazed! The usual question is "how can I get all of these scripts to run at the same time?", this time it is how to get them to run in order.

Just start them in order from the main script. Do not background them, spawn off a new shell for them, or anything fancy: just run them from the main script. The default behavior is for each to exit before the next can start.

An advantage is that you can have the master script test exit codes, record start and stop times, and monitor resources (load level perhaps) as needed.
1 members found this post helpful.
Old 07-26-2011, 01:49 AM   #5
Registered: Nov 2010
Posts: 35

Original Poster
Rep: Reputation: 0
Being able to time each script is really useful, I have just put
echo $(date)
after each "wait" at the moment, but I think if I google some date functions, I could get the script to calculate timings and print them after they have all run, which is useful. Especially as after this long process has run, something needs to be run on another server and previously i was doing it the next day, but this way I can have a much more accurate idea of how long everything is taking and get the next process to start at an appropriate time.

Load times is another thing that would be useful for me to monitor during the process, as it does at times cause a heavy load.

Thanks for the tip from Grail as well that if one fails for some reason I need to think about how to have that covered, some if statement maybe, I will have a read around today, not having heard about "exit codes" before, the phrase sounds like they may be able to be used to do that somehow.

The reason it has been done this way, was due to when I started writing the scripts, I had never written a bash script, so I was doing bit by bit and each time I had to resort to php, I was ending the bash process and calling the php. When I tried running a php script from within bash, the next bash command was executing before the php had finished, but it may be I had made some other error. At least with the various script files, it keeps the process structure clear in my mind for now, but in the future maybe I will put them all together so that they run in the foreground.

Old 07-26-2011, 02:40 AM   #6
Registered: Jun 2011
Distribution: Slackware
Posts: 513

Rep: Reputation: 223Reputation: 223Reputation: 223
Originally Posted by charu View Post
...but I think if I google some date functions, I could get the script to calculate timings and print them after they have all run, which is useful.
Perhaps the shell builtin time and /usr/bin/time can help you there. Note that both have the same name, so you would have to specify the full path to run the time command.

/usr/bin/time <command>
For GNU time command.

time <command>
For Bash time builtin.
2 members found this post helpful.


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
[SOLVED] Can't run some bash scripts porphyry5 Linux - Newbie 18 05-10-2011 01:22 PM
KDE 4 Autostart Program Bash Script to Write Autostart Bash Scripts for You! jdmcdaniel3 SUSE / openSUSE 1 05-03-2010 06:17 AM
run shell script bash from browser with php achilles Programming 10 08-02-2007 03:44 PM
Using PHP, run bash script and see results on Server's Monitor xmrkite Linux - Software 6 10-17-2006 01:28 PM
bash script: run multiple jobs in bg, & kill them separately? mattengland Programming 1 03-26-2006 05:07 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:05 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration