How to Run SSH Command Through PHP in Magento 2

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.

Any doubts in this post can be mentioned in the Comments section below.

I’d be happy to help.

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

Thank you.

Related PostList of Useful Magento 2 SSH/CLI Commands

Get Weekly Updates

Never miss Magento tips, tricks, tutorials, and news.

Thank you for subscribing.

Something went wrong.

5
(based on 12 Reviews)

Amit was a Magento developer at Meetanshi. He has good experience in Information Technology and education field over more than 8 years.

Leave a reply:

Your email address will not be published.