How to run external php script using Php shell_exec? - php

I have the following code on my index.php file. But it is not working properly.. When i directly visit domain.com/script.php it works. I need this script to be executed in the background while accessing index page. Can anyone help me?
shell_exec('php script.php > /dev/null 2>/dev/null &');

check php is running in safe mode or not shell_exec is disabled in safe mode for the sake of security why don't you use
curl
to run the code

Well I think there are 2 possible issues in your case
1) try this:
shell_exec("script.php 2>/dev/null >/dev/null &");
OR
shell_exec("script.php 2>&1 | tee -a /tmp/mylog 2>/dev/null >/dev/null &");
2) A simple way to handle the problem of capturing stderr output when using shell-exec under windows is to call ob_start() before the command and ob_end_clean() afterwards
ob_start();
ob_end_clean();

Well instead of using shell_exec, you can make an ajax call to script.php when the user visits index.php.
Another option is to run the script.php as a cron job every 5 minutes or so. When the user visits index.php, some data can be saved to database indicating that script.php should run. script.php should check if it is marked for running.

Related

Run a function/script in PHP and without affect the script flow

I need to make a php script that execute another function (or another script) in background: When i call it, the flow of the script must continue even if the second script is not finished.
I found http://php.net/manual/en/function.sleep.php (sleep function), but is not exactly what i need.
Some help?
Edit:
What i want to acomplish?
I need to make a change in my database. Then run a php script, but i need to make another change in my database 1 second (or whatever lapse) after i ran the php script.
Since you apparently don't care about a response from the script you are calling.
And if your server allow...
You could call your script as if it was from a shell and say that it should discard output with "> /dev/null 2>/dev/null &"...
shell_exec('php yourotherscript.php > /dev/null 2>/dev/null &');

get PID of PHP file executed with shell_exec with continuous output

I do have a PHP script which has a continuous ouptput (written to tmp file for logging). So far, I started the script right from the shell, but now, I would like to start it via shell_exec:
shell_exec("file.php >> /path/to/log 2>&1 &");
But shell_exec always return null instead of the PID.
How can I get the PHP process ID and keep the output written to the file?
Thanks in advance.
Look into proc_open and proc_get_status instead.

How to email output from shell_exec()?

With cron jobs, the output of the file being executed is emailed to me. I recently discovered via this answer that it's possible to asynchronously execute a PHP file using the shell_exec() function. Per the above answer, I've been using the following command:
shell_exec('php /file/path.php parameter1 parameter2 > /dev/null 2>/dev/null &');
I think what is of most interest with regards to this question is the stuff at the end:
> /dev/null 2>/dev/null &
Is there any way to change that so that the output is emailed, like with a cron job?
The line
> /dev/null 2>/dev/null &
Essentially sends the output to a null file. If you take that bit off, the output will be sent to the standard out, which should in turn should email you the results assuming you run it with a cron job.
So,
shell_exec('php /file/path.php parameter1 parameter2');
If you're not running it with a cron job, you'll need to build in the email functionality to the script itself.
First off, shell_exec() is not asynchronous. Execution of the PHP code will be suspended until the shell_exec() call has terminated.
This: > /dev/null tells the shell to redirect stdout from the process being executed to /dev/null, which means it disappears. This: 2> /dev/null does the same, but for stderr in stead of stdout.
If you remove these parts of the shell_exec() call, the call will return whatever is written to stdout:
$result = shell_exec('php /file/path.php parameter1 parameter2');
mail('me#email.com', 'Shell output', $result);
There are also other alternatives to shell_exec() that may suit your needs better. For example popen()andproc_open()` allow more fine grained control over input and output.
Also, since you are executing a PHP script, you may be able to simply use include() or require(), depending on how the script is written. Another option would be to read the file and then execute the PHP code using eval().
Use :
| mail -s "Result of cron job" myemail#company.com
At the end of your command. Also, you can pipe stderr and stdout into stdout 2> &1 | mail....
That should do the trick. Maybe because you use shell_exec("... &"), you'll have to wrap the whole php /file/... | mail -s "result" myemail#company.com in a subshell. like:
shell_exec('(php /file/path.php parameter1 parameter2 2> &1 | mail -s "Result of cron job" myemail#company.com) &')
This is a rather ugly way to get the output to send a message and I'd advise in favor of refactoring the outer call of shell_exec to read stderr and stdout, craft a message and send that to yourself. Meanwhile the | mailsolution should do the trick.
Happy mailing !

Is it time consuming to print log in exec command?

In PHP,
I am running exec command in php script so that It will run in background. But I'm wondering about the performance hit when logging to log_file?
exec("/usr/bin/php /path/to/Notification.php >> /path/to/log_file.log 2>&1 &");
If so then how can I not print output to log_file? I know /dev/null. But I don't know correct syntax or structure. Please Can anyone append me this on above line.
Any help will be appreciated.
since you are appending to the log file through the bash output redirection, it will be very good in terms of performance.
Everything depends ofcourse on how much output that is being logged and how big your log file is, if you aren't rotating it properly.
If this is too excessive, you can disable the output redirection, simply by removing everything after the >> output redirection directive:
exec("/usr/bin/php /path/to/Notification.php &");
this however could still output to the system's stdout or stderr, to skip this redirect all output to /dev/null as you mentioned:
exec("/usr/bin/php /path/to/Notification.php &> /dev/null &");
here is a good reference about output redirection in bash
please also note that $> is bash only.

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 &");
?>

Categories