0

I'm trying to run a background PHP process with shell_exec() as a part of WordPress plugin.

This is the sample.

/* Plugin Name: Sample Background Process */

add_action('init', 'Sample_Background_Process');    
function Sample_Background_Process() {
    $file = __DIR__ . '/log.html';
    $current = date('l jS \of F Y h:i:s A') . ': accessed<br />';
    file_put_contents($file, $current, FILE_APPEND);
}

It creates a log in the plugin folder and whenever a page gets access it appends some text to it.

Then, I added this line, shell_exec('php "' . ABSPATH . '/index.php" > /dev/null 2>/dev/null &');. So a background process should access the page and run the function which appends text to the log file. But it doesn't seem to be doing it. I expect one page load produces two lines in the log file. But it only adds one line, which means a background process is not run or WordPress does not do anything if it is called like this.

add_action('init', 'Sample_Background_Process');    
function Sample_Background_Process() {
    $file = __DIR__ . '/log.html';
    $current = date('l jS \of F Y h:i:s A') . ': accessed<br />';
    file_put_contents($file, $current, FILE_APPEND);
    shell_exec('php "' . ABSPATH . '/index.php" > /dev/null 2>/dev/null &');
}

What am I doing wrong?

The path, php in shell_exec() is altered by the path to php.exe in my actual script since I'm testing on Windows server. It runs fine in other PHP scripts.

Thanks.

5
  • Are you sure safe mode is disabled on the Windows server? Commented Sep 15, 2012 at 10:54
  • if( ini_get('safe_mode') ) returns false. Commented Sep 15, 2012 at 10:58
  • Try turning error reporting on and running just that single line on it's own. This should give you some clue as to what's going on. You may also want to print out the string you're passing to shell_exec and executing it manually. Commented Sep 15, 2012 at 11:00
  • It's strange that the line worked in other scripts. I have to figure it out. Thanks for the comment. Commented Sep 15, 2012 at 11:02
  • I tested it again and found /dev/null 2>/dev/null doesn't work at all in Windows. I thought it would have worked but as I view the other test scripts that I have created before, they were using 2>&1. So, thanks, all became clear now. Commented Sep 15, 2012 at 11:52

1 Answer 1

2

AFAIK ( as far as i know ) /dev/null doesn't exist in windows just use NUL... for example

shell_exec('php "' . ABSPATH . '/index.php" > NUL &');
Sign up to request clarification or add additional context in comments.

3 Comments

It seems to work. Thanks. And I realized that it creates an infinite process loop.
well... the infinite loop isn't what i excepted :)
using shell_exec in windows is a bad idea

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.