I am trying to implement a cron job in my lumen project. I have BookingMaster Table when a user is creating a booking I am setting the default status to B means booked in the table. in the day of booking I am trying to update the status to I to database means In-progress. When I am doing this locally the cron is running perfectly and the status is also updating.
But when I moved this code to my shared hosting it is not working any more. The cron is not updating the status in database.
Location Of the BookingUpdate.php is – app/Console/Commands/BookingUpdate.php
BookingUpdate.php
<?php
namespace AppConsoleCommands;
use Helpers;
use IlluminateConsoleCommand;
use AppBookingMaster;
class BookingUpdate extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'BookingUpdate:booking-update';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Cron for Booking Update';
public static $process_busy = false;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle(){
if (self::$process_busy == false) {
self::$process_busy = true;
$where['status'] = 'B';
$update = BookingMaster::updateRecord(6,$where);
self::$process_busy = false;
echo 'Done';
return true;
} else {
if ($debug_mode) {
error_log("Process busy!", 0);
}
return false;
}
}
}
karnel.php
<?php
namespace AppConsole;
use IlluminateConsoleSchedulingSchedule;
use LaravelLumenConsoleKernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
'AppConsoleCommandsBookingUpdate',
];
/**
* Define the application's command schedule.
*
* @param IlluminateConsoleSchedulingSchedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
//
}
}
Cron Job Command :
/usr/local/bin/php -q /home/rahulsco/public_html/api.pawsticks/artisan schedule:run 1>> /dev/null 2>&1
3
Answers
There might be a several issues with your code not running in Cron.
/usr/local/bin/php
try running Cron with justphp -q
#!/usr/bin/env php
or some similar combination.artisan schedule:run
so it might work once you put your command in quotation marks and escape spacesphp -q "/home/rahulsco/public_html/api.pawsticks/artisan schedule:run" 1>> /dev/null 2>&1
Finally, if anything else fails I would try logging something to a file and checking after cron runs, maybe there is some other error in your directory configuration causing your script to fail before writing to database and the cron is running fine…
In app/Console/kernel.php, the schedule function should be like this:
Then your BookingUpdate process will run daily at midnight. There are various other options to schedule your task as mentioned here: https://laravel.com/docs/7.x/scheduling#schedule-frequency-options
Also, you can simply execute the cron manually at any time using:
php artisan BookingUpdate:booking-update
PS. Replace
BookingUpdate:booking-update
with$signature
variable value defined in your command class you need to execute.Tested with shared server – linux with laravel/lumen API.
It’s working seamlessly.
Note: Wrap your URL inside double quote