Run .sh file using exec Laravel PHP

后端 未结 3 1872
灰色年华
灰色年华 2021-01-14 20:15

I am trying to run a .sh file that will import a excel file to my database. Both files are in same directory inside the public folder. For some reason the exec command isn\'

相关标签:
3条回答
  • 2021-01-14 20:32

    All of these answers are outdated now, instead use (Symfony 4.2 or higher):

    $process = Process::fromShellCommandline('/deploy.sh');
    

    Or

    $process = new Process(['/deploy.sh']);
    

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

    0 讨论(0)
  • 2021-01-14 20:33

    I know this is a little late but I can't add a comment (due to being a new member) but to fix the issue in Windows " 'sh' is not recognized as an internal or external command, operable program or batch file." I had to change the new process from:

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

    to use the following syntax:

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

    The only problem with is that when uploading to a Linux server it will need to be changed to call sh.

    Hope this helps anyone who hit this issue when following the accepted answer in Windows.

    0 讨论(0)
  • 2021-01-14 20:51

    you can use Process Component of Symfony that is already in Laravel 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();
    
    0 讨论(0)
提交回复
热议问题