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
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.
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 .
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.
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With