# Command execution from Controller

**URL:** <https://discourse.cakephp.org/t/command-execution-from-controller/8988>\
**Category:** Need Help\
**Created:** [February 9, 2021, 2:11pm UTC](https://discourse.cakephp.org/t/command-execution-from-controller/8988 "2021-02-09T14:11:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rsuyambu](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@rsuyambu](https://discourse.cakephp.org/u/rsuyambu)\
**Post date:** [February 9, 2021, 2:11pm UTC](https://discourse.cakephp.org/t/command-execution-from-controller/8988/1 "2021-02-09T14:11:32Z")

</div>

Created custom commands to import the data( **Not a shell comand** ), I want to execute the command from the controller, Let me know how can I execute the command in cakephp4 controller.

I tried to use  
use Cake\Console\CommandRunner;

$command = new CommandRunner();  
$command-\>run([‘import\_users’]);

But I am getting below error while creating object\*\*($command = new CommandRunner();)\*\*  
“Too few arguments to function Cake\Console\CommandRunner::\_\_construct()”

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [February 9, 2021, 5:32pm UTC](https://discourse.cakephp.org/t/command-execution-from-controller/8988/2 "2021-02-09T17:32:38Z")

</div>

Seems pretty straight-forward. The constructor for that class needs parameters; it needs at least a `ConsoleApplicationInterface` object.

---

<div class="post-metadata">

**Author:** ![raul338](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/raul338/32/69_2.png) [@raul338](https://discourse.cakephp.org/u/raul338)\
**Post date:** [February 10, 2021, 12:46am UTC](https://discourse.cakephp.org/t/command-execution-from-controller/8988/3 "2021-02-10T00:46:03Z")

</div>

If the comand spends more time than the maximum request time (usually 30-40 seconds), the executing commands is killed when reaches that limit.

I suggest creating a process with that command, so that the request finishes quickly. The command should be responsible to update process progress somewhere (like having a `tasks` model and update `percent` there)

Create a new process like this: (launch `ExampleCommand`)

```php
exec(
    'sh -c "php -d memory_limit=512M ' . ROOT .
    '/bin/cake.php Example ' . $params .
    ' > /dev/null 2>&1 &"'
);

```

For more advanced queue plugins you can use:  
[queue](https://github.com/cakephp/queue)  
[cakephp-queue](https://github.com/dereuromark/cakephp-queue)

---

<div class="post-metadata">

**Author:** ![Czepter](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/czepter/32/1975_2.png) [@Czepter](https://discourse.cakephp.org/u/Czepter)\
**Post date:** [May 15, 2022, 9:21pm UTC](https://discourse.cakephp.org/t/command-execution-from-controller/8988/4 "2022-05-15T21:21:58Z")

</div>

Did anyone solved this? Got the same issue and the reply from Zuluru is not realy helpfull ^^

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [May 16, 2022, 2:12am UTC](https://discourse.cakephp.org/t/command-execution-from-controller/8988/5 "2022-05-16T02:12:57Z")

</div>

If you’re getting the exact same error as the OP was, then my reply should be helpful. If you’re getting some other issue, then of course it would not be.

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [May 16, 2022, 8:49am UTC](https://discourse.cakephp.org/t/command-execution-from-controller/8988/6 "2022-05-16T08:49:50Z")

</div>

The initial question was: How can I execute a command inside a controller?

The answer to this is: DONT

You should instead refactor your logic into a generic utility/service class which you then call in your command as well as in your controller.

See my CakeFest talk explaining exactly this issue: [CakeFest Virtual 2021 Day 2 - How to re use code: Utility Classes and PHP Namespaces - Kevin Pfeifer - YouTube](https://www.youtube.com/watch?v=z3E_UdH1XeE)

---

<div class="post-metadata">

**Author:** ![jmcd73](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jmcd73/32/481_2.png) [@jmcd73](https://discourse.cakephp.org/u/jmcd73)\
**Post date:** [July 14, 2022, 8:16am UTC](https://discourse.cakephp.org/t/command-execution-from-controller/8988/7 "2022-07-14T08:16:32Z")

</div>

Thankyou I was just about to make a mess of trying to shoe horn CommandRunner into a controller. I greatly appreciate your contributions of knowledge Kevin.
