Run PHP with cron

0

I need to run a PHP script automatically, and for that I'm using the Cent'os Cron and the project is written with the Yii framework. The fact is that I am trying to add a crontab with:

crontab -e

And then I add the script path and the moment I want it to run:

***** /var/www/html/ClubGolf cliente/informeMateriales

(The project is ClubGolf , the controller is cliente e informeMateriales is an action of the controller)

When I run the crontab I get an error telling me that the time is wrong (or something like that) and I do not know why it can happen:

"/tmp/crontab.wqcMnf":1: bad hour errors in crontab file, can't install

Maybe I wrote the crontab badly, or I may have written the wrong route, since I do not write a complete route, but I take it directly to the action of the controller ...

    
asked by Csc99 03.08.2018 в 14:44
source

2 answers

1

Being a php file, you need to run it with the php interpreter.

In the terminal it is used to execute php files php file.php . To call functions of a PHP file in the command line, php -r is used, that what it does is to execute what it has in quotes. Then, first, we include the file where the function is and then we call the function that you want to execute. So in the crontab I should run it just like the console.

I think it's best to do a .sh file with crontab execution:

file.cron.sh

#!/bin/sh
php -r "require '/path/to/file.php'; functionToExecute()"

And then in the crontab:

* * * * * username /path/to/file.cron.sh
    
answered by 03.08.2018 в 15:02
0

I think the best thing you can do is create a command within Yii and call it through the terminal and the interpreter of PHP

In the own documentation of Yii you have how to create a console command, you could simply call that function: link

Once you have that command and it executes well directly in the end you can put it in the cron as follows:

***** /usr/bin/php /var/www/html/yii mi-comando

The first thing is the PHP interpreter, the second the path to your Yii and the third the name of the command within Yii

Remember to modify the cron of the user that has permissions in the folder in case you have to create a file or something you believe it with that user and not another or you have an error.

If you have any doubts, you tell me about it.

    
answered by 15.08.2018 в 12:28