Cake 4.x - bin/cake.php got deleted after running command [solved]

Hello,

I created a command I want to run as cronjob and when I tested it, the file bin/cake.php got deleted.
So now, I can’t either run commands or event bake something.

What should I do to fix it ?

maybe it helps to copy the missing file from the github repository ?

I can’t, the folder is locked.

Considering this task is going to run automatically, I don’t want the file to be destroyed at all.

then create a new empty project and copy the file from there to your project.
if you don’t want your task to delete any files, you have to change your task. No one instead of you knows what task you created.

Here’s my code :

// IN Table/ApisTable.php

namespace App\Model\Table;

use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\Http\Client;

class ApisTable extends Table{
	public function getUrl(){
		$http = new Client();
		$response = $http->get('http://api.tvmaze.com/shows/1');
		return $response;
	}
}




// IN Entity/Api.php

namespace App\Model\Entity;

use Cake\ORM\Entity;

class Api extends Entity{

}



//IN Command/TestCommand.php

namespace App\Command;

use Cake\Command\Command;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;

class TestCommand extends Command {

        public function execute(Arguments $a, ConsoleIo $io){
                $this->loadModel('Apis');
                $data = $this->Apis->getUrl();
                $io->out($data);
        }
}

Then, I run

bin/cake test

and the file bin/cake.php gets destroyed

the command “bin/cake test” ist wrong, it should be “bin/cake bake test”.

dirk@home==> cakephp % bin/cake test

Unknown command `cake test`. Run `cake --help` to get the list of commands.

Did you mean: `help`?

Other valid choices:

- help

- version

- cache list

- i18n

- routes

- server

- console

- bake test

- bake all

- bake task

- bake seed

- bake

dirk@home==> cakephp % bin/cake bake test

You must provide a class type to bake a test for. The valid types are:

1. Entity

2. Table

3. Controller

4. Component

5. Behavior

6. Helper

7. Shell

8. Task

9. ShellHelper

10. Cell

11. Form

12. Mailer

13. Command

Re-run your command as `cake bake <type> <classname>`

dirk@alltimerecipes cakephp %

Hummm, I don’t want the system to build somethings (what bake is for) I want to run the command.

I followed the command instructions : Command Objects - 4.x

I changed for testing your TestCommand,.php as follows:

<?php
namespace App\Command;

use Cake\Command\Command;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;

class TestCommand extends Command {

        public function execute(Arguments $a, ConsoleIo $io){
                $this->loadModel('Apis');
                $data = $this->Apis->getUrl();
                //$io->out($data);
                $io->out($data->getStringBody());
        }
}

The output looks like this (which seems to be ok):

dirk@home cakephp % bin/cake test
{"id":1,"url":"https://www.tvmaze.com/shows/1/under-the-dome","name":"Under the Dome","type":"Scripted","language":"English","genres":["Drama","Science-Fiction","Thriller"],"status":"Ended","runtime":60,"premiered":"2013-06-24","officialSite":"http://www.cbs.com/shows/under-the-dome/","schedule":{"time":"22:00","days":["Thursday"]},"rating":{"average":6.6},"weight":95,"network":{"id":2,"name":"CBS","country":{"name":"United States","code":"US","timezone":"America/New_York"}},"webChannel":null,"externals":{"tvrage":25988,"thetvdb":264492,"imdb":"tt1553656"},"image":{"medium":"https://static.tvmaze.com/uploads/images/medium_portrait/81/202627.jpg","original":"https://static.tvmaze.com/uploads/images/original_untouched/81/202627.jpg"},"summary":"<p><b>Under the Dome</b> is the story of a small town that is suddenly and inexplicably sealed off from the rest of the world by an enormous transparent dome. The town's inhabitants must deal with surviving the post-apocalyptic conditions while searching for answers about the dome, where it came from and if and when it will go away.</p>","updated":1573667713,"_links":{"self":{"href":"https://api.tvmaze.com/shows/1"},"previousepisode":{"href":"https://api.tvmaze.com/episodes/185054"}}}
dirk@home cakephp %

Yes, the output is ok, that’s not the problem.

The problem is that the bin/cake.php file was deleted after the command ran.

if we are talking about the files in folder /bin…
In my test project the files (“cake”, “cake.bat”, “cake.php”) are still there. nothing gets deleted. sorry, but I think I can’t help you.

Which version of cake are you using ?
I’m on 4.2.4.

I also used 4.2.4 for testing.

And you did nothing else instead of running the scripts you showed?

No, just that. I even tried on test projects without anything else.
If I run a command with other instructions (create entity, save, etc), it works fine.
But as soon as there is ->get(URL) the file get deleted.

I’m sorry, maybe there is anyone else who can help you.
In my test project I can repeat “/bin cake test” as often as I want. and when I then change into directory “bin” the files are still there.

dirk@home cakephp % bin/cake test

{"id":1,"url":"https://www.tvmaze.com/shows/1/under-the-dome","name":"Under the Dome","type":"Scripted","language":"English","genres":["Drama","Science-Fiction","Thriller"],"status":"Ended","runtime":60,"premiered":"2013-06-24","officialSite":"http://www.cbs.com/shows/under-the-dome/","schedule":{"time":"22:00","days":["Thursday"]},"rating":{"average":6.6},"weight":95,"network":{"id":2,"name":"CBS","country":{"name":"United States","code":"US","timezone":"America/New_York"}},"webChannel":null,"externals":{"tvrage":25988,"thetvdb":264492,"imdb":"tt1553656"},"image":{"medium":"https://static.tvmaze.com/uploads/images/medium_portrait/81/202627.jpg","original":"https://static.tvmaze.com/uploads/images/original_untouched/81/202627.jpg"},"summary":"<p><b>Under the Dome</b> is the story of a small town that is suddenly and inexplicably sealed off from the rest of the world by an enormous transparent dome. The town's inhabitants must deal with surviving the post-apocalyptic conditions while searching for answers about the dome, where it came from and if and when it will go away.</p>","updated":1573667713,"_links":{"self":{"href":"https://api.tvmaze.com/shows/1"},"previousepisode":{"href":"https://api.tvmaze.com/episodes/185054"}}}

dirk@home cakephp % cd bin

dirk@home bin % ls

cake cake.bat cake.php

dirk@home bin %

There is nothing that should be automatically deleting required system files, but I don’t see anything in your provided code that would do it either. There’s presumably some more that you aren’t showing us?

No, nothing more than that. I tried several times in new projects, so I would be sure it wasn’t anything else

I got it ! (Victory !)

After trying it on another computer, I saw the issue : my antivirus placed the file in quarantine when reaching another url.

Wow diabolical, but understandable. The AV should have notified you though. May I please ask which AV you’re running? It would be good to know for future reference!

It is Avast.
And it is my fault : their notifications are so annoying, I had put it in silence mode on my main computer.

glad you found the solution. thanks for sharing the solution