I am trying to build an angular project from a Laravel project.
When I run an exec command from Laravel I got env: node: No such file or directory
Steps to reproduce :
create Laravel project: laravel new laravel-bug
create angular project: ng new angular-bug
In the Laravel file routes/web.php, in the / route, add:
use Symfony\Component\Process\Process;
$process = new Process('cd /path/to/angular-bug && ng build');
$process->run(function ($type, $buffer) {
if (Process::ERR === $type) {
echo 'ERR > '.$buffer;
} else {
echo 'OUT > '.$buffer;
}
});
This will output the result, which for me is env: node: No such file or directory
Any ideas ?
I've had this issue before when node wasn't in the path of the web server user. The second argument to the Process::run() method is an array of environment variables. Use it to set a path.
<?php
use Symfony\Component\Process\Process;
$env = ["PATH" => "/sbin;/bin:/usr/sbin:/usr/bin:/path/to/node/if/its/different"];
$process = new Process('cd /path/to/angular-bug && ng build');
$process->run(function ($type, $buffer) {
if (Process::ERR === $type) {
echo 'ERR > '.$buffer;
} else {
echo 'OUT > '.$buffer;
}
}, $env);
The problem is that somewhere in this build process something is just calling node and expecting it will be in the path. (I think I experienced it when running npm.) This is not a great thing to do, and the software should be aware of where it’s located due to a compile-time setting or information from the package management system, or it should attempt to locate it.
Due to integration restrictions, I'm forced to register people through a headless browser because the platform doesn't have an API.
I'm able to get this done on my symfony through selenium and PHP Unit. The callenge is that selenium has to be running all through which I don't believe is ideal.
This is my command:
xvfb-run --server-args="-screen 0, 1366x768x24" selenium-standalone start
I was hoping using Symfony process class, I could run the command as in below:
public function fillFormAndSubmit($inputs,$url,$form)
{
$process = new Process('/usr/bin/xvfb-run --server-args="-screen 0, 1366x768x24" selenium-standalone start');
//$process = new Process('echo Tecmint is a community of Linux Nerds > /tmp/xvfb-run.log 2> /tmp/xvfb.err');
$process->run();
usleep(3000000);
// executes after the command finishes
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
$this->webDriver->get($url);
$body = $this->webDriver->findElement(\WebDriverBy::cssSelector('body'))->sendKeys(array(\WebDriverKeys::CONTROL, 't'));
$form = $this->webDriver->findElement(\WebDriverBy::className($form));
foreach ($inputs as $input) {
if($input['type'] == 'select')
{
//PHPUnit_Extensions_Selenium2TestCase_Element_Select::fromElement($input['id'])->selectOptionByValue($input['value']);
//PHPUnit_Extensions_Selenium2TestCase_Element_Select::fromElement($this->byId('selectMenu'))->selectOptionByValue('t3');
//$this->select($this->byId($input['id']))->selectOptionByValue($input['value']);
$select = new \WebDriverSelect($form->findElement(\WebDriverBy::id($input['id'])));
$select->selectByValue($input['value']);
}
elseif($input['type'] == 'checkbox')
{
$form->findElement(\WebDriverBy::id($input['id']))->click();
}
else {
//echo $input['id'];
$form->findElement(\WebDriverBy::id($input['id']))->sendKeys($input['value']);
}
}
$form->submit();
echo shell_exec('pkill -f selenium-standalone');
//$this->waitForUserInput();
}
However, the shell command doesn't fire when I try to run the service on-demand. Is there a better way of doing this? If not, anyone with an idea of how to get it to work?
My intention is this.
My client.html calls a php script check.php via ajax. I want check.php to check if another script task.php is already being run. If it is, I do nothing. If it is not, I need to run it in the background.
I have an idea what I want to do, but am unsure how to do it.
Part A. I know how to call check.php via ajax.
Part B. In check.php I might need to run task.php. I think I need something like:
$PID = shell_exec("php task.php > /dev/null & echo $!");
I think the "> /dev/null &" bit tells it to run in the background, but am unsure what the "$!" does.
Part C. The $PID I need as a tag of the process. I need to write this number (or whatever) to a file in the same directory, and need to read it every call to check.php. I can't work out how to do that. Could someone give me a link of how to read/write a file with a single number in to the same directory?
Part D. Then to check if the last launched task.php is still running I am going to use the function:
function is_process_running($PID)
{
exec("ps $PID", $ProcessState);
return(count($ProcessState) >= 2);
}
I think that is all the bits I need, but as you can see I am unsure on how to do a few of them.
I would use an flock() based mechanism to make sure that task.php runs only once.
Use a code like this:
<?php
$fd = fopen('lock.file', 'w+');
// try to get an exclusive lock. LOCK_NB let the operation not blocking
// if a process instance is already running. In this case, the else
// block will being entered.
if(flock($fd, LOCK_EX | LOCK_NB )) {
// run your code
sleep(10);
// ...
flock($fd, LOCK_UN);
} else {
echo 'already running';
}
fclose($fd);
Also note that flock() is, as the PHP documentation points out, portable across all supported operating systems.
!$
gives you the pid of the last executed program in bash. Like this:
command &
pid=$!
echo pid
Note that you will have to make sure your php code runs on a system with bash support. (Not windows)
Update (after comment of opener).
flock() will work on all operating systems (As I mentioned). The problem I see in your code when working with windows is the !$ (As I mentioned ;) ..
To obtain the pid of the task.php you should use proc_open() to start task.php. I've prepared two example scripts:
task.php
$fd = fopen('lock.file', 'w+');
// try to get an exclusive lock. LOCK_NB let the operation not blocking
// if a process instance is already running. In this case, the else
// block will being entered.
if(flock($fd, LOCK_EX | LOCK_NB )) {
// your task's code comes here
sleep(10);
// ...
flock($fd, LOCK_UN);
echo 'success';
$exitcode = 0;
} else {
echo 'already running';
// return 2 to let check.php know about that
// task.php is already running
$exitcode = 2;
}
fclose($fd);
exit($exitcode);
check.php
$cmd = 'php task.php';
$descriptorspec = array(
0 => array('pipe', 'r'), // STDIN
1 => array('pipe', 'w'), // STDOUT
2 => array('pipe', 'w') // STDERR
);
$pipes = array(); // will be set by proc_open()
// start task.php
$process = proc_open($cmd, $descriptorspec, $pipes);
if(!is_resource($process)) {
die('failed to start task.php');
}
// get output (stdout and stderr)
$output = stream_get_contents($pipes[1]);
$errors = stream_get_contents($pipes[2]);
do {
// get the pid of the child process and it's exit code
$status = proc_get_status($process);
} while($status['running'] !== FALSE);
// close the process
proc_close($process);
// get pid and exitcode
$pid = $status['pid'];
$exitcode = $status['exitcode'];
// handle exit code
switch($exitcode) {
case 0:
echo 'Task.php has been executed with PID: ' . $pid
. '. The output was: ' . $output;
break;
case 1:
echo 'Task.php has been executed with errors: ' . $output;
break;
case 2:
echo 'Cannot execute task.php. Another instance is running';
break;
default:
echo 'Unknown error: ' . $stdout;
}
You asked me why my flock() solution is the best. It's just because the other answer will not reliably make sure that task.php runs once. This is because the race condition I've mentioned in the comments below that answer.
You can realize it, using lock file:
if(is_file(__DIR__.'/work.lock'))
{
die('Script already run.');
}
else
{
file_put_contents(__DIR__.'/work.lock', '');
// YOUR CODE
unlink(__DIR__.'/work.lock');
}
Too bad I didn't see this before it was accepted..
I have written a class to do just this. ( using file locking ) and PID, process ID checking, on both windows and Linux.
https://github.com/ArtisticPhoenix/MISC/blob/master/ProcLock.php
I think your are really overdoing it with all the processes and background checks. If you run a PHP script without a session, then you are already essentially running it in the background. Because it will not block any other request from the user. So make sure you don't call session_start();
Then the next step would be to run it even when the user cancels the request, which is a basic function in PHP. ignore_user_abort
Last check is to make sure it's only runs once, which can be easily done with creating a file, since PHP doesnt have an easy application scope.
Combined:
<?php
// Ignore user aborts and allow the script
// to run forever
ignore_user_abort(true);
set_time_limit(0);
$checkfile = "./runningtask.tmp";
//LOCK_EX basicaly uses flock() to prevents racecondition in regards to a regular file open.
if(file_put_contents($checkfile, "running", LOCK_EX)===false) {
exit();
}
function Cleanup() {
global $checkfile;
unlink($checkfile);
}
/*
actual code for task.php
*/
//run cleanup when your done, make sure you also call it if you exit the code anywhere else
Cleanup();
?>
In your javascript you can now call the task.php directly and cancel the request when the connection to the server has been established.
<script>
function Request(url){
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
} else{
return false;
}
httpRequest.onreadystatechange = function(){
if (httpRequest.readyState == 1) {
//task started, exit
httpRequest.abort();
}
};
httpRequest.open('GET', url, true);
httpRequest.send(null);
}
//call Request("task.php"); whenever you want.
</script>
Bonus points: You can have the actual code for task.php write occasional updates to $checkfile to have a sense of what is going on. Then you can have another ajax file read the content and show the status to the user.
Lets make the whole process from B to D simple
Step B-D:
$rslt =array(); // output from first exec
$output = array(); // output of task.php execution
//Check if any process by the name 'task.php' is running
exec("ps -auxf | grep 'task.php' | grep -v 'grep'",$rslt);
if(count($rslt)==0) // if none,
exec('php task.php',$output); // run the task,
Explanation:
ps -auxf --> gets all running processes with details
grep 'task.php' --> filter the process by 'task.php' keyword
grep -v 'grep' --> filters the grep process out
NB:
Its also advisable to put the same check in task.php file.
If task.php is executed directly via httpd (webserver), it will only be displayed as a httpd process and cannot be identified by 'ps' command
It wouldn't work under load-balanced environment. [Edited: 17Jul17]
You can get an exclusive lock on the script itself for the duration of the script running
Any other attempts to run it will end as soon as the lock() function is invoked.
//try to set a global exclusive lock on the file invoking this function and die if not successful
function lock(){
$file = isset($_SERVER['SCRIPT_FILENAME'])?
realpath($_SERVER['SCRIPT_FILENAME']):
(isset($_SERVER['PHP_SELF'])?realpath($_SERVER['PHP_SELF']):false);
if($file && file_exists($file)){
//global handle stays alive for the duration if this script running
global $$file;
if(!isset($$file)){$$file = fopen($file,'r');}
if(!flock($$file, LOCK_EX|LOCK_NB)){
echo 'This script is already running.'."\n";
die;
}
}
}
Test
Run this in one shell and try to run it in another while it is waiting for input.
lock();
//this will pause execution until an you press enter
echo '...continue? [enter]';
$handle = fopen("php://stdin","r");
$line = fgets($handle);
fclose($handle);
Symfony2 enables developers to create their own command-line commands. They can be executed from command line, but also from the controller. According to official Symfony2 documentation, it can be done like that:
protected function execute(InputInterface $input, OutputInterface $output)
{
$command = $this->getApplication()->find('demo:greet');
$arguments = array(
...
);
$input = new ArrayInput($arguments);
$returnCode = $command->run($input, $output);
}
But in this situation we wait for the command to finish it's execution and return the return code.
How can I, from controller, execute command forking it to background without waiting for it to finish execution?
In other words what would be equivalent of
$ nohup php app/console demo:greet &
From the documentation is better use start() instead run() if you want to create a background process. The process_max_time could kill your process if you create it with run()
"Instead of using run() to execute a process, you can start() it: run() is blocking and waits for the process to finish, start() creates a background process."
According to the documentation I don't think there is such an option: http://api.symfony.com/2.1/Symfony/Component/Console/Application.html
But regarding what you are trying to achieve, I think you should use the process component instead:
use Symfony\Component\Process\Process;
$process = new Process('ls -lsa');
$process->run(function ($type, $buffer) {
if ('err' === $type) {
echo 'ERR > '.$buffer;
} else {
echo 'OUT > '.$buffer;
}
});
And as mentioned in the documentation "if you want to be able to get some feedback in real-time, just pass an anonymous function to the run() method".
http://symfony.com/doc/master/components/process.html
I am trying to use Behat for BDD testing. When running a build on Jenkins, I would like Behat to open PHP's build in web server and then close it after running the tests. How to do that?
Basically I need to run:
php -S localhost:8000
In my BDD tests I tried:
/**
* #Given /^I call "([^"]*)" with email and password$/
*/
public function iCallWithPostData($uri)
{
echo exec('php -S localhost:8000');
$client = new Guzzle\Service\Client();
$request = $client->post('http://localhost:8000' . $uri, array(), '{"email":"a","password":"a"}')->send();
$this->response = $request->getBody(true);
}
But then when running Behat it gets stuck without any message.
Just start the server as a part of your build process. Create an ant tasks which would start the server before behat is run and would kill it once behat is finished.
I've been successfully using this approach to start and stop the selenium server.
Solved this myself. I have create two methods. I call the first one before running my BDD tests and the second one after I ran the tests:
private function _startDevelopmentServer($pidfile)
{
$cmd = 'cd ../../public && php -S 127.0.0.1:8027 index.php';
$outputfile = '/dev/null';
shell_exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $outputfile, $pidfile));
sleep(1);
}
private function _killDevelopmentServer($pidfile)
{
if (file_exists($pidfile)) {
$pids = file($pidfile);
foreach ($pids as $pid) {
shell_exec('kill -9 ' . $pid);
}
unlink($pidfile);
}
}