cakephp 3 Cron Job not working in cpanel - php

I am trying to implement cron job in cakephp 3 shell script but it is not working in cpanel.
below is my cron job code blog is my cakephp 3 folder
cd /home/mmentert/public_html/abc.com/blog && bin/cake hello main
Cakephp 3 shell class file
namespace App\Shell;
use Cake\Console\Shell;
use App\Controller\UsersController;
class HelloShell extends Shell {
public function main() {
$userinfo=new UsersController();
$data=$userinfo->useremail();
$this->out($data);
}
}

I assume you are using shared hosting, the syntax suggested on CakePHP 3 Docs does not work for shared hosting, this is what worked for me
php -q -d register_argc_argv=on /home/public_html/bin/cake.php app main
Please use your own path for cake.php file
-q --no-header Quiet-mode. Suppress HTTP header output (CGI only).
-d --define Set a custom value for any of the configuration directives allowed in php.ini
Hope that helps.

Related

Trouble creating cronjob on Elastic Beanstalk

Im running a PHP web environment on Elastic Beanstalk.
Iv tried 6 ways to Sunday to setup a cronjob:
following AWS template on cronjobs in the .ebextensions folder,
manually creatingn the job in the shell using: crontab -e as suggested here https://stackoverflow.com/questions... and more...
Currently I have this config file in the .ebextensions folder saved as cron-linux.config
files:
"/etc/cron.d/mycron":
mode: "000644"
owner: root
group: root
content: |
*/3 * * * * root /var/www/html/crawler/mine.php >/dev/null 2>&1
commands:
remove_old_cron:
command: "rm -f /etc/cron.d/mycron.bak"
Please help!
Success!
Steps to fixing the issue:
After using the AWS example Cron Job found HERE,
Run the PHP script from the bash script:
#!/bin/bash
php -f /var/www/html/mine.php
exit 0
Then made "mine.php" executable using
sudo chmod +x mine.php
Two issues:
connection details to the database had to be hard coded as the $SERVER variable didnt work (Not sure why)
the part of the script that sends an email with the results still doesnt work.
Not sure why some things work when running the script from the browser but not in bash? But at least the Cron job is working.
Hope this helps someone

Symfony command / Cron jobs doesn't works with Ovh

Here is my problem. I'm developping a Symfony project which acces to telephony and emails data from OVH API. For that, I use cron job (from Ovh board).
I have php files ( home/mySite/www/command.php) which contains :
<?php
shell_exec("sh mycommand.sh");
?>
And my bash file ( home/mySite/mycommand.sh) :
#!/bin/bash
cd /homez.mynumber/mysite/www
/usr/local/php5.6/bin/php bin/console converseo:updateTelephony
I'm sure that the path is good cause I get error line 2: cd: /homez.mynumber/mysite/www: No such file or directory
So I removed line 2 :
#!/bin/bash
/usr/local/php5.6/bin/php bin/console converseo:updateTelephony
And get error No such file or directory
I actually don't know how to make it works. Thanks for yout help !
What is the name of your command? Does it end with "Command" as mentioned in symfony doc: http://symfony.com/doc/current/console.html

OVH cron jobs / Symfony Command

I'm developing a Symfony project and I've 4 commands which allows me to update telephony and emails data thanks to OVH' Api. When I use my terminal on local ( php bin/console converseo:updateTelephony ) the command works fine.
Now, I want to put these commands in crontab with the cron interface from Ovh. I made a php file test.php :
<?php
shell_exec("sh test.sh");
?>
And test.sh :
#!/bin/bash
/usr/local/php5.6/bin/php /homez.number/mysite/www/bin/console converseo:updateBilling
And I get the error :
[Symfony\Component\Console\Exception\CommandNotFoundException]
Command "converseo:updateBilling" is not defined.
Did you mean one of these?
converseo:updateBilling
converseo:updateEmailCount
converseo:updateTelephony
converseo:updateEmail
The lines are EXACTLY the sames, I don't understand why I get this error.
Thanks a lot for yours answers !

How to run yii2 cronjob from linux

I created a cronjob controller in yii2project/console/controllers :
namespace console\controllers;
use yii\base\Model;
use yii\console\Controller;
use Yii;
class MycronController extends Controller {
public function actionIndex() {
echo "cron service runnning";
die;
}
}
In windows i am running this file :
D:\xampp\htdocs\yii2project>d:\xampp\php\php yii mycron
output:
cron service running
Now how to run this in linux?
None of the solutions worked for me, atleast. To make it work from crontab, you have to provide the following command:
* * * * * php /path/to/project/root/yii controller-name/action-name
This example will run every minute by the way. For more information about cronjobs, check this link out.
By the way, if you just want to run the job from your SSH terminal, use this one instead:
php /path/to/project/root/yii controller-name/action-name
Edit: Make sure you have run init command after installing yii with composer. That sets the necessary permissions to run the yii script. If you still can't run it, try chmod +x yii to make the script executable.
Do this:
(take the $ symbol off)
$ /xampp/php/php/./yii mycron
php yii mycron
run in yii2project folder
The Cron-Job is for CentOs.
Open the terminal and navigate to your project folder
cd /var/www/html/advancedyii2
After entering into the folder type
crontab -e
The cron-manager file will be opened in the terminal.
Now edit the cron file like
* * * * * php /var/www/html/advancedyii2/yii controller/function
Before executing the above command make sure a controller is created under
/var/www/html/advancedyii2/console
- Here create a controller and function to server your need
For more about Cron-Configurations Visit this link

Why am I experiencing a weird execution case with my CakePHP Shell?

I use CakePHP (v2) Shell to execute a process behind my online application.
This Shell is part of a Plugin, so stored in:
app/Plugins/MyPlugin/Console/Command/MyScriptShell.php
I used 2 different dev servers, and everything goes well with the execution of this Shell.
Unfortunately, the execution fails on my production environment.
On a Unix terminal, if I try manually to execute the command below, it fails:
# app/Console/cake MyPlugin.MyScript helloworldfunction [params1, paramsN] -app app
PHP Fatal error: Class 'Controller' not found in /var/www/app/Controller/AppController.php on line 6
In an other side, if I execute the same command line with a "sudo -u {a-unix-user} {command}", the execution works! Here are 2 examples which work:
# sudo -u www-data app/Console/cake MyPlugin.MyScript helloworldfunction [params1, paramsN] -app app
# sudo -u root app/Console/cake MyPlugin.MyScript helloworldfunction [params1, paramsN] -app app
Ok, so noticing this behavior, I thought about a unix rights issue or something like this. I decided, as a new test, to grant a unix session of www-data in my termina to execute the same command, but without the "sudo -u {a-unix-user} {command}" directive. Unfortunately, this fails too:
# sudo su www-data
www-data# whoami
www-data
www-data# app/Console/cake MyPlugin.MyScript helloworldfunction [params1, paramsN] -app app
PHP Fatal error: Class 'Controller' not found in /var/www/app/Controller/AppController.php on line 6
www-data#
So here is a summary of the problem I'm occurring right now…
Any idea how to solve this weird behavior?
Thanks.
Ok, so no one found how to help, but I did it myself ;)
For those experiencing a such behavior, I would recommend you to verify the system PATHS set as variable into your code.
In my case, into bootstrap.php, I've a WWW_PATH define which contains the local path to reach the base of the code directory.
define('WWW_PATH', '/home/foo/www-prod/www/');
In reality, my code directory is located under /var/www-prod/www/, and /home/foo/www-prod is just a unix symbolic link.
It seems that CakePhp Shell cannot load properly ressource with a BASE_DIR using a symbolic link, because after altering my code without this symbolic link into the path, it worked!

Categories