4

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.

4 Answers 4

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();
Sign up to request clarification or add additional context in comments.

7 Comments

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.
Use the full path to sh since the webserver service is under a different user and a different PATH variable.
@apokryfos I alrady tried that. Actually its not recognizing the sh as a command. Any help
@Saurav That's odd, the gitbash is sh.exe which should normally run as a windows executable.
When i use the command sh fullpath/filename.sh using gitbash it works properly, but It return error when trying from laravel.
|
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

1 Comment

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?
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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.