If I have 2 php scripts in an sh file and I run the sh f开发者_StackOverflow中文版ile from the command line or from cron, can I be sure that the second script will only start running after the first script has completed?
In test.sh I have
php test1.php
php test2.php
And I run test.sh. Can I be sure that test2.php will not start before test1.php completes?
If "test1.php" daemonizes itself, then "test2.php" may execute before the former has been terminated. If you want to avoid this scenario you can use something like the following:
#!/usr/bin/env bash
test1.php
PIDLIST=$(ps axwww | fgrep -v grep | fgrep "test1.php" | awk '{ print $1 }')
while [ ! -z "$PIDLIST" ]; do
sleep 1
PIDLIST=$(ps axwww | fgrep -v grep | fgrep "test1.php" | awk '{ print $1 }')
done
test2.php
Assuming that at any given time, only one test1.php can be running on the system.
You got what you got - php will not quit unless test1.php is finished, then it will launch test2.php. However, this will not work for any wrappers like fastcgi.
You can use lots of parallel process management tools.. see my questiong at SF - https://serverfault.com/questions/259491/shell-script-run-a-batch-of-n-commands-in-parallel-wait-for-all-to-finish-run
set some variable in test1.php
and send that variable to test2.php
check that variable value in test2.php
if that is not set then do exit()
Forgive me if I am ignorant (beginner sh user) but can you not use && to separate commands so that the 2nd command is only executed after the first one returns a 0 value (success)?
(command1) && (command2);
EDIT: Or using || to run command2 if command 1 returns non-zero
精彩评论