使用exec Laravel PHP运行.sh文件

Sau*_*rav 2 php shell command sh laravel

我正在尝试运行一个.sh文件,将excel文件导入我的数据库.这两个文件都位于公用文件夹中的同一目录中.由于某种原因,exec命令没有被执行或者没有任何错误发生.

.sh文件colde:

IFS=,
while read column1  
      do
        echo "SQL COMMAND GOES HERE"

done < file.csv | mysql --user='myusername' --password='mypassword' -D databasename;
echo "finish"
Run Code Online (Sandbox Code Playgroud)

在我的php文件中,我尝试过以下内容:

$content = file_get_contents('folder_name/file_name.sh');
echo exec($content);
Run Code Online (Sandbox Code Playgroud)

和:

shell_exec('sh /folder_name/file_name.sh');
Run Code Online (Sandbox Code Playgroud)

注意:我可以直接从gitbash执行sh文件,但我希望它可以使用Laravel控制器中的函数来完成.我正在使用Windows操作系统.

Ale*_*eri 9

您可以使用Laravel中已有的Symfony的Process Component http://symfony.com/doc/current/components/process.html

use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;

$process = new Process('sh /folder_name/file_name.sh');
$process->run();

// executes after the command finishes
if (!$process->isSuccessful()) {
    throw new ProcessFailedException($process);
}

echo $process->getOutput();
Run Code Online (Sandbox Code Playgroud)


Ale*_*Kim 5

所有这些答案现在都已过时,请改用(Symfony 4.2 或更高版本):

$process = Process::fromShellCommandline('/deploy.sh');
Run Code Online (Sandbox Code Playgroud)

或者

$process = new Process(['/deploy.sh']);
Run Code Online (Sandbox Code Playgroud)

https://symfony.com/doc/current/components/process.html