3

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
  • 355
  • 1
  • 3
  • 13

4 Answers4

12

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();
Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
  • 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. – Saurav Jun 05 '17 at 17:42
  • Use the full path to `sh` since the webserver service is under a different user and a different `PATH` variable. – apokryfos Jun 05 '17 at 17:52
  • @apokryfos I alrady tried that. Actually its not recognizing the sh as a command. Any help – Saurav Jun 05 '17 at 17:57
  • @Saurav That's odd, the gitbash is `sh.exe` which should normally run as a windows executable. – apokryfos Jun 05 '17 at 17:59
  • When i use the command sh fullpath/filename.sh using gitbash it works properly, but It return error when trying from laravel. – Saurav Jun 05 '17 at 18:04
  • Well it was windows and everything is running perfectly in the server. Thanks both. – Saurav Jun 06 '17 at 04:45
5

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

Alexander Kim
  • 17,304
  • 23
  • 100
  • 157
  • 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? – Alex UnLimited Jun 29 '20 at 08:48
2

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.

CloudSPRT
  • 33
  • 1
  • 5
1

In Symfony 5.2.0 that used by Laravel 8.x (same as current Symfony Version 6.0 used by Laravel 9.x), you need to specify the sh command and your argument, in your case:

use Symfony\Component\Process\Process;

$process = new Process(['/usr/bin/sh', 'folder_name/file_name.sh']);
$process->run();

The system will find folder_name/file_name.sh from your /public folder (if it executed from a url), if you want to use another working directory, specify that in the second Process parameter.

$process = new Process(['/usr/bin/sh', 'folder_name/file_name.sh'], '/var/www/app');

And the /usr/bin/sh sometimes have a different place for each user, but that is a default one. Type whereis sh to find it out.

TheArKa
  • 350
  • 2
  • 5
  • 12