How to Run SSH Command Through PHP in Magento 2

Magento 2 offers SSH commands to perform actions like clean cache, reindex data, display status of indexer, etc. These commands can be run from the command prompt.

However, if you do not want to use the command prompt, the other method is to run SSH command through PHP in Magento 2.

You may want to do so in case of creating a script and running it at regular interval of time.

Here, I’ve taken the example to flush cache by the PHP script.

Method to Run SSH Command Through PHP in Magento 2:

echo "<pre>";
print_r(system('php72 bin/magento cache:flush')); // You can change command as you want.
echo "</pre>";

That’s it.

Also, do share the post with Magento Community via social media.

Thank you.

Sanjay Jethva

Article by

Sanjay Jethva

Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe. His passion for Magento 2 and Shopify solutions has made him a trusted source for...