LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Run multiple bash and php scripts from a bash script (https://www.linuxquestions.org/questions/programming-9/run-multiple-bash-and-php-scripts-from-a-bash-script-893642/)

charu 07-25-2011 09:20 AM

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?

charu 07-25-2011 11:09 AM

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


grail 07-25-2011 11:11 AM

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?

wpeckham 07-25-2011 11:14 AM

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.

charu 07-26-2011 01:49 AM

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!

Diantre 07-26-2011 02:40 AM

Quote:

Originally Posted by charu (Post 4425247)
...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.


All times are GMT -5. The time now is 07:41 PM.