Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rafidAhsan's avatar

Multiple corn jobs command are not getting executed.

i have created the cron job for service charge, it works properly the purpose of service charge is to deduct a certain amount of money from every user after 10 months the same way i implemented cron job for default charge, and its purpose is to deduct a certain amount when user does not pay installment accordingly. Now the cron job for default does not get executed when i use it in cpanel. Hope someone can help me out

Service charge is working

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

use App\SavingAcount;

use App\UserNotification;

use App\User;

use DB;

use App\Accounts;

use App\ServiceChrg;

class ServiceCharge extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'day:juneFirst';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Your Service Charge is automatically cut from your savings by our system. Thanks for stay with us';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $users = User::get();

        foreach($users as $user) {
            $total = 0;
            if($user->hasSavings() == $user->id) {
                $service_charges = new ServiceChrg();
                $saving = SavingAcount::where('user_id', $user->id)
                    ->latest()
                    ->first();
                $saving->total = $saving->total - 20;
                $saving->save();

                $service_charges->user_id = $user->id;
                $service_charges->save();

                $total += 20;

                // Service charge goes to accounts
                $accounts = new Accounts();
                $accounts->service_charge = 20;
                $accounts->user_id = $user->id;

                $row = count(Accounts::select('service_charge')->where('user_id', $user->id)->get());
                if($row == 0) {
                    $accounts->total_service_charge = 20;
                    $accounts->total = 20;
                }   else {
                    $prev_fees = Accounts::where('user_id', $user->id)->latest()->first();
                    $accounts->total_service_charge = 20 + $prev_amount->total_service_charge;
                    $accounts->total = $accounts->total_service_charge + $accounts->total_default_charge + $accounts->total_fee;
                }

                $accounts->save();

                $username = "Alauddin101";
                $hash = "4f9ec55ab0531a44a466910119d97847";
                $numbers = $user->mobile_number; //Recipient Phone Number multiple number must be separated by comma
                $message = '20tk has been deducted for your service charge. Thank you! Your current saving is '.$saving->total;

                $params = array('app'=>'ws', 'u'=>$username, 'h'=>$hash, 'op'=>'pv', 'unicode'=>'1','to'=>$numbers, 'msg'=>$message);

                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, "http://alphasms.biz/index.php?".http_build_query($params, "", "&"));
                curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json", "Accept:application/json"));
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

                $response = curl_exec($ch);
                curl_close ($ch);

                $accounts->save();
            }
        }
    }
}

For Cpanel

/usr/local/bin/php /home/afreensh/bdloans.afreenshop.com/system/artisan day:juneFirst >> /dev/null 2>&1

Default Charge Command is not working. But I use same way

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

use App\User;

use App\BusinessLoan;
use App\EduLoan;
use App\EmployeeLoan;

use App\SavingAcount;

use App\LoanInstallment;

class DefaultCharge extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'day:everyMonth';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Status of ddefault charge';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $users = User::get();

        foreach ($users as $user) {
            $user_id = $user->id;

            if($user->hasBusinessLoans() == $user_id) {
                $loan = BusinessLoan::where('user_id', $user->id)->where('completed', 0)->first();
                $loan_installments = LoanInstallment::where('loan_id', $loan->id)->latest()->first();

                if($loan_installments == '') {
                    // user doesn't give first installment
                    $saving = SavingAcount::where('user_id', $user->id)->latest()->first();
                    $saving->total = $saving->total - 20;
                    $saving->save();

                    // Default charge goes to accounts
                    $accounts = new Accounts();
                    $accounts->default_charge = 20;
                    $accounts->user_id = $user->id;

                    $row = count(Accounts::select('default_charge')->where('user_id', $user->id)->get());
                    if($row == 0) {
                        $accounts->total_default_charge = 20;
                        $accounts->total = 20;
                    }   else {
                        $prev_fees = Accounts::where('user_id', $user->id)->latest()->first();
                        $accounts->total_default_charge = 20 + $prev_amount->total_default_charge;
                        $accounts->total = $accounts->total_service_charge + $accounts->total_default_charge + $accounts->total_fee;
                    }

                    $accounts->save();

                    $username = "Alauddin101";
                    $hash = "4f9ec55ab0531a44a466910119d97847";
                    $numbers = $user->mobile_number; //Recipient Phone Number multiple number must be separated by comma
                    $message = '20tk has been deducted for your late of Loan from saving. Thank you! Your current saving is '.$saving->total;

                    $params = array('app'=>'ws', 'u'=>$username, 'h'=>$hash, 'op'=>'pv', 'unicode'=>'1','to'=>$numbers, 'msg'=>$message);

                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL, "http://alphasms.biz/index.php?".http_build_query($params, "", "&"));
                    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json", "Accept:application/json"));
                    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

                    $response = curl_exec($ch);
                    curl_close ($ch);
                }   else {
                    $to = \Carbon\Carbon::parse($loan->approved_date)->floorMonth();
                    $from = \Carbon\Carbon::parse($loan_installments->this_month)->floorMonth();
                    $net_installment_month = $to->diffInMonths($from);

                    if($loan_installments->amount < $net_installment_month * $loan->perInstallmentAmount) {
                        $saving = SavingAcount::where('user_id', $user->id)->latest()->first();
                        $saving->total = $saving->total - 20;
                        $saving->save();

                        // Default charge goes to accounts
                        $accounts = new Accounts();
                        $accounts->default_charge = 20;
                        $accounts->user_id = $user->id;

                        $row = count(Accounts::select('default_charge')->where('user_id', $user->id)->get());
                        if($row == 0) {
                            $accounts->total_default_charge = 20;
                            $accounts->total = 20;
                        }   else {
                            $prev_fees = Accounts::where('user_id', $user->id)->latest()->first();
                            $accounts->total_default_charge = 20 + $prev_amount->total_default_charge;
                            $accounts->total = $accounts->total_service_charge + $accounts->total_default_charge + $accounts->total_fee;
                        }

                        $accounts->save();

                        $username = "Alauddin101";
                        $hash = "4f9ec55ab0531a44a466910119d97847";
                        $numbers = $user->mobile_number; //Recipient Phone Number multiple number must be separated by comma
                        $message = '20tk has been deducted for less amount then per installment amount of Loan from saving. Thank you! Your current saving is '.$saving->total;

                        $params = array('app'=>'ws', 'u'=>$username, 'h'=>$hash, 'op'=>'pv', 'unicode'=>'1','to'=>$numbers, 'msg'=>$message);

                        $ch = curl_init();
                        curl_setopt($ch, CURLOPT_URL, "http://alphasms.biz/index.php?".http_build_query($params, "", "&"));
                        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json", "Accept:application/json"));
                        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

                        $response = curl_exec($ch);
                        curl_close ($ch);

                        return 0;
                    }   else {
                        return 0;
                    }
                }
            }   elseif($user->hasEmployeeLoans() == $user_id) {

            }   elseif($user->hasEduLoans() == $user_id) {

            }   else {
                // user has no loan
                return 0;
            }
        }
    }
}

Cpanel code for default charge

/usr/local/bin/php /home/afreensh/bdloans.afreenshop.com/system/artisan day:everyMonth >> /dev/null 2>&1
0 likes
8 replies
Snapey's avatar

You would have more control running cron every minute to run the Laravel scheduler and then firing your commands from within the Laravel Scheduler.

rafidAhsan's avatar

Can you give me any example. Do you mean to say that, I have to run both the commands at the same time? If the both command have to be executed then I have to ensure that individual commands work properly.

Thank you

rafidAhsan's avatar

@sinnbeck When I try to execute individual command in my localhost they do work. But unfortunately the same command is not executed in cPanel. Even after when I use schedule:run command. So therefore I was looking for some suggestions and assistants.

Thank you

Sinnbeck's avatar

Please show how it is currently set up (after our suggestions)

Snapey's avatar

@rafidahsan Please mention someone if you want a reply otherwise we don't know the question has been updated.

You set your cron to run the laravel scheduler every minute. Then you don't have any business critical logic in the server configuration - it can live in your application in version control.

So in app\Console\Kernel.php, in the schedule section;

    protected function schedule(Schedule $schedule)
    {
	// Run the task on the first day June at 17:00        
        $schedule->command('day:juneFirst')->yearlyOn(6, 1, '17:00');

        // Run the task on the first day of every month at 00:00
        $schedule->command('day:everyMonth')->monthly(); 
    }

note that the times are only checked at that exact minute stated. If your site is down at 17:00 on 1st June then the job will not run and none of the account balances will be adjusted, even if the site comes back on line at 17:01

You should have operational checks to ensure the commands have worked. A possibility is monitoring with https://thenping.me/

rafidAhsan's avatar

@snapey Then you don't have any business critical logic in the server configuration - it can live in your application in version control.

Can you elaborate pleas.

Thank you

Sinnbeck's avatar

@rafidahsan If you set it up in the crontab with exact times, then everybody on the team will need to know these cronjobs schedule to know when they are run. And if you ever migrate servers, you need to set them up exactly the same. If you simply have 1 cronjob that is running every minute, and the schedule is inside laravel, then it will always work. No need for any special configuration.

Please or to participate in this conversation.