简体   繁体   中英

Laravel 5.2 - How to run shell command via Modal / Controller

How i can run shell command via a Model or Controller. Not through Artisan command or $schedule.

And also how i can run a sudo command?

I have try the next options. Nothing works:

1:

$process = new Process('sudo /usr/bin/touch /var/www/html/Poptin test2.html');
                $process->run();
                // executes after the command finishes
                if (!$process->isSuccessful()) {
                    throw new ProcessFailedException($process);
                }

2:

shell_exec(escapeshellcmd('sudo /usr/bin/touch /var/www/html/Poptin test2.html));

3:

exec('sudo /usr/bin/touch /var/www/html/Poptin test2.html);

Help please!

Have just tested running exec in Laravel 5.5 and it works. This is what i tried:

$test = exec('echo 123');
dd($test);

I know you are running 5.2, but it should be no different i terms of running this.

I can see that you have forgot an ending ' in the exec method and also the shell_exec.

Instead of writing

exec('sudo /usr/bin/touch /var/www/html/Poptin test2.html);

You should write

exec('sudo /usr/bin/touch /var/www/html/Poptin test2.html');

Also make sure that the command you are trying to run works when running it in the shell directly.

您所做的事情是正确的,但是您必须像这样做,例如shell_exec('sudo / usr / bin / touch / var / www / html / Poptin test2.html')尝试一下,在shell中使用shell_exec控制器是一个坏习惯。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM