I am trying to build a backup app for my website, and as part of that, I want to launch a PHP script in the background. That way I don't have to worry about it timing out. The strange part is that it works fine on my local computer, but when I copy it to my Hostgator website, it goes crazy.

I've cobbled together a simple example...

First, I created a simple PHP script:

```
<?php
    echo 'Current PHP version: ' . phpversion() . "\n";
    echo 'Now:       '. date('Y-m-d') ."\n";
    echo "This is created by phpbg-back.php\n\n";
    echo "Current file: " . __FILE__;
?>

```

I can run that script by SSH'ing into my host and running this command:

```
# php phpbg-back.php 
Current PHP version: 5.4.36
Now:       2015-01-16
This is created by phpbg-back.php

....

```

So, the script seems to work fine. Now, I created a page that will launch the script as a background process:

```
<?php
    if(isset($_GET["schedule"])) {  ?>
<html>
    <head>
        <title>Scheduling PHP Process</title>
    </head>
    <body>
        <h1>Schedule....</h1>
        <p>We are scheduling the process right now.  Here is the output:</p>
        <pre><?php
            $command = "php phpbg-back.php > phpbg-out.txt 2>&1 &";
            echo "Command: " . $command . "\n";
            echo shell_exec($command);
        ?></pre>

        <a href="phpbg-front.php">Go back to the front page and check the output</a>
    </body>
</html>
<?php
    } else {
        echo "Invalid -- the page should have the schedule parameter set.\n\n";
        echo "Current file: " . __FILE__;
    }
?>

```

Finally, I created a nice front page that should be able to view the output:

```
<html>
    <head>
        <title>Example: PHP Background Process</title>
    </head>
    <body>
        <h1>Example: PHP Background Process</h1>

        <h2>Schedule</h2>
        <p>Click this link to schedule the background process to run</p>
        <a href="phpbg-sched.php?schedule=true">Schedule</a>

        <h2>Output</h2>

        <?php if(file_exists('phpbg-out.txt')) { ?>
            <pre><?= file_get_contents('phpbg-out.txt') ?></pre>
        <?php } else { ?>
            <p>No output file found</p>
        <?php } ?>
    </body>
</html>

```

The problem is that when the script runs through this page it seems to totally ignore the script that I am passing to it. It runs the current PHP page instead. So, the output looks more like this (phpbg-out.txt):

```
Content-type: text/html^M
^M
<html>
    <head>
        <title>Scheduling PHP Process</title>
    </head>
    <body>
        <h1>Schedule....</h1>
        <p>We are scheduling the process right now.  Here is the output:</p>
        <pre>Command: php phpbg-back.php > phpbg-out.txt 2>&1 &
</pre>

        <a href="phpbg-front.php">Go back to the front page and check the output</a>
    </body>
</html>

```

I've tried all sorts of things to get it to work. I tried adding bash -c in front of the command. I've used the -f switch to make it pick the PHP script. I'm not sure what else to try.