Here is my php code
$command = "C:\Program Files\ClustalW2>clustalw2 -INFILE=seq.txt -TYPE=Protein -OUTFILE=res.aln";
exec($command);
When i run the command using the cmd, it generates the desired file. However when i try passing the same command via my php code it generates no result. How do i fix this problem?
Probably it's because of the > symbol before executable's filename? Also, try with single quotes:
$command = 'C:\Program Files\ClustalW2\clustalw2 -INFILE=seq.txt -TYPE=Protein -OUTFILE=res.aln';
exec($command, $output, $retval);
var_dump($output);
var_dump($retval);
Related
I have a a shell script (myscript.sh), which contains many bash commands. I want to run these command from a php file (index.php).
I used exec to execute the commands as following:
$contents = file_get_contents('../folder/myscript.sh');
$output = null;
$return_var = null;
exec($contents, $output, $return_var);
print_r($contents);
print_r($return_var);
print_r($output);
Here is the content of myscript.sh
VAR1=("somePath/allDirs")
ls
cd ..
ls
for DIR in "${VAR1[#]}"; do
echo "Name is $DIR"
done
pwd
....... other 5 lines of commands
All commands before the loop is working i.e: ls and cd .. and I got the output. However, the for loop is not working and it stop executing the rest of the commands after it.
My question is: how to execute this for loop that is inside shell script file from php file?
note: the for loop contains more command here I just put the echo to make the code easy to read and clear.
Are you mutating the contents of the script at all? Why not just execute the script file rather than passing in the contents of the script? Regardless, if you must pass the contents directly to exec, pass it to bash -c and escape the command contents:
$contents = file_get_contents('../folder/myscript.sh');
$output = null;
$return_var = null;
$contents = escapeshellarg($contents);
exec("bash -c $contents", $output, $return_var);
I am working in the Laravel framework where I am using shell_exec(). I am creating the string of command as in the following example.
$cmd = "php artisan serve:setup";
$resp = shell_exec($cmd);
How can I put a name to the above command? It shows in the system monitor as only "php"; I have multiple commands like this. I need to access the PID of that command and also the status of that command.
The command is running successfully, and in the system monitor, it shows the process name as "PHP".
I need process name with exec() or shell_exec() from PHP.
I have tried like this:
$cmd = "bash -c exec ServerCPP -a cd $path && php artisan serve:setup 2>&1";
$resp = shell_exec($cmd);
dd($resp);
It gives the error: "Could not open input file: artisan."
Your last attempt needs to be corrected:
cd first and quote the path in case the $path variable contains spaces
quotes around the command executed with bash -c
exec -a ServerCPP if you want to give the name ServerCPP to your process
$cmd = "cd '$path' && bash -c 'exec -a ServerCPP php artisan serve:setup' 2>&1";
$resp = shell_exec($cmd);
dd($resp);
The best idea would be to create a command.
php artisan make:command ServeSetup
Once the file is generated, set the name of your command in app/Console/Commands.
protected $signature = 'serve:setup';
Hope this helps.
I have a shell script in Linux that performs SFTP to get some files. It works fine when I execute it from a terminal.
I am trying to call the script from PHP. It seems to work until the echo, and then it doesn't do anything.
The script and the PHP file are in the same folder.
This is the PHP code:
<?php
$comando = "sh ftpgesdoc.sh";
$result=exec($comando);
echo $result;
?>
And this is shell script. When I execute from the web, I can see the echo "ejecutando sftp", but nothing happens after this point.
#!/bin/sh
echo "ejecutando sftp"
folder="/aaa/bbb"
file="xxx.PDF"
sftp UserXX#nnn.nn.n.nn << EOF
cd $folder
get $file test.pdf
EOF
exec returns only the last line from the command output. If you want to capture entire output, use proc_open. See this answer, for instance.
you have to give the full path to file
and use this 2>&1 and know the error
try something like this
$comando = "sh pathTofile/location/ftpgesdoc.sh";
if(exec("$comando 2>&1", $output, $return_var))
{
print_r($output);
echo "<br>";
print_r($return_var);
}
I want to run this code from php
echo <password> | sudo -S /usr/sbin/asterisk -rx "dongle show devices"
but it's not working. Can anyone help?
You can just use the 'backtick' character (`) around your shell string like:
<?php
$output = `command_goes_here`;
echo $output;
?>
Keep in mind this will only work if the shell_exec() function would work on that server, which could also be used in a similar way.
Use php function shell_exec or exec to execute shell commands
For more details
http://www.php.net/shell_exec
http://php.net/manual/en/function.exec.php
u can try by this if u want to run shell script command in php file
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";
u can also try other way by create .sh file for shell script and run that .sh file by php function
$output = shell_exec('./deploy.sh');
echo "<pre>".$output."</pre>";
I have an application where I need to run a PHP script by exec() function of php.
"php
/var/www/server/data/scripts/ThreadHandler.php
145596 >
/var/www/server/data/logs/threads/thread.145596.log
2>&1 &"
also tried
"php
/var/www/server/data/scripts/ThreadHandler.php
145596 >
/var/www/server/data/logs/threads/thread.145596.log
2>&1 "
I am running above command with exec() function of PHP, but it is not getting run, I can't track any error. please suggest any change.
try
passthru('php -f /var/www/server/data/scripts/ThreadHandler.php 145596 >
/var/www/server/data/logs/threads/thread.145596.log 2>&1');
Also, try to run it from linux command line
Try defining $output and $return_var arguments for exec and print their values :
$output = array();
$rv = null;
exec("your command", $output, $rv);
print_r($rv);
print_r($output);