LinuxQuestions.org
Review your favorite Linux distribution.
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 07-25-2011, 09:20 AM   #1
charu
Member
 
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
charu
Member
 
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.


Code:
#!/bin/sh

/usr/bin/php test.php
wait

/bin/bash test.sh
wait

/usr/bin/php test2.php
wait

/bin/bash test2.sh
wait

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

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
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
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 5,763

Rep: Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764
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
charu
Member
 
Registered: Nov 2010
Posts: 35

Original Poster
Rep: Reputation: 0
Being able to time each script is really useful, I have just put
Code:
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.

Thanks!
 
Old 07-26-2011, 02:40 AM   #6
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 515

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
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.

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

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


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
[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

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

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