PHP script makes a duplicate - php

I have a long-running PHP script with set_time_limit(0) set. It works very good for 15 minutes (900 sec) but then become something very strange: a second process with the same parameters starting! I see it because I am starting a new log file at the beginning of the script and there is two log files processing same data!
BTW script runs in background from PHP with
exec('wget http://example.com/script.php?id=NNN > /dev/null &');
This instruction normally runs only once, and I can not get what runs it second time after 900 seconds (exact time).

This is because wget has a read time limit of 900sec. After it is reached, the download restarts.
You can set the timeout higher with the --timeout=seconds or the --read-timeout=seconds argument.
Or, you can start it directly from the shell(this way is much better).
Here is a link: wget download options
Here is the shell code(for Linux):
exec('php yourscript.php > /dev/null 2>&1 &');

Related

php cron timeout - run script in background

i have a script in php extract_data.phpand it takes 20 minutes to run the script.
I activated cron to run the script. but the cron has a time of only 30 seconds (this time cannot be increased).
the problem is that i always get the timeout error.
I would like it to appear: file loading... while the script is running.
i test exec("extract_data.php"." > /dev/null &");
does not work
It seems that you have a default configuration for the timeout.
Please take a look here: https://www.php.net/manual/en/function.set-time-limit.php
set_time_limit(0); // To run without timeout.
Or
set_time_limit(20*60); //to allow to run for only 20 minutes

Timeout issue when running background PHP script as web

I've got a PHP script that does some heavy lifting that I'm trying to fire off as a background script using the following code:
exec("script.php > /dev/null 2> /dev/null &");
What happens:
When I run the code above as part of my web app, the script quits
after about a minute.
When I run the code as part of my web app without the final
ampersand, the job runs fine - but exec() waits for the script to
complete before loading the next page, defeating the purpose as the user stares at an unresponsive page.
When I run the shell command script.php > /dev/null 2> /dev/null & as myself from the console with the final ampersand, the job runs fine.
When I run the shell command from the console as web, the job stops running after about a minute.
I've tried piping my output to logfiles: script.php > /home/public/tmp/output.txt 2> /home/public/tmp/errors.txt &. Output looks normal, I don't get any errors. The script just stops working.
The basic rule seems to be: If run as a foreground process as web or as me, it'll complete. If run as a background process as web, it stops working after about a minute.
I'm not running this as a cronjob because my host (NearlyFreeSpeech) can only run cronjobs once an hour, which is more than I want to make users wait for when the job only takes a couple minutes- it might as well fire when users initiate it.
The subscript starts with set_time_limit(60 * 60 * 4); so this shouldn't be a matter of PHP timing out.
set_time_limit
does not include shell-execution time.
http://php.net/manual/en/function.set-time-limit.php
Try using of the code examples in the comments on that site.

Limit execution time of shell_script in PHP

I am calling a shell script in Linux using a PHP script, I do the following:
shell_exec('./shell_script.sh');
after this the PHP script continues on.. All this works as expected.
However, sometimes the shell_script doesn't finish executing for whatever reason, so here is the question:
How can I terminate the shell_script.sh after being executed for x amount of time?
Should this be dealt with in PHP itself somehow (dont think that's possible in this instance) or should it be done in the .sh itself?
So just after the:
#!/bin/bash
at the beginning of the .sh, is there something I can put for it to terminate if execution time exceeds say 20 seconds perhaps?
I don't know if you can do it in php, and there may exist better solutions in bash, but this is what you could do:
This is the line you can put immediately after #!/bin/bash to kill the current script after 20 seconds:
(sleep 20 && kill $$) &
bash replaces $$ with the pid of the current script.
You could use something like this:
# start timer
( sleep $TIMEOUT ; kill $$ ) 2>/dev/null &
TIMER_PID=$!
# "payload"
echo hello
# cancel timer
kill $TIMER_PID
YMMV though, in my tests the part that is supposed to cancel the timer sometimes doesn't kill sleep and the program waits until the timeout finishes.

Running cron job from browser

I have several cron jobs that run automatically, and I was requested to add a button that says 'run now' in the browser... Is this possible? Some of these cron jobs need to be executed from command line as they take around 15 minutes... Is it possible to execute them from the browser, not as a normal php function but somehow trigger an external php from the browser?
You're looking for the exec() function.
If it's a 15 minute task, you have to redirect its output and execute in in the background. Normally, exec() waits for the command to finish.
Example: exec("somecommand > /dev/null 2>/dev/null &");

PHP CLI process not terminating when done

I have this in one PHP file:
echo shell_exec('nohup /usr/bin/php -f '.CRON_DIRECTORY.'testjob.php > /dev/null 2>&1 &');
and in testjob.php I have:
file_put_contents('test.txt',time()); exit;
And it all runs just dandy. However if I go to processes it's not terminating testjob.php after it runs.
(Having to post this as an answer instead of comment as stackoverflow still won't let me post comments...)
Works for me. I made testjob.php exactly as described, and another file test.php with just the given line (except I removed CRON_DIRECTORY, because testjob.php was in the same directory for me).
To be sure I was measuring correctly, I added "sleep(5)" at the top of testjob.php, and in another window I have:
watch 'ps a |grep php'
running. This happens:
I run test.php
test.php exits immediately but testjob.php appears in my list
After 5 seconds it disappears.
I wondered if shell might matter, so I switched from bash to sh. Same result.
I also wondered if it might be because your outer script is long-running. So I put "sleep(10)" at the bottom of test.php. Same result (i.e. testjob.php finishes after 5 seconds, test.php finishes 5 seconds after that).
So, unhelpfully, your problem is somewhere other than the code you've posted.
Remove & from the end of your command. This symbol says nohup to continue running in background, thus shell_exec is waiting for task to complete... and waiting... and waiting... till the end of times ;)
I don't even understan why would you perform this command with nohup.
echo shell_exec('/usr/bin/php -f '.CRON_DIRECTORY.'testjob.php > /dev/null 2>&1');
should be enough.
You're executing PHP and make that execution a background task. That means it will run in background until it is finished. shell_exec will not kill that process or something similar.
You might want to set an execution limit, PHP cli has a setting of unlimited by default. See as well set_time_limit PHP Manual;
So if you wonder why the php process does not terminate, you need to debug the script. If that's too complicated and you're unable to find out why the script runs that long, you might just want to terminate the process after some time, e.g. 1 minute.

Categories