How To Execute Cron Job Manually?

Hi All,

Currently We have scheduled No. Of Cron Jobs which will be executed As per the schedule time.

Using PHP Can We execute the cron job Manually ?

As per my experience, we can directly execute the PHP Script that we have setup in the cron job.

But is it possible to execute the cron job manully using the linux command that we can pass to php’s exec() function ?

execute the cron job manully using the linux command that we can pass to php’s exec() function ?

It sounds like you already have your solution. :slight_smile:

But i want to know, how can we call cron job command using php’s exec() function ?

Each cron job will take more than 15 hours to complete the process. So If i will allow the user to run cron job script through a external url there will be problem related to connection and all.

If the script is terminated then the execution for rest of the process will not be completed.

I have no. of stored procedures that will be called one after another in the php script. So let us assume that if currently user has started cron job and one stored procedure execution is in process at that time if the connection is lost then rest of the stored procedures mentioned in the php script will not be called.

Why not create a table in a RDBMS of your choosing to store completed / pending runs?

You could create a PHP script to create a pending entry in this table, then increase your current CRON frequency to check this table and decide whether or not to run.

Yes, i have already implemented that thing. And i am tracking the scheduled cronjob status in that way. But the thing is that now we have user who will be executing the script.

For example, if user has started the cron job script manually from external URL. And after some time if user closes the browser intentionally or unintentionally or system is switched off or anything from client side then the execution of rest of the script will be stopped.

The script the user is calling, should NOT be the script that process your job. It should only create an entry in your job table as pending.

It would essentially only consist of the following:-


<?php
/**
 * ==Statuses==
 * 
 * 0 = Requested / Pending
 * 1 = Running
 * 2 = Complete
 * 3 = Cancelled
 */
#UserRunRequest.php
$sSQL = sprtinf("INSERT INTO table (user, requested, completed, status)VALUES('%s', %s, 0, 0);",
    $sUsername,
    time()
);
$rResult = mysql_query($sSQL);
?>

You would then have a separate script that would determine how to proceed based on the data contained within the queue.

  • Has a job ran in the x hours, if so cancel all current requests
  • If no jobs scheduled and its been over x hours schedule a job and run it marking its status as ‘running’.
  • etc…
  • etc…

execute the php binary from a shell


/usr/bin/php /path/to/script.php > /dev/null &

Can I call the above mentioned command using php’s exec() function ?

Because the cron script will be executed by the user. And after calling the script if user closes the browser than also i want the procees to be continued.