How to execute a Symfony command as cron job? - php

I have this simple command in symfony :
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class AuctionEndCommand extends Command
{
protected function configure()
{
error_log(print_r('test',true), 3, "/tmp/error.log");
$this->setName('desktop:auction_end')->setDescription('Execute when auction is end.')->setHelp("Identify winners for auctions");
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// outputs multiple lines to the console (adding "\n" at the end of each line)
$output->writeln([
'User Creator',
'============',
'',
]);
// outputs a message followed by a "\n"
$output->writeln('Whoa!');
// outputs a message without adding a "\n" at the end of the line
$output->write('You are about to ');
$output->write('create a user.');
}
}
Now when I execute : /var/www/myProject/bin/console desktop:auction_end this command works fine. But when I try to execute as a cron in linux this script doesn't start :
In linux as sudo I did :
nano crontab -e, and the cron :
* * * * * /usr/bin/php /var/www/myProject/bin/console desktop:auction_end > /dev/null
What I'm doing wrong, can you help me please ? Thx in advance and sorry for my english

I can keep it very short. You will need to use PHP to execute the console.
* * * * * php -q /usr/bin/php /var/www/myProject/bin/console desktop:auction_end > /dev/null
Writing the output of a cron job that you are testing to a file will help you debug errors. All output is now lost in the void :)
* * * * * php -q /usr/bin/php /var/www/myProject/bin/console desktop:auction_end > /home/<user>/crons/auction_end.cron.txt
Edit
It might be that your php should be used as absolute path.
* * * * * /path/to/php /path/to/bin/console symfony:command
Or even by specifying the user to execute the command with:
* * * * * root /usr/bin/php /path/to/bin/console command:to:execute
Also make sure that the root user has permission to execute files in your symfony project.

What user are you executing the cron as?
Symfony requires write access to ( depending on version ) app/cache && app/log(s) || var/cache && var/log(s) if the user you're executing the command as doesn't have write access to the directories you're trying to write to then your operation will fail.
A good way of fixing this would be to check the error logs, either in var/log(s) app/log(s) or check the apache2 error logs in /var/log/apache2/error.log I would guess one of these logs will contain a hint to your problem.

If you run it as root, does that work:
* * * * * root php /var/www/myProject/bin/console desktop:auction_end > /dev/null
See if that works.

Related

Cron job runs and stops mysteriously?

I have a cron set on ubuntu to run some Laravel scheduled commands. It's running sometimes and stopping at random times. last week it did not run the commands for the whole week, the it started to run again yesterday. This morning it stopped again. I have tried everything but nothing seems to be solving the issue. What am i missing?
Here is the cron:
*/15 * * * * ubuntu cd /var/www/html && php artisan schedule:run >> /dev/null 2>&1
And Here is my Kernel.php:
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
\App\Console\Commands\TrackData::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('data:track')->withoutOverlapping()
->everyFifteenMinutes()->appendOutputTo(storage_path().'/logs/laravel_output.log');
$schedule->command('queue:listen')->withoutOverlapping()
->everyFifteenMinutes()->appendOutputTo(storage_path().'/logs/laravel_output.log');
$schedule->command('delete:oldxml')->withoutOverlapping()
->dailyAt('00:01');
// $schedule->command('sitemap:generate')
// ->daily();
}
/**
* Register the commands for the application.
*
* #return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
I've tried everything but nothing seems to solve the problem. What am i missing
it might be many reasons for this kind of problem where your crontab suddenly stopped working.
it might be related to crontab user. to solve the problem, you can try to delete all crontabs for both root and other users.
You can catch the script error into a log file for better investigation as following.
*/15 * * * * ubuntu cd /var/www/html && php artisan schedule:run >> /tmp/script.log 2>&1
it might be related to the PHP config. Your script may run longer than your php.ini's max_execution_time and also check set_time_limit()
You can check cron log by using this command tail -fn 100 /var/log/cron
it might occur due to script file permission and ownership of script files. so make sure you give proper permission to the script file.
chmod +x yourscript.sh chown root:root yourscript.sh
it might be related to OS. Maybe your webserver disk is full or an OS that can't spawn new threads.
Maybe your script has begun to overlap themselves and */15 * * * * is not enough. you can try to increase time to */30 * * * *

Digitalocean cron and Laravel

I have a hard time running cron job on Digitalocean. I noticed there are two different cron files, I get to one by getting to /etc/crontab and other one by entering the command crontab -e.
To make it more confusing, both of those have somewhat different "layout". First one:
* * * * * root php /var/www/Laravel artisan schedule:run >> /home/laravel.log
and second one:
* * * * * php /var/www/Laravel artisan schedule:run >> laravel.log
Here is Laravel scheduler part:
protected $commands = [
'App\Console\Commands\SyncAPIs',
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('media:sync')->everyThirtyMinutes();
// sync for yesterday to eliminate discrepancies
$schedule->command('media:sync ' . Carbon::yesterday())->dailyAt(6);
}
The thing is that the laravel.log does get created, but I see nothing in it, and I don't know if my commands actually get ran. Does that mean that cron IS actually running? How can I debug the issue as I don't see in the database that cron filled it. When I go to the folder /var/www/Laravel and execute the commands which are supposed to be called in scheduler, dataabse fills correctly.
cd ~
crontab -u root -e
* * * * * php /var/www/laravelprojectname/artisan schedule:run 1>> /dev/null 2>&1
service cron restart
grep -i cron /var/log/syslog|tail -3
I think this should help
May be you need to change this line :
* * * * * php /var/www/Laravel artisan schedule:run >> laravel.log
to :
* * * * * /usr/bin/php /var/www/Laravel artisan schedule:run >> laravel.log
I found an answer being actually:
* * * * * php /var/www/Laravel/artisan schedule:run >> laravel.log
php works globally, but artisan needs to be shown the path which is in my Laravel main folder

cronjob is not working Linux

I want a script file to run once every minute.. I've written this command.
* * * * * php -q /home/<username>/public_html/cron.php
But, this cronjob is not working. whenever, I try to open this file cron.php in browser, it works fine.
I'm using Linux OS. Is there a way to debug it in order to come to know the error?
If you're using Ubuntu as I am, use the full path.
* * * * * /usr/bin/php -q /home/<username>/public_html/cron.php
Have you added an empty line (new line) after your cronjob?
To debug:
Append 2>&1 to the end of your Crontab command. This will redirect the stderr output to the stdout. Then ensure you're logging the crontab's Unix command.
* * * * * php -q /home/<username>/public_html/cron.php; ls -la >>/var/log/cronrun 2>&1
This will capture anything from the Unix command.
A couple of additional hints: Write out the environment variables by issuing the command set with no parameters. And get the shell to echo each command with the set -x command. At the top of your script issue;
set
set -x
For cPanel, you may want to test curl (in case it's installed on your server):
curl --silent --compressed http://www.your-domain.com/cron.php
So it should look similar to: http://grabilla.com/0450d-93d93a32-02ab-457c-ac1c-d2883552a940.html#
You may also want to try removing the -q from your command and see if it helps.
* * * * * php /home/<username>/public_html/cron.php
*/1 * * * * /usr/bin/php -q /home//public_html/cron.php
Add the above line to the crontab file and run it . It will add a cronjob every minute

Cron job for PHP yiic command

I have created a php yiic command as
class StartCommand extends CConsoleCommand {
public function run($params){
Yii::log("Cron Job has started");
}
}
When I run the command php yiic Start, I get the result in the log.
And I have add this to the crontab
* * * * * /usr/bin/php /path/app/console/commands/StartCommand.php
But the job is not running.
I think I have to mention the php yiic Start to the crontab.
But where do I add it ??
* * * * * cd /path/app/console/commands && php yiic Start

How to setup a CakePHP Cron / Shell task on MacOSX?

I have a cakephp shell that works correctly. If I run
/Users/petarpetrov/path_to/app/Console/cake CleanDatabase
it works great. However, as per documentation if I try this:
* * * * * cd /Users/petarpetrov/path_to/app && Console/cake CleanDatabase
I get the error:
-bash: Config: command not found
I noticed that it does matter where I am at the point of issuing the crontab command. If I run it from / I would get:
-bash: Applications: command not found
So I have really no idea what is going on. Any help?
Task code:
<?php
class CleanDatabaseShell extends AppShell {
public function main() {
$this->out('hello');
}
}
I figured it out. I was pasting this line in terminal which was wrong:
* * * * * cd /Users/petarpetrov/path_to/app && Console/cake CleanDatabase
when in fact I should have done:
crontab -e to open the editor
paste the line in
save & close it.

Categories