2

I have a PHP file and I'm trying to run a file in the background. Unfortunately the file seems to time out after sometime.

This is what I have tried:

   ini_set('max_execution_time', 0);
    exec('php  process.php /dev/null &');

The programme seems to be working in the background, but unfortunately times out a few seconds / minutes later.

I'm getting 'Error 502'

4
  • I think you need to write ini_set('max_execution_time', 0); this line in process.php Commented Dec 26, 2017 at 12:11
  • I already did that. It's already there. Thanks. Commented Dec 26, 2017 at 12:13
  • Possible duplicate of Limit execution time of an function or command PHP Commented Dec 26, 2017 at 12:17
  • 1
    I just want to note that running a php script with no timeout is dangerous. Depending on what you need, you can run a scheduler every some time, and still keep the timeout. Commented Dec 26, 2017 at 13:52

1 Answer 1

0

Doing some experimentation with exec and shell_exec I have uncovered a solution that worked perfectly! I choose to use shell_exec so I can log every notification process that happens (or doesn't). (shell_exec returns as a string and this was easier than using exec, assigning the output to a variable and then opening a file to write to.)

I'm using the following line to invoke the email script:

shell_exec("/path/to/php /path/to/send_notifications.php '".$post_id."' 'alert' >> /path/to/alert_log/paging.log &");

It is important to notice the & at the end of the command (as pointed out by @netcoder). This UNIX command runs a process in the background.

The extra variables surrounded in single quotes after the path to the script are set as $_SERVER['argv'] variables that I can call within my script.

The email script then outputs to my log file using the >> and will output something like this:

[2011-01-07 11:01:26] Alert Notifications Sent for http://alerts.illinoisstate.edu/2049 (SCRIPT: 38.71 seconds)
[2011-01-07 11:01:34] CRITICAL ERROR: Alert Notifications NOT sent for http://alerts.illinoisstate.edu/2049 (SCRIPT: 23.12 seconds)

Thanks!

Sign up to request clarification or add additional context in comments.

Comments

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.