Run .sh file using exec Laravel PHP

16,582

Solution 1

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();

Solution 2

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

Solution 3

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.

Share:
16,582
Saurav
Author by

Saurav

Updated on June 04, 2022

Comments

  • Saurav
    Saurav almost 2 years

    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't being executed or neither any error occurs.

    .sh file colde:

    IFS=,
    while read column1  
          do
            echo "SQL COMMAND GOES HERE"
    
    done < file.csv | mysql --user='myusername' --password='mypassword' -D databasename;
    echo "finish"
    

    In my php file i have tried following:

    $content = file_get_contents('folder_name/file_name.sh');
    echo exec($content);
    

    And:

    shell_exec('sh /folder_name/file_name.sh');
    

    Note: I can directly execute the sh file from gitbash however I want it to be done using function in Laravel controller. I'm using windows OS.

  • Saurav
    Saurav almost 7 years
    Thakyou but now i'm getting an error: Exit Code: 1(General error) Output: ================ Error Output: ================ 'sh' is not recognized as an internal or external command, operable program or batch file.
  • apokryfos
    apokryfos almost 7 years
    Use the full path to sh since the webserver service is under a different user and a different PATH variable.
  • Saurav
    Saurav almost 7 years
    @apokryfos I alrady tried that. Actually its not recognizing the sh as a command. Any help
  • apokryfos
    apokryfos almost 7 years
    @Saurav That's odd, the gitbash is sh.exe which should normally run as a windows executable.
  • Saurav
    Saurav almost 7 years
    When i use the command sh fullpath/filename.sh using gitbash it works properly, but It return error when trying from laravel.
  • Saurav
    Saurav almost 7 years
    Well it was windows and everything is running perfectly in the server. Thanks both.
  • Alessandro Minoccheri
    Alessandro Minoccheri almost 7 years
    Glad to help you!
  • Alex UnLimited
    Alex UnLimited almost 4 years
    Hi, just tried both your solutions but it seems like nothing happend. I tried to execute a .sh file that creates a .txt file. I didn't recive any error or someting like that.(also my .sh file is in the main project folder so I tried ['/comp.sh']. ['comp.sh'] and the full path). Do you have any idea why?