PHP script won't run in the background - php

I am trying to run a php CLI script in the background and it just won't run - it has a status of Stopped SIGTOU (Trying to write output) - Here are the details
Mac OS X Lion 10.7.2
PHP 5.3.6 with Suhosin-Patch (cli) (built: Sep 8 2011 19:34:00)
I created a basic script test.php
<?php echo 'Hello world'.PHP_EOL; ?>
Here are the results of various tests:-
php -f test.php (Hello world gets displayed)
php -f test.php >test.log 2>&1 (Hello world gets put into test.log)
php -f test.php >test.log 2>&1 & --- I get [1]+ Stopped(SIGTTOU) php -f test.php > test.log 2>&1 -- and the job just sits there doing nothing nothing gets logged however lsof shows the log file is open
It is something to do with PHP? A similar shell script gets executed no problems in the background.

If readline is enabled in your build of php, simply pass /dev/null as the input.
In your example above, it would be:
php -f test.php </dev/null >test.log 2>&1

This is resolved now -- thanks to all who responded. The problem was that Apple provide PHP pre-built with the OS - the CLI version was built with readline included - http://www.php.net/manual/en/intro.readline.php ... this prevents any background running of scripts because readline automatically starts IO with the TTY ...
My problem was that I couldn't build my own version of PHP because of this -> http://forums.macrumors.com/showthread.php?t=1284479 - once I got that resolved my background script issue was gone :)

Well, a PHP script stops when its done execution, ie a simple echo "Hello World" is done execution as soon as it outputted the string, i would guess it have something to do with it ;-)

Related

Execute matlab function from IIS 7 + php

I have MATLAB r2013b, IIS 7.5, php 5.6 on Windows Server 2008 and trying to do the following:
<?php
...
chdir($matlabScriptsDir);
exec("matlab -r test_func(args) -logfile $logfile", $output);
$output shows that process exited with code 0, in the TaskManager I see MATLAB.exe running from the user IUSR, $logfile is created and locked by this process, but the process does nothing – just hangs (there are no issues with running the same command from cmd.exe).
Where is the problem?
using the -r option is like entering commands to Matlab's command line, so after the command is executed you still have Matlab running, waiting for your next command. Try instead:
matlab -r "test_func(args), exit"
So that the Matlab process will end. I suggest you do even more, and wrap it with a try-catch (followed by exit) so that in case of an error the process will not hang.

PHP to exec casperjs/phantomjs script

I'm having trouble using PHP to execute a casperjs script:
<?php
putenv("PHANTOMJS_EXECUTABLE=/usr/local/bin/phantomjs");
var_dump(exec("echo \$PATH"));
exec("/usr/local/bin/casperjs hello.js website.com 2>&1",$output);
var_dump($output);
Which results in the following output:
string(43) "/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:."
array(1) {
[0]=>
string(36) "env: node: No such file or directory"
}
The only stackoverflow posts I could find hinted that there's a problem with my paths, and that maybe the PHP user can't access what it needs.
I have also tried the following: sudo ln -s /usr/bin/nodejs /usr/bin/node
Does anyone know what I would need to do or change for this error to resolve?
Thanks
My guess is you have something, somewhere, that assumes node is installed.
First, are you running php from the commandline? I.e. as php test.php in a bash shell. If so, you can run the commands, below, as they are. If through a web server the environment can be different. I'd start with making a phpinfo(); script, and then run the troubleshooting commands through shell_exec() commands. But, as that is a pain, I'd get it working from the commandline first, and only mess around with this if the behaviour is different when run through a web server. (BTW, if you are running from a cron job, again, the environment can be slightly different. But only worry about this if it works from commandline but does not work from cron.)
Troubleshoot hello.js
The easy one. Make sure your script does not refer to node anywhere. Also remember you cannot use node modules. So look for require() commands that should not be there.
Troubleshoot your bash shell
Run printenv | grep -i node to see if anything is there. But when PHP runs a shell command, some other files get run too. So check what is in /etc/profile and ~/.bash_profile . Also check /etc/profile.d/, /etc/bashrc and ~/.bashrc. You're basically looking for anything that mentions node.
Troubleshoot phantomjs/casperjs
How did you install phantomjs and casperjs? Are the actual binaries under /usr/local/bin, or symlinks, or are they bash scripts to the . E.g. on my machine:
cd /usr/local/bin
ls -l casperjs phantomjs
gives:
lrwxrwxrwx 1 darren darren 36 Apr 29 2014 casperjs -> /usr/local/src/casperjs/bin/casperjs
lrwxrwxrwx 1 darren darren 57 Apr 29 2014 phantomjs -> /usr/local/src/phantomjs-1.9.7-linux-x86_64/bin/phantomjs
And then to check each file:
head /usr/local/src/casperjs/bin/casperjs
head /usr/local/src/phantomjs-1.9.7-linux-x86_64/bin/phantomjs
The first tells me casper is actually a python script #!/usr/bin/env python, while the second fills the screen with junk, telling me it is a binary executable.

How to run a php file forever in ubuntu by command line

I am using web-socket in php to run a chat service problem is when i close the command line the connection also close , is there a solution so that the chat service can run forever .
i am using this command
php -q server.php
create a php file on your server name it as start_server.php
and post this content
<?php
$shell_command = "php -q server.php > /dev/null &";
shell_exec($shell_command);
?>
now execute this command
php start_server.php
I am using screen for running php script and so far I haven't really faced any issues.
screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells). Each virtual terminal provides the functions of a DEC VT100 terminal and, in addition, several control functions from the ISO 6429 (ECMA 48, ANSI X3.64) and ISO 2022 standards (e.g. insert/delete line and support for multiple character sets). There is a scrollback history buffer for each virtual terminal and a copy-and-paste mechanism that allows moving text regions between windows.
More about screen here.
One drawback with using screen is that if you make a change in your script you will have to terminate the virtual terminal and create a new one for the changes to take effect.
Here is a sample code:
$ screen -L php /path/to/php/script
This will create a new virtual terminal.
Some useful commands:
To detach screen (once you are in the virtual terminal): ctrl + a + d
To resume screen (from your main terminal once detached): $ screen -r
If there are multiple screens running on your machine the above command should be followed by the pid.tty.host string which is unique for each screen
To terminate screen: ctrl + c
To list pid.tty.host strings of each terminal: $ screen -ls

Is it possible to redirect Boris output (PHP interactive command line)?

I am using Boris—"A tiny little, but robust REPL for PHP". To be more specific, I am using WP-CLI's implementation of Boris (wp shell—it replaces the $boris command prompt with wp>).
I was wondering if it was possible to pipe the command line output to say, a text file. For example, I want to capture my PHP info to a text file. Here is what happens when I execute phpinfo();
wp> phpinfo();
phpinfo()
PHP Version => 5.3.14
System => Darwin Macintosh-HD.local 12.4.0 Darwin Kernel Version 12.4.0: Wed May 1 17:57:12 PDT 2013; root:xnu-2050.24.15~1/RELEASE_X86_64 x86_64
Build Date => Jul 4 2012 17:23:04
Configure Command => './configure' '--with-mysql=/...
//phpinfo() output continues here
I want to redirect this output from the standard display to a text file. I know this is bash syntax, but this is what I want to achieve in theory :
wp> phpinfo(); > phpinfo.txt
// phpinfo.txt now contains phpinfo() output
Is there any way to make this work?
You should be able to use the bash syntax on the command that opens the shell, try it there.
EX:
wp_shell > output.txt
Where "wp_shell" is the command that opens your prompt. You might be able to pass phpinfo directly to the prompt as well if you want to not have to open it, if there is a way to pass direct script to it as with the default php CLI.

Executing a powershell script with Exchange Snap-In via PHP

I'm attempting to query an Exchange server using a powershell script executed by PHP and it doesn't seem to be working. The command I'm attempting to run is:
powershell "Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010";"Get-CASMailbox -Identity user#example.com | fl ActiveSyncEnabled"
If I type that directly into the command prompt, it executes correctly and returns:
ActiveSyncEnabled : True
When I try it in PHP:
$output=shell_exec('powershell "Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010";"Get-CASMailbox -Identity user#example.org | fl ActiveSyncEnabled"');
I get nothing. $output is set but blank. If I add "> output.txt" to the end, I get a blank text file. I'm able to run other, simpler powershell commands successfully via PHP, but not this one. The Apache service is running as the same user as when I successfully executed the script via the command prompt.
Any ideas?
I'm running:
Windows Server 2008 R2 SP1 64-bit
Apache 2.2.22 32-bit
PHP 5.2.17 VC6 32-bit
Exchange Management Console 2010
You may try adding 2>&1 at the end of the command, e.g.:
$output=shell_exec('powershell "Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010";"Get-CASMailbox -Identity user#example.org | fl ActiveSyncEnabled" 2>&1');
Sometimes, a line break at the beginning of the powershell output seems to prevent shell_exec printing anything else.

Categories