We help IT Professionals succeed at work.

PHP:  create and delete cron jobs using PHP

detox1978
detox1978 asked
on
Hi All,

Using PHP and cron i want to create two scripts/cron jobs;

Script 1/Cron 1:  Cron 1 runs Script 1, which creates a the following cron job; * * * * * php /home/public_html/cron.php
Script 2/Cron 2:  Cron 2 runs Script 2, which delete all cron jobs except Cron 1 and Cron 2

Ideally I would like Script 1 to run every 28 hours, and Script 2 to run every 30 hours.



Many thanks
Comment
Watch Question

Most Valuable Expert 2011
Top Expert 2016

Commented:
I would set up the cron jobs in the usual way, and have a script copy the template scripts to the cron folder.  The cron scheduler needs pre-defined names, so if you are deleting scripts but want to put them back later you will need to use the predefined names.

What is the application that needs this capability?

Author

Commented:
It's a custom application with poor documentation.

I can get the name of the cron jobs it creates.

Is there a way to manage these automatically?
Most Valuable Expert 2011
Top Expert 2016

Commented:
To manage these automatically?  Perhaps, but it would help to know more about what the jobs are trying to achieve.  Please tell us more, thanks, ~Ray

Author

Commented:
It's a 3rd party script so i dont really know what they do.

Is there a way to create a cron job from a PHP file?

Also, is there a way t delete a cron job from PHP?

Commented:
Hi detox1978,
I believe your website hosting provider will provide you with a GUI facility to setup crons. Did you try that? Take a look at it & you might find it useful. You can set the timings to run, the scripts to run & all administrative options regarding the Crons via this GUI. You should normally be able to see this option among other options when you login into your account.

Hope that helps.

Author

Commented:
Yeah, I have been creating the cron jobs and deleting them manually using cPanel.  But i need an automated method.


I know this can be done, simple because cPanel can add/remove them.


Most Valuable Expert 2011
Top Expert 2016

Commented:
But surely you have some idea of what the cron jobs do, right?  Or you have the scripts that the cron jobs execute?  I can't imagine how you would set up automated execution of jobs if you did not know what they do.  Please tell us a little more about the application, thanks, ~Ray

Commented:
Hi detox1978,
The GUI itself automates the process. It's only the initial setup that you have to take care of. So if you are already using the CRON GUI in your account & thereby have automated the process, then what other kind of  "automation" are you seeking?

Author

Commented:
~Ray,  the cron jobs run several PHP script in a specific order to import and compare data from several external sources.  The issue i have is for the import and comparision to work the cron must be run as * * * *, but this cripples the server.  So we only want it to run for a few hours each day as * * * *.  I've tried runing the scripts on a slow interval, but data returned was out of sync.  I have exec() access on the server, can PHP manually create/delete cron jobs?


~mepro, currently i have to log into cpanel twice a day.  once to create the cron job and once to delete it.  I need a method in PHP of creating/deleting cron jobs, so i can create a cron job to manage the cron job that needs to be created/deleted everyday.
Most Valuable Expert 2011
Top Expert 2016

Commented:
Hey, detox... I was hoping to get a higher level view of the application.  I understand all that about CRON and cPanel - I am trying to figure out what the application does - what are the inputs and outputs, and why is there an assumption that a CRON job is needed.  If you can, please, start with the assumption that you cannot run any CRON jobs at all, and tell us what the application requirements are.  Something like "Every day at 4:00pm I have to stop everything and change its diaper..."  What I am suggesting is that the CRON assumption might not be the best answer or the only answer.  Given a better understanding of what you need to do, we may be able to suggest good alternatives to using exec().

Best regards, ~Ray

Author

Commented:
Is it possible to manage cron jobs using PHP or via another cron job?


I'm grateful for your offer to solve the bigger issue, but I wouldn't get authorization to implement any other solution than to automate the current manual process.
Most Valuable Expert 2011
Top Expert 2016

Commented:
"Is it possible to manage cron jobs using PHP or via another cron job?"

Sure.  You can have a "cron manager" that runs as frequently as once a minute, for example.  It will use CURL POST to start an asynchronous script.  The cron manager is datetime-aware, whereas the asynch script would not need to be.

This kind of thing would have undetectably low overhead.  Does that make sense for your app?

Author

Commented:
yes this would be great.  Do you know what the PHP code is to add/delete cron jobs is?
Most Valuable Expert 2011
Top Expert 2016
Commented:
I'm trying to suggest that adding and deleting cron jobs is the wrong design pattern, at least as far as I can tell from the information I have in front of me.  

Instead add ONE cron job that runs once a minute.  Each time it goes off, it checks the current time against an array.   The array is a multidimensional array.  It has keys that are ISO8601 time strings (you could make this more complicated and interesting if you want.)  It has values that are arrays of script names.  Whenever the time matches a key in the array that is set to a value, the CRON job starts each of the scripts with a CURL POST.

This is an untested "pidgin" script, but it will illustrate what I am talking about.

As you can see, whenever you want to make a change, you just change the array of things you want to run, and upload a new version of this script -- the change will take effect the very next minute.
<?php // RAY_cron_manager.php
error_reporting(E_ALL);

// THE ARRAY OF TIME SIGNALS AND SCRIPTS THAT MUST BE RUN
$arr = array
('0800' => array('reveille.php'),
('1200' => array('mess.php', lunchtime.php'),
('1730' => array('retreat.php'),
('2200' => array('taps.php')
);

// WHAT TIME IS IT
$now = date('Hi');

// ANY WORK TO DO - IF NOT, DIE
if (!isset($arr[$now])) die();

// START THE SCRIPTS FOR THIS TIME OF DAY
foreach ($arr[$now] as $script_name)
{
    my_curl_post_function($script_name);
}

Open in new window

Author

Commented:
Thanks for the script.


You've made me think I can add a time variable to the PHP script the cron runs, so that it only does something if it is within an allowed timeframe.


Many thanks
Most Valuable Expert 2011
Top Expert 2016

Commented:
Yes - you might consider making this array into a data base table, so you could "check off" when the script is run. That way you might be able to have an audit trail.  CRON tasks are not 100% perfect, especially in shared server environments.  But for the most part they will work right, and a task that is supposed to start once a minute, usually starts once a minute.

Thanks for the points and best regards, ~Ray