Running shell commands in .php file from command-line - php

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.

Related

Executing node script from php web application (Windows)

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

Run PHP script with command line globally on win10

How to run a PHP script with command line globally, like:
update var1=abc var2 var3=def
"Update" located in fact: "C:\Localhost\Scripts\Update.php"
Many sources suggest this as:
php update.php var1=abc var2 var3=def
Shortly: How to run a PHP script as if it was a registered executable?
Disclaimer: I've written this answer for Linux/Ubuntu, I'm not sure if it'll work for Windows (the OP updated the question's requirements).
So, this is possible, the way I've described here basically just has a global bash script call a PHP file. Here's the steps:
Make a new bash script:
#!/usr/bin/env bash
php /path/to/yourphpfile.php
Change the file executable: chmod +x /path/to/yourbashscript
Copy the bash script into /usr/local/bin/: sudo cp /path/to/yourbashscript /usr/local/bin/
Open a new terminal, then run yourbashscript (or whatever you named it).
And voila! A globally available PHP script!
I finally solved it:
-Create a .bat file where your php file in, it contains:
#ECHO OFF
php %~dp0/update.php %*
-And save it as "update.bat"
-Add the directory name into "Path" in "Environment Variables"
Now you can write "update var1=abc var2 var3=def" freely in command prompt!

Rscript does not work when invoked in PHP but does from Command Prompt

I am trying to execute an R script from my PHP page using the exec function. I have set the environment variables in Windows and Rscript works fine on the command prompt. However on the PHP page it says, " 'Rscript' is not recognized as an internal or external command, operable program or batch file."
Any help would be greatly appreciated.
I would define a launcher.bat where I deal will all R-paths problem:
PATH PATH_TO_R/R-version/bin;%path%
cd PATH_TO_R_SCRIPT
Rscript myscript.R arg1 arg2
Then in the php side you can use exec:
<?php
exec('c:\WINDOWS\system32\cmd.exe /c START PATH_TO_LAUNCHER\LAUNCHER.bat');
?>

php command line exec() multiple execution and directories?

I am trying to Execute a multiple commands in php using exec() and shell_exec but i am getting a null value back which i shouldn't and nothing is happening (if i copy and paste the strings below in the command line it will work fine and accomplish the job needed) this is the commands i am using:
$command = "cd /../Desktop/FolderName;";
$command .= 'export PATH=$PATH:`pwd`;';
$command .= 'Here i execute a compiler;';
and then i use the escapeshellcmd()
$escaped_command = escapeshellcmd($command);
then
shell_exec($escaped_command);
any ideas what i am doing wrong and i also tried escapeshellarg() instead of escapeshellcmd()?
Solution: the Problem was the permission of the execution compiler for other owners is non and this was the problem.
because when you are using exec() function in php the owner of the file will be www-data so you need to give permission for the www-data either from the ACL of ubuntu or whatever linux based operating system(you can know the owner by doing this exec('whoami')), or by the files you need to execute.
(Sorry my bad English)
On Linux you can add your Commands in a Shell Script.
You can put this in any file:
#!/bin/bash
cd /../Desktop/FolderName
export PATH=$PATH:`pwd`
EXECUTE COMPILER
And save this as fille.sh
Then, add execution permissions:
chmod +x path/to/file.sh
From PHP, you can call this Script executing:
shell_exec('sh path/to/file.sh');
Hope this helps!

PHP shell_exec() - having problems running command

Im new to php shell commands so please bear with me. I am trying to run the shell_exec() command on my server. I am trying the below php code:
$output = shell_exec('tesseract picture.tif text_file -l eng');
echo "done";
I have the picture.tif in the same directory as the php file. In my shell I can run this without a problem.
It takes a while to run the code, then it doesnt make the text_file like it does when I run it in command prompt.
Per your comment:
Should I write a loop in shell
instead?
You can write a very simple shell script to run the command in a loop. Create the script file first:
touch myscript.sh
Make the script executable:
chmod 700 myscript.sh
Then open it with a text editor such as vim and add this:
for (( i = 0 ; i <= 5; i++ ))
do
tesseract picture.tif text_file -l eng
done
Thats the very basics of it (not sure what else you need), but that syntax should help get you started. To run the script, do this if you're in the same directory as the script:
./myscript.sh
Or specify the full path to run it from anywhere:
/path/to/mydir/myscript.sh
Could this be a permissions issue? My guess is that PHP isn't running with the same permissions that you do when you execute the command directly from the command prompt. What OS are you running on?

Categories