HI there!
I've run into some problem while learning to combine .sh files and PHP. I've create a file test.sh and in that file I call a PHP file called test.php.
If I double click on the .sh file then it runs perfectly but when I try to run it from the terminal I get "command not found". I'm in the exact folder as my .sh file but it wont work. Here's my test.sh:
#!/bin/bash
LIB=${0/%cli/}
exec php -q ${LIB}test.php one two three
exit;
When I doubleclick on the test.sh file then it returns the argv array like it suppost to. But why can't I run it from terminal?
use ./filename.sh
no matter if your are in the same folder or not, without giving ./ the system is searching the path variable. (/bin/, /usr/bin and so on)
Is execute bit enabled?
chmod +x test.sh
Your $PATH variable may not include '.' - so the shell may not be able to find the command to run.
As others have said, sh ./file.sh will work...
It is possible that your environment is different when launching from the Terminal and when launching via a double-click. Try executing which php and echo $PATH from the Terminal to see what the deal is.
EDIT 1
As others have noted, if your "command not found" is referring to the shell script and not php, then you probably forgot to include the "./" before the name of the script (i.e. ./test.sh). Also, don't forget to make it executable by invoking chmod a+x test.sh. The reason for this is that PATH does not include the current directory (i.e. "."), because doing so would be a security risk (e.g. folders with a ton of files in them including a fake "ssh" which could then intercept your password or the like).
EDIT 2
Also, I don't know about you, but ${0/%cli/} is evaluating to -bash from within my Terminal. Are you sure that's what you wanted it to expand to? Perhaps you should specify the exact filename.
Another option is to run it with sh (or bash, if sh on your machine isn't bash and the script uses bashims)
sh filename.sh
bash filename.sh
This will work whether or not the file is executable or in $PATH
Related
i have move_files.sh and installed it in crontab.
Actually job is working because it's printing those echo. And creating log file.
But it's not calling that PHP script.
Interesting thing is if i run it by manually it's calling php script and working 100%. But why it's not calling after i installed it on crontab.
should i put "php" before calling php script. I am thinking that cronjob would work same as manually running script. Please give me idea.
My code is below.
#!/usr/local/bin/bash
DIR=/data/aa/bb
LOG=~account/HOME/log/dd
DATE=`date +%Y%m%d`
LOG_FILE=$LOG/move_files.$DATE.log
PROG=~account/HOME/bin/move_files.php
for type in "1" "2" "3"
do
echo "Check files in $DIR/dat/$type" >> $LOG_FILE
$PROG $DIR/dat/$type $DIR/backup/$type >> $LOG_FILE
echo "Compress files in $type" >> $LOG_FILE
find $DIR/backup -name "*.DAT" -type f -exec gzip -f {} \; >> $LOG_FILE
done
I had the similar issue as you're doing. My backup script was working very well if it's called directly but wasn't working in crontab at all. I figured it out by adding this in the start (after shebang) of bash script:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt
Where "/opt" in the last is the directory where my bash script exists.
Try calling the script specifying php binary path.
/path/to/php_binary script_file.php
E.g:
/usr/bin/php5 myscript.php argument1 argument2 >> mylogfile.log
If you execute the script locally, you will visit to that folder (i.e. ~account/HOME/bin/)
By executing cron, it will use a new shell to execute the *.sh file in the present path. Therefore what you missing is to visit the path of where the .php/.sh file is contained. (absolute path is recommended)
cd ~account/HOME/bin/
The log file can be created because you issue an environment variable $LOG which indicates the exact location of that log file.
Thanks Guys,
anyway i put on myscript following line:
export PATH=$PATH:/usr/local/bin
Then it's working now.
Thank you all of you guys.
I'm trying to run a python script from php with
$res = '';
exec('./home/Desktop/Scripts/fetch_matches.py', $res);
My python file starts with #!/usr/bin/python and has exec rights. For now I only have a print in there but it's not working(var_dump($res) gets me an empty array). What's missing?
Also, if I'll have different methods in that script, how would I call them?
If you change your exec to include /usr/bin/python this should work as expected:
exec('/usr/bin/python ./home/Desktop/Scripts/fetch_matches.py', $res);
In these circumstances you should probably use absolute paths (check realpath as well as chdir and any argument escaping you need to do).
I'm not completely familiar with how the shebang in files is run but I know if calling from PHP it's far better just to include the interpreter in the command you run.
You need to get rid of the . before the path. If it's a normal Unix/Linux system, /home is in the root of the filesystem. . means the current directory, so unless you've recreated the /home filesystem in the directory from which you're running the PHP program, the path is incorrect. Also, unless your username is Desktop, you're missing a directory between /home/ and Desktop/ - it should be your username. The pwd command will never return a . before the present working directory.
For your second question, please refer to the docs on calling Python from the command line. You can execute arbitrary code from the command line, which could be something along the lines of
python -c "from fetch_matches import Fetcher; Fetcher()"
i am ubuntu user.. i have a command in php that exec a python file.. the python file is set to executable.. so, my php command is:-
shell_exec("try.py");
the python file is located at desktop.. but the php file is located in www folder..
/var/www/try.php
in the try.py, i have a code to rename a file on the desktop as follow:-
print "enter"
os.rename("a.txt", "b.txt")
print "exit"
so, the try.py and a.txt are in desktop..
my problem is, when i execute the php file, it shows the "enter" only but not with the "exit".. so i guess it cannot execute the os.rename maybe because of the root privilege or anything that i dont know.. i have tried some solutions to disable password for sudo but still i didnt show the "exit".
but, if i execute the try.py directly by double click it on the desktop and execute it, the command can be done and the output shows:-
enter
exit
so, anyone knows how to execute it using php?
You can directly rename files in php using the rename function.
rename($oldname, $newname);
The problem is when you are doing os.rename("a.txt", "b.txt") it is looking for a.txt in the directory from where the application is running (so it is looking for a.txt in /var/www/.
You should give both a.txt and b.txt the full path:
os.rename('/home/user/Desktop/a.txt', '/home/user/Desktop/b.txt')
You will also have to make sure that www-data (or whatever user is running Apache) can write to the Desktop directory; however from a security perspective this is a very bad idea - any script running on the server can read the contents of your desktop (and even delete the files).
finally, i got the solution..
to be able to rename the file in desktop using os.rename, we need to edit some of the configurations in sudoers..
it involved some steps in visudo and also the php script..
in visudo (using command "sudo visudo" in terminal:)-
#user privilege specification
root All=(ALL:ALL) ALL
www-data ALL=(ALL:ALL) ALL
#includedir /etc/sudoers.d
ubuntu ALL=(ALL) NOPASSWD: ALL //use your username.. mine is ubuntu
www-data ALL=(ALL) NOPASSWD: ALL //the place where i put the php file
in php file:-
<?php
$output = exec("sudo python /home/ubuntu/Desktop/[filename.py]");
?>
for python file is just the same..
os.rename("a.txt", "b.txt")
ok.. that is the solution.. now i can run the python script which it will change the name of a file in desktop from php..
Thank you for all the comments and suggestion for the solutions.. =))
You need something like:
my_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
That will equate to the directory try.py is actually in.
Then you can execute:
os.rename(os.path.join(my_dir, 'a.txt'), os.path.join(my_dir, 'b.txt'))
As mentioned above, this isn't necessarily a great idea from a security PoV though, since you have to grant write permissions to the php script.
I also usually put the my_dir code in an if sys.argv[0]: so that I can still debug from python REPL:
if sys.argv[0]:
# running as executable
my_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
else:
# imported into interpreter
my_dir = '.'
That way I can still import the module into REPL to debug it, as long as I'm in the same directory as the file in question.
I am trying to run a .sh file from php.
I tried doing it with shell_exec(). but its not working
I refered many questions related to this in stack overflow but could not solve
my php code is(web.php)
<?php
echo shell_exec('/var/www/project/xxe.sh');
echo "done";
?>
only done is printed. but it is working from terminal(php /var/www/project/web.php)
In xxe.sh I am calling a python file
python vin.py
I have also changed the file permission to 777 for both .sh n .py files
please help
If you say it works on the terminal and not on apache then apache's php.ini file may be disabling the use of shell_exec().
See http://www.php.net/manual/en/ini.core.php#ini.disable-functions
Your apache's php.ini file may look something like
disable_functions=exec,passthru,shell_exec,system,proc_open,popen
Remove shell_exec from this list and restart the web server, although this is a security risk and I don't recommend it.
shell_exec might not know what directory to look in for your executable's location directory. What solved it for me was this before the shell_exec:
putenv('PATH=/usr/local/bin');
Then the terminal can find the executable. Also check permissions on every part of the command to make sure apache user has read and execute permissions.
If it works well in shell, I think apache is chrooted. So php can't find /var/...
Or user of httpd user does not have permission to enter /var/...
If you are good at PHP. Open dir /var/... And readdir() and check dir exists and check file exists.
This question might help you. scanning /home/ with opendir()
The problem is usually that when you exec code from within php it is run as the webservers user www-data in alot of linux distros. Normaly this user does not have an enviroment set up, and because of that no PATH. By using full paths in your files you can usually overcome this.
xxe.sh
/usr/bin/python /path/to/script/vin.py
While trying to run a script triggered by github post-receive webhook.
Here is where my project directory is located(cloned git repo):
/var/www/html/my-repo
I create a script inside the above directory called webhook.php:
<?php
#webhook.php
$cmd = shell_exec("git pull 2>&1");
#for debugging
echo $cmd;
?>
Execute the following command inside /var/www/html
sudo chown www-data:www-data -R my-repo/
Test it by going to http://www.myserver.com/my-repo/webhook.php
Add the path to your script to github webhooks.
I have been stuck in this problem for several hours.
I have thought about a solution.
1. move your script to a python file "script.py" and place this file to your server root.
2. shell_exec("python script.py");
Any way, it works for me.
On my host I had to give a different path for my php file to be executed from shell_exec().
This didn't work shell_exec('/usr/bin/php backgroundtask.php');.
While this did shell_exec('/opt/php/php-5.5.0/bin/php backgroundtask.php');.
You can visit this Reference.
I had the same issue because PHP backslashes.
PHP escapes the backslashes, so the command that reaches the shell
'COPY E:path1\path2\file.prn /B \127.0.0.1\"PRINTER NAME"'
so I gave command like this
'COPY E:\\path1\\path2\\file.prn /B \\\\127.0.0.1\"PRINTER NAME"'.
You have to double-escape the backslashes: once for PHP and once for the shell.
So I'm working with a designer on a website in PHP/MySQL and there are a few scripts that he would like to have to make life easier for him. He is pretty comfortable using the command line for stuff like git, SASS, node, etc. I would like him to be able to run my scripts like he would run a program, instead of running it through PHP.
So instead of this:
php /path/to/file/create_module.php module_name
I would like to do this:
myscript create_module module_name
Is it possible to do this with PHP on an Apache server? I know I will most likely have to modify the server to interpret it properly, which is fine. I just don't even know where to begin, and couldn't find what I needed on Google.
Your best bet would to be to create an alias.
So an alias of myscript would actually point to the command: php /path/to/file/create_module.php and then any extra arguments will be passed as typed.
In command line, do the following:
cd /etc/
nano bash.bashrc
At the very bottom of the file, add this line of text:
alias "myscript=php /path/to/file/create_module.php"
BASHRC is a script that is run on user login, so the alias will be recreated every time the user logs into the system.
I am not sure what you are looking for myscript to do, but to run a php script via the command line without specifying the php binary, just add a a shebang, like
#!/bin/env php
<?php
// The above expects env is in /bin
$foo = "bar";
Or the full path if you like
#!/usr/bin/php
Another option: Shell script wrapper
create a new text file in /bin or another directory in your PATH, name it how you would like to invoke your script and give it this content
#!/bin/bash
cd /path/to/your/php/scripts/folder
php script.php $*
don't forget to
chmod a+x /path/to/bash/script
The advantage of this is that your PHP script is run in the right directory where it may expect other resources to be that it depends on.
On A Linux Solution:
/usr/bin/php /path/to/file.php
On a Windows Solution:
C:\Path\To\PHPExe C:\Path\To\phpfile.php