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
Related
I'm running a Laravel PHP application as a container on AWS ECS. The container runs both php-fpm and nginx in the same container.
I didn't write the application, I'm a DevOps guy, but I'm struggling with a Command that I added recently to generate a sitemap.xml.
The important bits of the application's Dockerfile is an ENTRYPOINT script which contains this...
nginx -g "daemon off;" &
echo "--- Clearing artisan cache"
yes | php artisan config:clear
echo "--- Running migrations"
yes | php artisan migrate
echo "--- Seeding database"
yes | php artisan db:seed
echo "--- Running final container command"
exec "$#"
And its CMD looks like this:
CMD ["php-fpm", "--nodaemonize", "--force-stderr"]
The PHP command I C&P'd from the Spatie sitemap generator examples looks like this:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Spatie\Sitemap\SitemapGenerator;
class GenerateSitemap extends Command
{
/**
* The console command name.
*
* #var string
*/
protected $signature = 'sitemap:generate';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Generate the sitemap.';
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
// modify this to your own needs
SitemapGenerator::create(config('app.url'))
->writeToFile(public_path('sitemap.xml'));
}
}
If I exec into the running container and run php artisan schedule:list I see the following output:
0 0 * * * php artisan sitemap:generate ........ Next Due: 12 hours from now
The container has been running for nearly a week with this command, but if I run find / -name "sitemap.xml" I see no results and if I view mywebsite.co.uk/sitemap.xml I get no response.
The main problem I'm having is that I have no idea how to diagnose any problems with this command. I've got Sentry configured correctly (it reports exceptions - but nothing related to this command) and my container logs only appear to show Nginx access logs.
If I navigate through the container's file structure, I can't see any PHP logs at all. I'm wondering if the PHP logs are working as expected, but the command is just failing silently?
Any thoughts on the command itself or the container PHP logs would be greatly appreciated.
I have a command scheduled in the Laravel 5.4 scheduler and would like to start the Laravel cron on Mac OS X El Capitan.
app/Console/Kernel.php
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
protected $commands = [
'App\Console\Commands\GetToken'
];
protected function schedule(Schedule $schedule) {
$schedule->command('gettoken')->everyMinute();
}
protected function commands() {
require base_path('routes/console.php');
}
}
My GetToken.php makes an API call and then a DB change. I believe that this is working properly, as I can run the task directly from the cli using:
php /path/to/project/artisan schedule:run 1>> /dev/null 2>&1
To edit my cron file I use:
env EDITOR=nano crontab -e
I then add:
* * * * * php /path/to/project/artisan schedule:run >> /dev/null 2>&1
I save with ctrl+o and exit with ctrl+x.
Re-editing the file shows that the changes have saved.
Running crontab -l shows the text that I entered into the crontab file.
My cron never runs. I can only get it to run once by running manually using the command I mentioned above.
Not directly answering your question, but proposing another solution:
If you want to set up cron jobs for your development environment, it's best to use Homestead, for its Linux standards compliance.
For small projects that i develop directly inside macOS, i run the following command inside the project root (in a separate terminal tab) to have my jobs run every minute:
while true; do php artisan schedule:run; sleep 60; done
This helps to make sure, the cron jobs are only run while i'm developing. When i'm done, i Ctrl+C that command and can be sure nothing unexpected happens while i'm not watching.
Plus it gives me the freedom to adjust the interval, by simple choosing another number of seconds for the sleep command. This can save time when developing.
Update Laravel 8.x
Laravel now offers the above as a single artisan command:
php artisan schedule:work
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.
I have Apache running on Debian with PHP 5.4.
PHP-cli is installed.
My directory structure for the web project is:
- /myproject
- /src
- /controller
- getProviders.php
- /model
- /public
- ClassLoader.php
I want to create a cron job to execute getProviders.php every 5 minutes. This is as far as I have come:
*/5 * * * * /usr/bin/php /var/www/myproject/src/controller/getProviders.php
It doesn't work because I have a require_once in getProviders.php requiring ClassLoader.php, but he can't find it.
require_once "../ClassLoader.php"
getProviders.php works when executed via URL.
I'm not new to PHP development, but new at configuring the server around it.
What do I have to do to make it work. I'm guessing I have to set the include path, but I have no idea to what exactly.
Thanks in advance for your help.
Create a shell script like this in /usr/sbin (For Example: getProviders)
#!/bin/bash -x
cd /var/www/myproject/src/controller/
php getProviders.php
give permission
chmod a+x /usr/sbin/getProviders
in /etc/crontab
*/5 * * * * root /usr/sbin/getProviders
the problem is probably due to inclusions not in absolute value in the php script
To make sure the require_once will always work, you could use dirname(__FILE__) + the path relative to the getProviders.php
In your case this would be:
require_once(dirname(__FILE__)."/../ClassLoader.php");
Change to the right workdirectory.
You could do this with the cd command or inside your PHP script with the chdir() function.
I have custom Artisan commands that run locally as well as on my production server when I am SSH'd in, but are unavailable to any cron jobs. I've even tried running it as the user the cron job runs as and it works fine from my console.
When I run php artisan in the above settings, my custom commands are listed and available. However, they are not listed when I run php artisan as a cron job.
Furthermore, trying to run the custom command php artisan subjects:calculate as a cron job results in the following error:
[InvalidArgumentException]
There are no commands defined in the "subjects" namespace.
I was fighting with the same error and I found the solution.
First failed attempts
*/5 * * * * /usr/bin/php /home/mysite/public_html/artisan my:command
*/5 * * * * php /home/mysite/public_html/artisan my:command
Solution
*/5 * * * * /usr/local/bin/php /home/mysite/public_html/artisan my:command
Be sure to add the command to app/start/artisan.php file:
Artisan::add(new SubjectsCommand);
or if you are using the IOC container:
Artisan:resolve('SubjectsCommand');
Then run the CronJob from the folder of the app:
00 09-18 * * 1-5 php /path/to/yourapp/artisan subjects:calculate
or
00 09-18 * * 1-5 /usr/bin/php /path/to/yourapp/artisan subjects:calculate
At least for me that worked:
class Kernel extends ConsoleKernel
{
protected $commands = [
// Commands\YourCommand::class,
];
}
Just added my command to the list of command kernel.
You probable need to make sure artisan is running from the correct directory
cd /path/to/yourproject && php artisan subjects:calculate