php exec script from different folder - php

I want to run ~/bin/git_cache_meta.sh in a folder ~/www/project/folderA/ with a php script.
My script is located in the www folder and looks like this:
#!/usr/bin/php
<?php
chdir('project/folderA/');
exec('git_cache_meta.sh --apply');
The script shows the error sh: 1: git_cache_meta.sh: not found how can I tell exec that the script git_cache_meta.sh is in the folder ~/bin/git_cache_meta.sh ? I do not want to copy the file git_cache_meta.sh to ~/www/project/folderA/.

You should use full path of the file and be sure it has the necessary access.
i.e.
in your case:
~/bin/git_cache_meta.sh
Also, use chmod +x to give it the permission to be executable.

Related

Run command in other folder

I have a php script that should run a command located in a certain subfolder, in another subfolder.
The command is in folder Cmd, and should be executed in folder 1.6.2. So first I switch to the 1.6.2 directory, and then I use a relative path to call the command:
exec("cd 1.6.2");
exec("..\Cmd\sencha app build production");
But this throws the error that the directory couldn't be found, because the second exec still executes in the main folder, where the calling index.php file resides.
The php manual on exec doesn't provide any possibility to execute in another directory. Am I missing something here?
The current system is a Windows, but I have to make it portable because it may be executed on linux in the future.
Try changing the working directory first
e.g.
chdir('/path/to/1.6.2');
exec("sencha app build production");
You have to change the directory within the same exec() command.
like so: exec("cd 1.6.2 && Cmd/sencha app build production"); depending on your actual folder structure. I'm just guessing)

Running a php file in a batch file

I'm trying to run a php file within a batch file. I have the folder for php.exe within the same directory as the bat.
set addLocation="%cd%\test.php"
set exeLocation="%cd%\php\php.exe"
start "run" "%cd%\php\php.exe" "%location%"
pause
Any help would be appreciated
I use this method myself
Batch file called xxxx.cmd
PATH=%PATH%;C:\path\to\folder\with\phpexe
CD \path\to\php_source
php file.php
The php folder is added to the PATH only for the duration of this command windows existance
You can follow the following code:
To change the directory -
set path = D:\Workspace
CD /D %path%
To run the php file -
php file.php
This code will work well.
Thank you.

How to execute a PHP script from the command line by just typing the file name?

I want to be able to type into the command line, and have the following PHP script execute like so:
$ hello.php
Question is, where do I save my hello.php file to run it like this. I see a lot of tutorials showing this way, but they do not mention where to save the actual file. If I save it on my desktop then it will run when I type in:
~/Desktop/hello.php
I want to be able to just type in the file name regardless of what directory I'm in and have it execute.
Do I have to setup an alias? Or can I just put it in a specific folder.
hello.php
<?php
echo 'Hello! This is a test';
All you have to do is add a shebang line:
#!/usr/bin/php
<?php
echo 'Hello! This is a test';
And then:
chmod +x hello.php
Then you can run it from the same directory by just typing:
./hello.php
If you want to be able to run it from anywhere, put it somewhere in $PATH, such as /usr/local/bin. You can either actually put the file there, or change your PATH, or create a symbolic link. Then, you can run it as:
hello.php
add a shebang to your executable file.
chmod +x file_name.php to make it runnable.
just like this:
#!/usr/bin/php
<?php
var_dump($argv);
?>
this one is a bit better (see this question):
#!/usr/bin/env php
<?php
var_dump($argv);
?>

file_put_contents not working of used from command line?

Here is the contents of a PHP file:
<?
file_put_contents('test.txt', 'stuff');
echo 'complete';
?>
I call this test.php, it is located in /mnt. It has 777 permissions. Now I go into SSH, and execute: sudo /usr/bin/php /mnt/test.php. I receive the complete message at the bottom of my script. Now, I cd into /mnt. Text.txt is not there. What gives?
proper code is this:
file_put_contents(__DIR__.'/test.txt', 'stuff');
echo 'complete';
otherwise, php writes file in "current working directory", not where it is put
You haven't specified a path, so you are putting stuff into text.txt in the current working directory. That will be the directory you cd to (or your default directory for a new shell) before running sudo, not the directory the PHP script is in.
Change your code to include the absolute path, like so:
file_put_contents('/mnt/test.txt', 'stuff');
Otherise, PHP will just write the file in the working directory (which is usually /home/user)

Correct path to run python script with php from cgi-bin folder

I want to run python script 'test.py' from my cgi-bin directory on my webserver. the cgi-bin directory is at 'www/cgi-bin/'. The python scrip is in that directory. The php code I'm executing is at 'www/html/website/index.php'.
what is the correct path that goes here ------> exec('path');
TYVM
edit:
My python script has been chmod +x'd and is executable (I have tested)
Assuming the PHP script is in the same directory as the "www" directory.
<?
exec('./www/cgi-bin/test.py');
?>
or without chmoding, you can run it through Python
<?
exec('python ./www/cgi-bin/test.py');
?>
Your PHP code is executing in the www/html/website/ directory then. You need to go up two directories (arriving in the www/ directory), then go down to the cgi-bin/ subdirectory. So this should work:
exec('../../cgi-bin/test.py');
Note that this is relying on the current work directory being the PHP script directory. This might not always be the case, particularly if something in the PHP script changes the current work directory explicitly or simply if this PHP script is included from a different script. So it is better to use absolute paths (e.g. put the absolute path of the base directory into a config file).

Categories