1

I want to run a custom PHP script in Magento 2 webshop using a custom module.

But I want it to run only when installing or during installation of my custom module.

To be specific, during setup:upgrade command and will trigger only once except if there's a change in my custom module version.

Anyone tried that before?
Can you point me into right direction on how to do it?

Thanks!

asked Feb 14, 2019 at 7:18

2 Answers 2

1

You can use PHP shell_exec() command for your requirement.

You can execute shell_exec() command in Install or Upgrade Schema of module. So when your module is installed or you change module version your code will be executed.

If it is not standalone PHP script then another approach would be to Just execute your Module's functions in Install script.

Find below example to execute shell script

<?php
namespace Vendor\Module\Setup;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
class UpgradeSchema implements UpgradeSchemaInterface {
 /**
 * {@inheritdoc}
 */
 public function upgrade(
 SchemaSetupInterface $setup, ModuleContextInterface $context
 ) {
 if(version_compare($version1, $version2)){
 //execute your script here
 shell_exec('base_path/scriptname.php'); // this will execute custom php script at given location
 }
 }
}
answered Feb 14, 2019 at 7:24
7
  • can you site an example , I mean a sample code on implementation Commented Feb 14, 2019 at 7:25
  • @magefms example included Commented Feb 14, 2019 at 7:32
  • let me check. I'll update you later Commented Feb 14, 2019 at 7:38
  • is base_path dynamic or static? should i change it ? Commented Feb 14, 2019 at 8:00
  • this doesn't work Commented Feb 14, 2019 at 8:17
0

I am not sure what you want exactly but according to your description, You may use magento default InstallSchema feature to achieve this.

Like if you create a module, and create following file.

File: [Vendor]/[Module]/Setup/InstallSchema.php

<?php
namespace [Vendor]\[Module]\Setup;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
class InstallSchema implements InstallSchemaInterface
{
 public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
 {
 $setup->startSetup();
 // Your php script code here
 $setup->endSetup();
 }
}

You may execute your code in between, which will only be executed while only installation of your module.

answered Feb 14, 2019 at 7:32
2
  • I'll check first Commented Feb 14, 2019 at 7:40
  • You want to execute php file through shell scripting ? Commented Feb 14, 2019 at 8:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.