It might be kind of a nooby question but ...
How can I possibly run a php command like php /path/to/script.php on a hosted web-server?
I have no access via SSH or something.
You can check if the program execution commands like exec() have been disabled on the server side using:
function execEnabled()
{
$arrDisabled = explode(',', ini_get('disable_functions'));
return (!in_array('exec', $arrDisabled));
}
A list of all system execution commands can be found here:
http://us.php.net/exec
If exec(), passthru() etc. have been disabled, there is no way to execute shell commands.
As Christian Ciupponi asked, why would you execute a PHP script using shell command? You could simply include the file to run it if file access to the script is permitted.
Have a look at this function, maybe it will help: http://it1.php.net/function.exec
By the way why can't you just visit the page you need? In this way the script will run
EDIT:
How to use the exec function:
The exec function allow you tu execute an external program when you do not have access to an ssh consolle. (note that this function can be disabled by the sysadmin )
For example you can use:
$whoami = exec('whoami');
Related
I need to make use of the PHP function shell_exec(). Unfortunately I'm using shared hosting and shell_exec() has been disabled for security reasons.
With the cron job php -q /home/user_name/exec_test.php >/dev/null 2>&1 I found out, shell_exec() is only disabled in /home/user_name/public_html/ but not in /home/user_name/, which I can access via cron job.
Of course, I cannot access the directory /home/user_name/ via browser. (I would prefer not to use cron job.)
My idea was to include (include "/home/user_name/exec_test.php";) my exec file from a php script in the browser accessible direction. Unfortunately this gives me the same error: shell_exec() has been disabled for security reasons
Is there a way I can reach what I want? Is there a way to execute /home/user_name/exec_test.php without include it in /home/user_name/public_html/ directory?
PHP's exec() function is turned off in my hosting server and it's not possible to change it (for security reasons). Are there alternatives for the exec() function?
Here is more info about my problem:
What I want to do: I want to use .jar file in host server.
This is how it looks on my localhost:
function generateReqXML() {
exec('"C:\Program Files\Java\jdk1.7.0_21\bin\java.exe"
-jar vaiisis.jar auth', $out);
}
This .jar file help my system to generate XML code.
This code work perfect for my local machine, but its facing the problem when I am trying to use it in my host server.
Alternatives to exec are:
system(); passthru(); shell_exec();
You can check if these are available with echo ini_get("disable_functions");
But since these are probably disabled there are only non-PHP options left.
Do you have to use the exec() function via PHP?
You could for example write a shell script, which can be invoked via cron to do your job.
What are you exactly trying to accomplish?
Is there any reason why I can not complied files in PHP's shell_exec/exec/system function?
Example of something that does work in command line and PHP's shell_exec function:
<?php
$data = shell_exec("ls");
echo $data;
?>
Example of something that does not work in PHP's shell_exec function but will work in command line (I can confirm that):
<?php
$data = shell_exec("./c-compiled-file argv1 argv2 argv3");
echo $data;
?>
Is there anything I can do on my server so this will work? I've looked everywhere and no solutions I found fixed the problem. The compiled file is in the same directory as the PHP script as well, it just won't execute it. Also if you're asking, yes I have tried this with SSH2 and it still will not execute.
Also PHP is not in safe mode and NO functions are disabled.
Some common glitches when executing external commands from PHP that work fine from shell:
Command uses relative paths but PHP is launched from an arbitrary location:
Use getcwd() / chdir() to get/set working directory
PHP and shell run with different user credentials. This is often the case when PHP runs through a web server.
PHP and shell run different commands. Many people call stuff like exec("foo $bar") and doesn't even check what "foo $bar" contains.
No error checking is done. The bare minimum is to capture and print standard output, standard error, status code and, of course, all PHP error messages including warnings and notices.
You can redirect stderr to sdtout
You can use a PHP function that allows to capture more information, such as exec()
The web server is disallowed to execute the command at operating system level.
Lookout for SELinux or similar tools.
Just a guess, but the binary you're trying to execute might not have the proper permissions. Prepeding it with ./ in the command line forces it to execute, but PHP probably strips that for security purposes. Try this:
chmod +x c-compiled-file
You want to use system in the second case, and not shell_exec.
system executes an external program and displays the output.
shell_exec executes a command via shell and returns the complete output as a string.
and for good measure:
exec simply executes an external program.
Furthermore you want to make sure your external program is executable and (though you have stated it, I'll restate this) has execute permissions for the user which is running the web server. You also want to make sure the directory your external program is running in has the ability to write to its directory or /tmp or whatever output directory you have set.
Finally you should always use absolute paths for executing things like this in cron or php or whatever... so don't use ./c-compiled-file argv1 argv2 argv3, but instead use /home/username/c-compiled-file argv1 argv2 argv3 or whatever the full path is.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php shell_exec() vs exec()
How do I run a linux command from a PHP script? I'm running Linux Debian and PHP5. I want to be able to issue a wget command to the console.
An example of what I'm looking for is something like this:
phpFunction ("wget http://www.example.com/image.jpg /folder");
echo "done";
Also would I be able to echo the output of that function?
Use exec to run any command. Be careful not to exec any user input though, as it can severely compromise your server.
Also, note that most shared servers block off the exec function so you won't be able to use it.
Finally, as a shorthand, you can wrap the command you want to exec in backticks.
You can do what you want with the following code :
system(command);
See http://php.net/system
You can execute linux commands within a php script - all you have to do is put the command line in brackits (`).
And also concentrate on exec() , this and shell_exec() ..
you can use
exec
shell_exec
http://php.net/manual/en/function.shell-exec.php
Occasionally my media server goes down and I'm wondering if it's possible to start it remotely using php to check the port and if it's not running invoke cron (or some other way) to run a shell command. Is this possible because this is not a strong area for me. Here's the process I use with PuTTy.
login to shell
cd to source/red5/dist
screen
./red5.sh
CTRL-A then D to detach
logout
Simplest thing is to write a shell script. And then login to remote console via PHP.
shell_exec: execute a shell command and returns the output as string.
exec: just executes an external program
A simple way to achieve what you want is to run this in screen:
while /bin/true ; do ./red5.sh ; done
If you can write a shell script that does what you need, then PHP's has exec(), system() and passthru() for you.
PHP actually has a special operator for executing shell commands, the backtick:
`cd source/red5/dist`
will go to the specified directory. (But I don't know much about shell, so I can't implement you the whole thing.)
If you need much control over the execution (I don't know whether you need here) use proc_open.
you can use corn job on php and put all command on .sh file and run like this
59 11 * * 1,2,3,4,5 root command file.sh?token
something like this ,it will be save
There is more than one good answer here, but you should opt for executing the init script for red5 instead of the .sh or .bat. There are pre-made init scripts here: http://code.google.com/p/bigbluebutton/downloads/detail?name=red5&can=2&q= and here: http://www.videowhisper.com/forum.php?ftid=48&t=init-file-red5-linux-installations-red5-linux-init.d-chkconfig