简体   繁体   中英

How to run Cron Job on Shared Hosting? (Lumen)

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 App\Console\Commands;

use Helpers;
use Illuminate\Console\Command;
use App\BookingMaster;

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 App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        '\App\Console\Commands\BookingUpdate',

    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $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

There might be a several issues with your code not running in Cron.

  • Maybe the correct path to PHP is not on the /usr/local/bin/php try running Cron with just php -q
  • Some systems require the script to start with #!/usr/bin/env php or some similar combination.
  • There is a space in your cron command on this part artisan schedule:run so it might work once you put your command in quotation marks and escape spaces php -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:

protected function schedule(Schedule $schedule) {
    $schedule->command('BookingUpdate:booking-update')->daily();
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM