What SUID\SGUID I need to add for sh script for root executable of it?
I want to see working script with root rights from php exec function.
exec('cd /usr/share/htvcenter/local-server/bin/ && ./panelhost-local-server-nfsip >> log.txt');
What I must to add to config file as well for see another file like config?
I want to see /etc/exports_add like continue of config /etc/exports is it possible?
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 need to run a php script from the command line. The problem is, this script will be in a different location on different PCs.
So I want to be able to enter something like this:
php myscript.php
where myscript.php will be in a folder which is in the windows path, but not in the current folder.
Is it possible to do something like this?
Please refer to this link in the php manual.
http://php.net/manual/en/install.windows.commandline.php
On any pc you have to setup the PATH and PATHEXT variable.
Append the location of the PHP executable (php.exe, php-win.exe or
php-cli.exe depending upon your PHP version and display preferences)
to the PATH environment variable. Read more about how to add your
PHP directory to PATH in the corresponding FAQ entry.
Append the .PHP extension to the PATHEXT environment variable. This
can be done at the same time as amending the PATH environment
variable. Follow the same steps as described in the FAQ but amend
the PATHEXT environment variable rather than the PATH environment
variable.
Associate the .PHP extension with a file type. This is done by
running the following command: assoc .php=phpfile
Associate the phpfile file type with the appropriate PHP executable.
This is done by running the following command: ftype phpfile="C:\PHP5\php.exe" -f "%1" -- %~2
This will allow your script to run as normal shell command, like a batch file.
You will run it as :
myscript.php
and not
php myscript.php
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.
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