Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Running terminal commands with PHP

Friday, March 15th, 2019 by Servage

terminalSometimes you may find yourself in a situation where you want to run more than just PHP code. Perhaps PHP does not have a built-in function for what you want to do or you want to run some custom commands, such as executing an external program. There are multiple ways to achieve this, and we will learn how to do it in two different ways.

The usual way: exec() and shell_exec()

These two functions are likely the ones that come to your mind first when thinking about a way to execute a terminal command. Both of there are basic PHP functions, and they are very similar to each other. The biggest difference between these functions is that shell_exec() returns all output produced by the command while exec() only returns the last line of output by default.

These functions are simple to use: call the function and pass the command you want to run as the first argument, for example: $directoryListing = shell_exec(”ls”). This will run the ls command that lists all files and directories in the current directory. The list will be saved in the $directoryListing variable as a string. You could also use exec(”ls”) to get the last line of output.

These commands are usually enough if you want to execute simple commands using PHP, but sometimes you may need more advanced alternatives. There are a couple different ways to run terminal commands, and one popular way is to use the process component from the Symfony framework.

The advanced way: Symfony Process Component

Being a more advanced way to run terminal commands, this is also a litte more difficult to set up. We won’t cover setting up a Composer project here, but the easiest way to install the component is to require it using Composer. You can do so with composer require symfony/process. If you are not familiar with Composer, you can also download the process component from GitHub. You can read more about this on the official Symfony documentation page.

When you have set up the component, first you have to create a new instance of the process object. As the only parameter to the constructor, you can pass the command you want to run. An example looks like this: $process = new Process(”ls”). This will only create an instance of the process, but the command will not be executed yet. This is because you can specify some extra functionality before running the command. You can for example disable all output or set a timeout for the process.

When you are ready to run the comand, you can do so by calling $process->run() or $processstart(). Both of these will execute the command, but run() waits for the command to finish executing while start() runs the command asynchronously. This allows you to run multiple commands without slowing down your application.

As you can see, the process component of Symfony is a more flexible and powerful tool. It even allows you to return data in real-time and return incremental output from a command if you want to retrieve the output of a process at a later time.

Running terminal commands with PHP, 3.2 out of 5 based on 11 ratings
Categories: Tips & Tricks


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.