How to execute external shell commands from laravel controller?
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
How to Execute External Shell Commands from Laravel Controller
When working with Laravel, it is often necessary to execute external shell commands directly in your controller. This may include file operations or running system-wide scripts outside the project's directory. Here's a comprehensive guide on how to achieve this.Using PHP's Native Function: system()
You can utilize the native PHP function 'system()' to execute external shell commands in your Laravel controller. Let's consider an example where you want to delete a file located outside your project directory. To do this, use the code below:public function removeFile() {
system('rm /var/www/html/test.html');
}
Note that the 'system()' doesn't run as a part of your application, but rather executes a command directly on the operating system. Thus, you should be careful when running commands with security implications or affecting other processes on the system.
Using PHP's exec()
Another option available in PHP is using 'exec()'. It works very much like 'system()', but it allows more control over command execution. For instance, you could use it with '&' to run a command in the background or pipe the output to another process:public function executeCommand() {
exec('rm /var/www/html/test.html &', $output);
}
Remember that using 'exec()' might not provide you with a complete command output, unlike 'system()'. However, it gives more control over the execution process.
Using Artisan Commands for Ease and Security
If you wish to run shell commands in your application regularly or need added security measures, consider creating an Artisan command: 1. Create a new PHP artisan command using 'make:command'. For example: `php artisan make:command RemoveFileCommand` 2. Open the newly created command file (RemoveFileCommand.php) and add your desired logic to execute your shell commands, ensuring it's secure:public function handle() {
// Add your shell command execution code here
}
3. Run the Artisan command from the terminal using `php artisan RemoveFileCommand` or even schedule it to run automatically via a cron job or similar scheduler.
Using an Artisan command provides greater control over the execution process and ensures your application's security by restricting shell commands only when necessary.