Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple cronjob same php file

Tags:

php

cron

i have multiple cronjobs that are setup as define:

0 1 * * * php -q /home/user/cron/cron1.php 20 1 * * * php -q /home/user/cron/cron2.php 40 1 * * * php -q /home/user/cron/cron3.php 0 2 * * * php -q /home/user/cron/cron4.php 

each of these cronjobs do different tasks but use the same libraries like phpmailer, pdf creator, geoip etc...

how can i combine this cronjob into one so i dont have to create 50+ files that includes the same file over and over?

thanks

like image 860
Gino Sullivan Avatar asked Nov 11 '11 03:11

Gino Sullivan


People also ask

Can 2 cron jobs run at the same time?

We can run several commands in the same cron job by separating them with a semi-colon ( ; ). If the running commands depend on each other, we can use double ampersand (&&) between them. As a result, the second command will not run if the first one fails.

What is the use of * * * * * In cron?

It is a wildcard for every part of the cron schedule expression. So * * * * * means every minute of every hour of every day of every month and every day of the week .

Can you have multiple crontabs?

FACT: you can run as many cron jobs from a single crontab file as you wish. FACT: you can also run different jobs as different users, each with their own crontab file.


1 Answers

Here's what I recommend:

0 1 * * * php -q /home/user/cron/cron.php --task=task1 20 1 * * * php -q /home/user/cron/cron.php --task=task2 40 1 * * * php -q /home/user/cron/cron.php --task=task3 #etc... 

and then in your cron.php file you do:

<?php  // include libraries  function getArguments() {   $argument = array();   for($i = 1; $i < $_SERVER['argc']; ++$i) {     if(preg_match('#--([^=]+)=(.*)#', $_SERVER['argv'][$i], $reg)) {       $argument[$reg[1]] = $reg[2];     }   }   return $argument; }  $argv = getArguments();  if($argv['task'] == 'task1') {   // do task } elseif($argv['task'] == 'task2') {   // do task } 
like image 73
Book Of Zeus Avatar answered Sep 28 '22 12:09

Book Of Zeus