phpN00b
phpN00b

Reputation: 807

How to batch execute PHP scripts from one PHP script?

I have a series of PHP scripts that I want to run in a particular order. I tried using

<?php
    exec('file1.php');
    exec('file2.php');
    exec('file3.php');
?>

to accomplish this, but just got a series of errors. If I run them from the command line, they all work fine. How to fix this problem?

Upvotes: 0

Views: 1637

Answers (3)

Whitey
Whitey

Reputation: 68

You can run it from the command line from your scripts, assuming you have root access.

Example:

<?php
    system("php -f path/to/your/script/file1.php");
    system("php -f path/to/your/script/file2.php");
    system("php -f path/to/your/script/file3.php");
?>

I haven't tested it, but it should work :)

Upvotes: 0

Adam Wright
Adam Wright

Reputation: 49376

If the state of each script is well isolated (i.e. not clashing function/class names and global variables), you can just include each of them in turn.

include("file1.php");
include("file2.php");
...

This will also ensure you don't spin up multiple PHP interpreters.

Upvotes: 2

DigitalRoss
DigitalRoss

Reputation: 146073

system('php file1.php')

Or, just use a shell script if on *nix.

Upvotes: 0

Related Questions