Cron Job PHP script execution time report - php

My question is simple: I want to know how long a PHP script is taking to execute. On top of this, I am executing it via cron. Now, I could do something via the PHP code itself to get the execution time start/end, however I wondered if there was something via the cron command that I could add to get that emailed to me, in milliseconds?
Currently I am using:
/usr/bin/php -q httpsdocs/folder/script.php > /dev/null 2>&1
Which runs my script and stops all errors/output getting emailed to me. Can I change the above to get the execution time emailed to me somehow?
Thanks

/usr/bin/time /usr/bin/php -q httpsdocs/folder/script.php
| mail -s "Some Subject" you#youremailid.com
:-)

You can use time command like this:
/usr/bin/time /usr/bin/php -q httpsdocs/folder/script.php > /var/log/crontiming

By the given script you can change the cron job execution time.
$aCronList = array();
$result = -1;
exec('crontab -l', $aCronList, $result);
foreach($aCronList AS $item)
{
// Find what you want, replace the times
}
$sCronList = implode(PHP_EOL, $aCronList);
exec('crontab < ' . $sCronList);

Related

exec() hangs when I run bash command in background

I'm trying to run my server from PHP script as a background process, but it's hanging the PHP script anyway. I call it like this:
$exec_result = exec('./myapp option1 option2 &> /dev/null &');
I tried things from PHP hanging while exec() bash script like adding "set -m && " or "shopt -u checkjobs && " but that doesn't help. I also tried to call in exec() my C++ utility that runs command in background (basically just calls std::system with " &"), but that didn't help either. Using "nohup" doesn't change anything. Also, the problem is not in my server because same thing happens when I call "sleep" command.
Calling exactly the same command from bash runs process in background as expected. Honestly I'm so confused and frustrated. What am I doing wrong? Maybe PHP needs some kind of permissions to run a background task? I'm kinda new to Linux.
I'm doing it all from Debian 10 and PHP 7.3 if it matters.
I've managed to fix it, but I have no idea why the new solution works while the old one doesn't. Maybe it has something to do with exec() build-it parser? Both lines work identically in bash so I'm blaming PHP on this.
So, I've replaced
$exec_result = exec('./myapp option1 option2 &> /dev/null &');
with
$exec_result = exec('./myapp option1 option2 > /dev/null 2>&1 &');
and that did it. I've checked it back and forth multiple times and the second line works consistently while the first one fails every time.

Activate Cron job from a PHP page

I googled but didn't found any solution
I have a PHP page that takes 45 minutes to execute.
What I am trying to achieve is:
1. Whenever I run a URL abc.com/test.php the script should check the cron job and activate it (run myscript.php) .
2. And should execute the page until it's complete.
So the PHP should call a cron job and execute it ONLY once when it is requested.Is Cron right approach
I do not want the script below, which i tried This will add a script that runs every day at 9:30am.
exec('echo -e "`crontab -l`\n30 9 * * * /path/to/script" | crontab -');
Why set a new cronjob, if you only want to execute it once?
exec('php -f /path/to/script >> /dev/null 2>&1 &');
This will run the script. Echo all the output into the nowhere and use fork, so it will run in the background and your Request won't wait for a return.

Unix 'at' command and returning a job number

I'm currently using the unix at command to schedule jobs in the future. However, I also want to be able to delete these jobs if I deem it necessary. Is there any way to get the job number immediately after the creation of a job? I know that atq can show a list of pending jobs but I want to be able to get the job number in PHP code immediately after creating the job. I can run the atq command but what if another job is created before I get the last job in the queue? Then I don't have the correct job number.
Long story short, is there any way after creating a job with the at command that I can get the job number in return without using the atq command?
For example, my code is:
exec('echo /usr/bin/php -f /home/site/public_html/test.php | sudo /usr/bin/at now');
I'm currently looking over command substitution that one comment suggested to figure out how to get the job number from this php exec() code.
When I try
var=$(echo /usr/bin/php -f /home/site/public_html/test.php | sudo /usr/bin/at now)
in the shell and echo $var I get an empty line. Why doesn't var hold the output?
$x=exec("echo sleep|at 14:00 2>&1");
$match="";
if(preg_match("/^job\s*(\d+)\s*/",$x,$match)){
$job= $match[1];
print "job: ". $job . "\n";
}
Try:
exec('at now "php /home/site/public_html/test.php" 2>&1 | grep job | awk "{print $2}", $output);
var_dump($output);
Help from:
https://superuser.com/questions/368328/have-the-at-command-return-the-job-id-of-the-task-just-submitted

PHP Exec: Without Waiting, Without Discarding the Output, Without nohup

I need to run a command in PHP like this:
exec('dosomething > saveit.txt');
Except I don't want PHP to wait for it to be complete. I also don't want to throw away the output, and I don't want to use nohup because I'm using that for something else in the same directory.
I also tried pclose(popen('dosomething > saveit.txt','r')); and that didn't work, it still waited.
Add an ampersand to the end of the command, so:
exec('dosomething > saveit.txt &');
in the documentation of exec() there is an interesting comment that says:
Took quite some time to figure out the line I am going to post next. If you want to execute a command in the background without having the script waiting for the result, you can do the following:
<?php
passthru("/usr/bin/php /path/to/script.php ".$argv_parameter." >> /path/to/log_file.log 2>&1 &");
?>

php start php script and continue

At the moment I have a line of code like this:
system("/usr/bin/php myphpscript.php --param=".val);
Is there a way to make php not wait for the script to finish - and just move on instead?
It's a loop moving email, and the myphpscript.php is parsing the mails. And I don't wan't to wait for myphpscript.php to finish each time - just start it and move on!
UPDATE SOLUTION
Found the answer here:
http://www.php.net/manual/en/function.exec.php#101506
passthru("/usr/bin/php myphpscript.php --param=".val." >> /dev/null 2>&1 &");
Exp:
/dev/null
I needed to write to something else that STDOUT, else PHP will hang untill script finish. So I write to /dev/null instead.
2>&1
Redirecting errors to STDOUT
&
"Run in background" as mentioned in this thread.
Have a good day!
jack
jack
This should work
exec("/usr/bin/php myphpscript.php --param=".val . '&');
Throwing the background command to the end should le the script continue:
system("/usr/bin/php myphpscript.php --param=".val . "&");
I'd also add nohup just to be safe, since I think this sub-process might get killed when the parent php script finishes:
system("nohup /usr/bin/php myphpscript.php --param=".val . "&");

Categories