I am trying to run a function from a php file and run it from cli without having to type php deploy so have added a shebang so the cli knows how to run the script.
e.g.
#!/usr/bin/env php
<?php
if (PHP_SAPI !== 'cli') {
echo 'bin/deployer must be run as a CLI application' . "\n";
exit(1);
}
function deploy(){
echo "Deploying" . "\n";
}
foreach ($argv AS $arg){
function_exists($arg) AND call_user_func($arg);
}
File: deployer
This does work fine from root directory and running bin/deployer deploy works as expected. I'm just curious as to why if run from the directory bin: deployer deploy i get -bash: deployer: command not found
try this in inside the bin/ directory:
./deployer deploy
If you omit the ./ in front of your file, your shell will look for the command deployer in your path ($PATH), instead of treating it as a path to the file to execute.
The $PATH is a list of directories, where your shell will look for the command you typed.
To see what is in your path, try:
echo $PATH
Related
I have a php web application and on one of the route I try to execute node script.
Node script location: C:\Users\meusr\Workspace\www\myproject\directImport.js
I have configured my package.json, such that npm start executes above script.
php (laravel) application's executable index.php located at: C:\Users\meusr\Workspace\www\myPHPproject\public\
And on one of my route: importer/run
I have:
$commandToRun = "npm start --prefix ". env('IMPORTER_PATH'). " /dev/null 2>&1";
Where IMPORTER_PATH is configured in my env, as: IMPORTER_PATH = C:\Users\meusr\Workspace\www\myproject\
when I try to execute the command with exec
exec($commandToRun, $output);
Page keeps on loading no output, no error and no end.
The script has no issue and running the same command on command line on windows system works:
npm start --prefix C:\Users\meusr\Workspace\www\myproject\
outputs as expected.
I thought it was issue with permission first (which should have thrown error, but I still tried and ran another command)
exec('npm -v', $output);
which outputs my npm version.
Similarly I tried to use simply node directImport.js which didn't work either.
Then tried to change directory to the location where node file is located and ran the command again.
exec(cd C:\Users\meusr\Workspace\www\myproject\ && dir /dev/null 2>&1) // this works, but:
exec(cd C:\Users\meusr\Workspace\www\myproject\ && node directImport.js /dev/null 2>&1) // this didn't work
Thanks to miken32, what worked for me was:
<?php
$commandString = 'start /b c:\\programToRun.exe -attachment "c:\\temp\file1.txt"';
pclose(popen($commandString, 'r'));
?>
http://php.net/manual/en/function.popen.php#92413
I'm trying to execute a .exe file form PHP.
I've set the PATH variable and confirmed that it works by running my command from random directory.
PHP code:
$command = "myCommand >> log.txt 2>&1";
echo exec($command, $output, $return)."<br>";
I've inspected the log.txt and it contains an error:
'myCommand' is not recognized as an internal or external command,
operable program or batch file.
What could be the reason for this issue? it seems that PHP won't look into all directories specified inside of PATH. What do you recommend that I do?
Specifying full path to myCommand works, but I'd like to omit it. I am running Windows 10.
I want to run the simple embedded webserver from php via command line
php -S 0.0.0.0:8000
and let it show the content of the current directory.
As of the man page the only possible additional option to -S is -t for the document root. I know I can just create a index.php file with the following content
<html>
<body>
<?php
$directory = "./";
$files = glob($directory . "*");
foreach($files as $file) {
echo "".basename($file)."<br>";
}
?>
</body>
</html>
but I don’t want to create a file in that directory.
Isn’t it possible to run that code as a command line argument? I have tried it with the option -r and even with a virtual bash file and the -F-option as follows: -F <(… php code here …) but it seems true that the -S command only accepts -t as additional command.
Is there any trick to achieve it?
PS: I know that with python 2 or python 3 it is easily possible to show the current directory with python’s embedded webserver with
python -m SimpleHTTPServer # python 2
python -m http.server # python 3
but I want to do it with php.
Replace "./" from the assignment of variable $directory with the complete path of the directory you want to serve. Or, even better, change it to:
$directory = $_SERVER['DOCUMENT_ROOT'].'/';
to serve the path you provide as argument to -t in the command line.
Put index.php wherever you want in the file system and add its location (with complete path) to the end of the php command line.
If, for example, you save it as /home/erik/index.php, start PHP as:
php -S 0.0.0.0:8000 -t /home/erik/web /home/erik/index.php
PHP will use the script as a router. It will run it on every request and you can change it to interpret the request ($_GET[], $_SERVER[]) and generate different output, depending of the requested path and query string.
When I install a new Symfony application, either via composer or by downloading it directly, it ships with a command line console application. I can run this application with the following command
$ php app/console help
In other systems that have developer command line applications, (Drupal's drush, Magento's n98-magerun, etc.), the application is capable of figuring out where the root folder is when you're deep in the file hierarchy, and you can run the application from anywhere
$ cd some/drupal/path
$ drush //still works!
To do something similar with Symfony's app/console, you need to construct this path yourself
$ cd some/symfony/path
$ php ../../../app/console
And even this may not work if the command relies on the PHP working directory being the root directory.
Is there a standard/well-supported way to get the "run from any folder" behavior of other CLI applications with Symfony's app/console?
I use following console script for this purpose:
#!/usr/bin/env php
<?php
function fileLocator($file =null, $maxDepth = 10, $currentDir = ".")
{
if(empty($file)){
return false;
}elseif(file_exists($currentDir . "/$file")){
return $currentDir;
}elseif(--$maxDepth){
return fileLocator($file, $maxDepth, $currentDir . "/..");
}else{
return false;
}
}
$projectDir = fileLocator('app', 10, getcwd());
if($projectDir){
$projectDir = realpath($projectDir);
if(substr($projectDir, -1) != DIRECTORY_SEPARATOR){
$projectDir .= DIRECTORY_SEPARATOR ;
}
$appDir = $projectDir . 'app';
}else {
die('You are not in symfony project');
}
// if you don't want to setup permissions the proper way, just uncomment the following PHP line
// read http://symfony.com/doc/current/book/installation.html#configuration-and-setup for more information
//umask(0000);
set_time_limit(0);
require_once $appDir.'/bootstrap.php.cache';
require_once $appDir.'/AppKernel.php';
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Debug\Debug;
$input = new ArgvInput();
$env = $input->getParameterOption(array('--env', '-e'), getenv('SYMFONY_ENV') ?: 'dev');
$debug = getenv('SYMFONY_DEBUG') !== '0' && !$input->hasParameterOption(array('--no-debug', '')) && $env !== 'prod';
if ($debug) {
Debug::enable();
}
$kernel = new AppKernel($env, $debug);
$application = new Application($kernel);
$application->run($input);
For *nix users
Create console file with this code. put it in your global path of your choice(like /usr/local/bin/)
give it executable permission
sudo chmod +x /usr/local/bin/console
you are ready to go. You can run console from within any directory of a symfony2 project.
For windows users:
Assuming you have your php path in path variable so you was able to execute console like php app/console.
create a console.php file with the same code. create a console.bat file with the following script:
#ECHO OFF
php "%~dp0sf.php" %*
copy both console.php and console.bat file in your php directory. Now you are ready to go.
Enjoy!!
you need to create a symlink
sudo ln -s /full/path/to/app/console /usr/local/bin/console
sudo chmod +x /usr/local/bin/console
then you can run sudo console help
Here's a solution implemented as a Bash script, so suitable under Linux/Mac or Cygwin's bash prompt under Windows. It should work fine even if you swap regularly between projects, as it doesn't rely on environment variables.
Make a script with the following contents, make it executable (using chmod a+x) and then put it in your path:
#!/bin/sh
while [[ "`pwd`" != "/" ]]; do
app_dir=`find \`pwd\`/ -maxdepth 1 -name 'app'`
if [ -f "$app_dir/console" ]; then
php $app_dir/console $#
break
fi
cd ..
done
Then just use it anywhere within your Symfony project, and use it like you would use "php app/console". It doesn't depend on any environment variables - all it does is recursively look at parent folders from where you're located, looking for app/console, and then runs it with any and all the parameters you pass to it (that's what the $# symbol is for).
I created a Bundle for this to resolve. It is currently only a bash/fish script, which creates something like a virtual environment:
$ composer require sk/symfony-venv
// [...] wait for install
$ . vendor/bin/activate
(project) $ console
On https://github.com/skroczek/symfony-venv you can read more. Feedback is welcome. :)
I have a series of shell commands I want to put in a program and execute the program from the command line. I decided to use PHP for this so currently I am trying to get the most basic shell commands to run.
Save as build.php
<?php
shell_exec('cd ..');
echo "php executed\n";
?>
From the command-line
php build.php
Output
php executed
Php executes correctly but I'm still in the same directory. How do I get shell_exec( ... ) to successfully call a shell command?
You need to change the cwd (current working directory) in PHP... any cd command you execute via exec() and its sister functions will affect ONLY the shell that the exec() call invokes, and anything you do in the shell.
<?php
$olddir = getcwd();
chdir('/path/to/new/dir'); //change to new dir
exec('somecommand');
will execute somecommand in /path/to/new/dir. If you do
<?php
exec('cd /path/to/new/dir');
exec('somecommand');
somecommand will be executed in whatever directory you started the PHP script from - the cd you exec'd just one line ago will have ceased to exist and is essentially a null operation.
Note that if you did something like:
<?php
exec('cd /path/to/new/dir ; somecommand');
then your command WOULD be executed in that directory... but again, once that shell exits, the directory change ceases to exist.