How to Programmatically Create a New Cron Job

How can I programmatically create a new cron job?

The best way if you're running as root, is to drop a file into /etc/cron.d

if you use a package manager to package your software, you can simply lay down files in that directory and they are interpreted as if they were crontabs, but with an extra field for the username, e.g.:

Filename: /etc/cron.d/per_minute

Content:
* * * * * root /bin/sh /home/root/script.sh

How do I create a crontab through a script

Cron jobs usually are stored in a per-user file under /var/spool/cron

The simplest thing for you to do is probably just create a text file with the job configured, then copy it to the cron spool folder and make sure it has the right permissions (600).

How to create a cron job using Bash automatically without the interactive editor?

You can add to the crontab as follows:

#write out current crontab
crontab -l > mycron
#echo new cron into cron file
echo "00 09 * * 1-5 echo hello" >> mycron
#install new cron file
crontab mycron
rm mycron

Cron line explaination

* * * * * "command to be executed"
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

Source nixCraft.

How to set up a Cron Job Programmatically?

Why not using cPanel API v2 ?

cPanel Inc created a Client XML API, guess what.. it's using cURL to call API.

Firstly get the xmlapi.php file, now search in the xmlapi.php these lines :

private $port               =   '2087';
private $protocol = 'https';

In order to make it work with a cPanel account without root access, change the $port to 2083 (HTTPS) or 2082 (HTTP), obviously, if you're using HTTP-Port, change the $protocol to http.

cPanel API has a Cron module documentation as you asked, you can delete, add and even edit a CronJob.

Example of use

List all CronJob :

require_once 'xmlapi.php';

/*
* Instanciate the class, setting up username/password/IP
* @ip - cPanel server IP, if this script is on the cPanel server replace $ip by $ip = getenv('REMOTE_HOST');
* @account - string - your cPanel username
* @pass - string - your cPanel password
*/

$ip = '127.0.0.1';
$account = 'username';
$pass = "password";
$xmlapi = new xmlapi($ip, $account, $pass);

/*
* Just to be sure that XML-API will use the correct port and protocol
* @set_port(port); change port to 2082 if it isn't redirected to HTTPS and/or using HTTP protocol, else.. use 2083
* @set_protocol(protocol); change protocol to http if your sever accept HTTP else put the protocol to https
* @set_output(format); change to XML if you want the result output w/ XML, JSON if you want the result output w/ JSON
*/
$xmlapi->set_port('2083');
$xmlapi->set_protocol('https');
$xmlapi->set_output("json");
$xmlapi->set_debug(1);

/*
* @api2_query(account, module, function, params)
*/
print $xmlapi->api2_query($account, "Cron", "listcron");

Per example, with a single line in CronJob it'll return :

{"cpanelresult":{"data":[{"day":"1","minute":"1","hour":"1","count":1,"command_htmlsafe":"/usr/bin/php5","command":"/usr/bin/php5","weekday":"1","month":"1","linekey":"7209fe24c876a729b42a929692c62ce3"},{"count":2}],"apiversion":2,"module":"Cron","event":{"result":1},"func":"listcron"}}

Create a CronJob

require_once 'xmlapi.php';

/*
* Instanciate the class, setting up username/password/IP
* @ip - cPanel server IP, if this script is on the cPanel server replace $ip by $ip = getenv('REMOTE_HOST');
* @account - string - your cPanel username
* @pass - string - your cPanel password
*/

$ip = '127.0.0.1';
$account = 'username';
$pass = "password";
$xmlapi = new xmlapi($ip, $account, $pass);

/*
* Just to be sure that XML-API will use the correct port and protocol
* @set_port(port); change port to 2082 if it isn't redirected to HTTPS and/or using HTTP protocol, else.. use 2083
* @set_protocol(protocol); change protocol to http if your sever accept HTTP else put the protocol to https
* @set_output(format); change to XML if you want the result output w/ XML, JSON if you want the result output w/ JSON
*/
$xmlapi->set_port('2083');
$xmlapi->set_protocol('https');
$xmlapi->set_output("json");
$xmlapi->set_debug(1);

/*
* @command string - The command, script, or program you wish for your cronjob to execute.
* @day int - The day on which you would like this crontab entry to run. Wildcards and any acceptable input to a crontab time expression line are allowed here.
* @hour int - The hour at which you would like this crontab entry to run. Wildcards and any acceptable input to a crontab time expression line are allowed here.
* @minute int - The minute at which you would like this crontab entry to run. Wildcards and any acceptable input to a crontab time expression line are allowed here.
* @month int - The month you would like this crontab entry to run. Wildcards and any acceptable input to a crontab time expression line are allowed here.
* @weekday int - The weekday on which you would like this crontab entry to run. Wildcards and any acceptable input to a crontab time expression line is allowed here. Acceptable values range from 0 to 6, where 0 represents Sunday and 6 represents Saturday.
*/

$command = "/usr/bin/php cron.php";
$day = "1";
$hour = "1";
$minute = "1";
$month = "1";
$weekday = "1";

/*
* @api2_query(account, module, function, params)
*/
print $xmlapi->api2_query($account, "Cron", "add_line", array(
"command"=>$command,
"day"=>$day,
"hour"=>$hour,
"minute"=>$minute,
"month"=>$month,
"weekday"=>$weekday
));

Obviously, it returns you this :

{"cpanelresult":{"module":"Cron","event":{"result":1},"apiversion":2,"data":[{"statusmsg":"crontab installed","status":1,"linekey":"9b0c93fe238a185e4aa78752a49a0718"}],"func":"add_line"}}

Removing a CronJob

Before explaining how to remove a CronJob, you have to know the line of the CronJob that you want to remove.. If you check "List all CronJob" on the response part, you may saw a count in the JSON response, exactly this :

[{"day":"1","minute":"1","hour":"1","count":1,"command_htmlsafe":"/usr/bin/php5","command":"/usr/bin/php5","weekday":"1","month":"1","linekey":"7209fe24c876a729b42a929692c62ce3"},{"count":2}]

The exact line is the "count":1 after the "hour":1 and NOT the latest "count":2, as you understand, the line is the..... FIRST (well done sherlock).

Now we can use the same script with a Curl::remove_line :

require_once 'xmlapi.php';

/*
* Instanciate the class, setting up username/password/IP
* @ip - cPanel server IP, if this script is on the cPanel server replace $ip by $ip = getenv('REMOTE_HOST');
* @account - string - your cPanel username
* @pass - string - your cPanel password
*/

$ip = '127.0.0.1';
$account = 'username';
$pass = "password";
$xmlapi = new xmlapi($ip, $account, $pass);

/*
* Just to be sure that XML-API will use the correct port and protocol
* @set_port(port); change port to 2082 if it isn't redirected to HTTPS and/or using HTTP protocol, else.. use 2083
* @set_protocol(protocol); change protocol to http if your sever accept HTTP else put the protocol to https
* @set_output(format); change to XML if you want the result output w/ XML, JSON if you want the result output w/ JSON
*/
$xmlapi->set_port('2083');
$xmlapi->set_protocol('https');
$xmlapi->set_output("json");
$xmlapi->set_debug(1);

/*
* @api2_query(account, module, function, params)
*/
print $xmlapi->api2_query($account, "Cron", "remove_line", array(
"line" => "1"
));

And output :

{"cpanelresult":{"module":"Cron","data":[{"status":1,"statusmsg":"crontab installed"}],"func":"remove_line","apiversion":2,"event":{"result":1}}}

Editing a CronJob

AGAIN You need the linekey OR the line number (called commandnumber) in order to edit a line, the code is exactly the same except that there is a linekey and line params , check the Cron::listcron response for linekey, per example here :

[{"day":"1","minute":"1","hour":"1","count":1,"command_htmlsafe":"/usr/bin/php5","command":"/usr/bin/php5","weekday":"1","month":"1","linekey":"7209fe24c876a729b42a929692c62ce3"},{"count":2}]

The linekey param is 7209fe24c876a729b42a929692c62ce3 and the commandnumber is 1 (see count here : "hour":"1","count":1)

There is the code :

require_once 'xmlapi.php';

/*
* Instanciate the class, setting up username/password/IP
* @ip - cPanel server IP, if this script is on the cPanel server replace $ip by $ip = getenv('REMOTE_HOST');
* @account - string - your cPanel username
* @pass - string - your cPanel password
*/

$ip = '127.0.0.1';
$account = 'username';
$pass = "password";
$xmlapi = new xmlapi($ip, $account, $pass);

/*
* Just to be sure that XML-API will use the correct port and protocol
* @set_port(port); change port to 2082 if it isn't redirected to HTTPS and/or using HTTP protocol, else.. use 2083
* @set_protocol(protocol); change protocol to http if your sever accept HTTP else put the protocol to https
* @set_output(format); change to XML if you want the result output w/ XML, JSON if you want the result output w/ JSON
*/
$xmlapi->set_port('2083');
$xmlapi->set_protocol('https');
$xmlapi->set_output("json");
$xmlapi->set_debug(1);

/*
* @command string - The command, script, or program you wish for your cronjob to execute.
* @commandnumber int - The line of the cron entry to be edited, as reported by listcron. If this is not specified, linekey (see below) must be specified.
* @linekey int - The linekey for the entry to be edited, as reported by listcron. If this is not specified, commandnumber (see above) must be specified.
* @day int - The day on which you would like this crontab entry to run. Wildcards and any acceptable input to a crontab time expression line are allowed here.
* @hour int - The hour at which you would like this crontab entry to run. Wildcards and any acceptable input to a crontab time expression line are allowed here.
* @minute int - The minute at which you would like this crontab entry to run. Wildcards and any acceptable input to a crontab time expression line are allowed here.
* @month int - The month you would like this crontab entry to run. Wildcards and any acceptable input to a crontab time expression line are allowed here.
* @weekday int - The weekday on which you would like this crontab entry to run. Wildcards and any acceptable input to a crontab time expression line is allowed here. Acceptable values range from 0 to 6, where 0 represents Sunday and 6 represents Saturday.
*/

$command = "/usr/bin/php cron.php";
$commandnumber = "1";
$linekey = "7209fe24c876a729b42a929692c62ce3";
$day = "1";
$hour = "2";
$minute = "1";
$month = "1";
$weekday = "1";

/*
* @api2_query(account, module, function, params)
*/

print $xmlapi->api2_query($account, "Cron", "edit_line", array(
"command"=>$command,
"commandnumber"=>$commandnumber,
"linekey"=>$linekey,
"day"=>$day,
"hour"=>$hour,
"minute"=>$minute,
"month"=>$month,
"weekday"=>$weekday
));

PS : If you use linekey you can leave commandline empty and vice-versa.

I make it simple.. but there is a ton of possibilities using POST request etc..

libssh2 way

I found a way to do it with libssh2, checkout here

shell_exec way

Only disadvantage is that shared hosting or even a correct webmaster is not going to enable shell function but.. if there're enabled, you should check here a solution was given by @ajreal, but it also depends on what user crontab..

Hope this helped you!

Add a job to linux cron list programmatically with Nodejs

The command crontab which is part of Vixie Cron allows you to create, edit and delete per-user cron entries.

Or if you are running as the root user, which you should not be doing, you can drop cron files into /etc/cron.d

This is not always supported, and if you're running in a Docker type container environment it is doubtful that you have any cron at all. In that environment you'd want your running Nodejs to handle scheduled jobs for you. Or use some other kind of distributed scheduled work system.

How to create cron jobs in firebase programmatically

Instead of trying to dynamically creating scheduled function, consider having a single function that runs on a fixed schedule, and then implementing your timing logic inside of that function.

So instead of saying "I need to schedule a function that runs 3 hours from now and send a message to these 5 people", think of it as a task that you can write into a database: "at x:yz send an email to these 5 people", and then have a periodic Cloud Function that checks what tasks it needs to execute.

Also see Delay Google Cloud Function, How to use scheduler for Firebase Cloud Functions with Realtime Database/Analytics triggers?

Alternatively you can use Cloud Scheduler to create a task for the specific action you want to perform, and then have it post to Cloud Functions via PubSub.

As an even newer alternative: Use a separate scheduler service that has an API to create schedules, like Cloud Tasks. Doug wrote a great article about that in How to schedule a Cloud Function to run in the future with Cloud Tasks (to build a Firestore document TTL).

Creating Dynamically Cron jobs at particular intervals

You'll want to use the Cloud Scheduler API. Specifically, this is a REST API that lets you do everything you could do via the console or the gcloud command.

Create Magento cron job task programmatically

I do not see what is the purpose of that and there is probably a better way to do but I think it should be done like that

I never had this case but you probably can use the class Mage_Cron_Model_Schedule Mage::getModel('cron/schedule') and set data accordingly, then save. You need to define what is the cron task anyway in a config.xml for magento to be able to associate.

It should populate the table cron_schedule that it is checked for the cron tasks to be ran.



Related Topics



Leave a reply



Submit