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'

JoeyBall
  • 43
  • 3
  • I think you need to write ini_set('max_execution_time', 0); this line in process.php – Rahul Dec 26 '17 at 12:11
  • I already did that. It's already there. Thanks. – JoeyBall Dec 26 '17 at 12:13
  • Possible duplicate of [Limit execution time of an function or command PHP](https://stackoverflow.com/questions/1176497/limit-execution-time-of-an-function-or-command-php) – Rahul Dec 26 '17 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. – Wiguno Dec 26 '17 at 13:52

1 Answers1

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!

Akshay Prabhakar
  • 430
  • 4
  • 13