Sunday, January 22, 2023

Set cron for laravel in ngnix server

SSH into your server and navigate to the root directory of your Laravel application.

Run the command crontab -e to open the cron tab editor.

If you want to run cron in every minute then : 

Add the following line to the editor: * * * * * php /path/to/your/laravel/installation/artisan schedule:run >> /dev/null 2>&1

Save and exit the editor.

This will run the Laravel task scheduler every minute.


Note: You should replace the /path/to/your/laravel/installation/ with the actual path of your laravel installation in the server.


Alternatively, you can also add the cron job using the cron command. You can do this by running the following command:


* * * * * cd /path/to/your/laravel/installation && php artisan schedule:run >> /dev/null 2>&1

This will change the current working directory to your laravel installation and runs the schedule:run command every minute.

If you want to run cron in every day: 

0 0 * * * cd /path/to/your/laravel/installation && php artisan schedule:run >> /dev/null 2>&1
This will change the current working directory to your laravel installation and runs the schedule:run command every day at 00:00 (midnight)

You can adjust the time in the cron schedule by modifying the first two fields. The first field is for minutes (0-59), the second field is for hours (0-23), the third field is for day of the month (1-31), the fourth field is for months (1-12) and the fifth field is for day of the week (0-7) (both 0 and 7 mean Sunday).

Make sure that your Nginx server have the correct permissions to execute the cron job, and also make sure that your laravel application have the correct permissions to write to log files, cache and other necessary files.

You can also verify that the cron job is running correctly by checking the logs of the cron jobs, usually located in the /var/log/cron or /var/log/syslog directory. 

It is not necessary to set up a cron job in the cron tab when using Supervisor to manage your Laravel task scheduler.

setup laravel supervisor in ngnix server

 Install Supervisor:


sudo apt-get install supervisor

Create a new configuration file for your Laravel task scheduler in the /etc/supervisor/conf.d/ directory. The file should be named laravel-worker.conf

Add the following configuration to the file:

Copy code

[program:laravel-worker]

process_name=%(program_name)s_%(process_num)02d

command=php /path/to/your/laravel/installation/artisan schedule:run

autostart=true

autorestart=true

user=www-data

redirect_stderr=true

Replace /path/to/your/laravel/installation/ with the actual path of your Laravel installation.

Update the Supervisor's configuration by running the command:

Copy code

sudo supervisorctl reread

Start the Laravel task scheduler process by running the command:

Copy code

sudo supervisorctl start laravel-worker

With this configuration, Supervisor will start the Laravel task scheduler process when the server boots, and it will automatically restart the process if it crashes. It also keeps track of the process and log any errors that may occur.


You can also use sudo supervisorctl stop laravel-worker to stop the process or sudo supervisorctl status laravel-worker to check the status of the process.


You can also use sudo supervisorctl tail -f laravel-worker to see the logs of the process


Note: Make sure you have supervisor installed in your server and you have the correct permissions to manage the process.


Also, make sure your Nginx server have the correct permissions to execute the cron job, and also make sure that your laravel application have the correct permissions to write to log files, cache and other necessary files.

To restart Supervisor, you can use the following command:

sudo service supervisor restart

Alternatively, you can also use the following command:

sudo systemctl restart supervisor

This command is used to restart the supervisor service on systems that use systemd.

You can also use the following command to check the status of the Supervisor service:

sudo service supervisor status

or

sudo systemctl status supervisor

This command will show you if the service is running or not and if there are any errors.

Please note that after restarting supervisor, you will need to update the process by running the following command:

sudo supervisorctl update

Example for laravel cron job which will send 2 birhtday notification mail to user first is one day before and second is the day when birthday is

In your terminal, navigate to the root directory of your Laravel application.

Run the command crontab -e to open the cron tab editor.

Add the following line to the editor: * * * * * php /path/to/your/laravel/installation/artisan schedule:run >> /dev/null 2>&1

Save and exit the editor.

This will run the Laravel task scheduler every minute.


Next, you can define two new tasks in your app/Console/Kernel.php file that will send birthday notifications to users.


use App\User;

use Carbon\Carbon;

use Illuminate\Console\Scheduling\Schedule;

use Illuminate\Support\Facades\Mail;


class Kernel extends ConsoleKernel

{

    protected function schedule(Schedule $schedule)

    {

        $schedule->call(function () {

            $users = User::all();

            foreach ($users as $user) {

                if($user->birthday->isToday()) {

                    Mail::to($user->email)->send(new BirthdayEmail($user));

                }

                if($user->birthday->isTomorrow()) {

                    Mail::to($user->email)->send(new BirthdayReminderEmail($user));

                }

            }

        })->daily();

    }

}

In model: 

use Carbon\Carbon;


class User extends Authenticatable

{

    // other model properties and methods


    public function isToday()

    {

        return Carbon::today()->isSameDay($this->birthday);

    }

   public function isTomorrow()

    {

        return Carbon::tomorrow()->isSameDay($this->birthday);

    }

    public function isTwoDaysBefore()

    {

        return Carbon::now()->addDays(2)->isSameDay($this->birthday);

    }

}