Crontab (Linux/CentOS 7) – More frequent than 1 minute

Robert AndresenUncategorized Leave a Comment

Setting up crontab

Open and edit crontab file

# crontab -e

 

Add this line (Change the path to your own)

*/1 * * * * sh /usr/bin/cronscript.sh >> /var/www/mywebpage/cron_log.log

This will open the cronscript.sh every minute.

 

Create a new shell-script file

#vi /usr/bin/cronscript.sh

The .sh script will open the cron.php every 15 secounds.

#!/bin/sh
/usr/bin/php -f /var/www/mywebpage/cron.php
sleep 15s
/usr/bin/php -f /var/www/mywebpage/cron.php
sleep 15s
/usr/bin/php -f /var/www/mywebpage/cron.php
sleep 15s

 

 

PHP cronjob manager

THIS IS NOT NEEDED TO USE CRONTAB – It’s just my own little crontab manager in PHP!
I like to add, edit and test cronjobs in my browser – and log when they was last runned. This is because I have some sync files, that could be called manually, like user-sync with AD LDAP. If new users are missing, I can at least check when the last sync was called. I can also run it manually from GUI.

SQL script

--
-- Tabellstruktur for tabell `system_cronjobs`
--

CREATE TABLE IF NOT EXISTS `system_cronjobs` (
`cron_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`title` varchar(128) NOT NULL,
`filepath` varchar(256) NOT NULL,
`interval` int(11) NOT NULL COMMENT 'interval in sec',
`run_on_day` tinyint(4) NOT NULL COMMENT 'Day (1-7) 1=monday, 7=sunday',
`run_on_time` int(4) unsigned zerofill NOT NULL COMMENT 'Time in day (ex 1600)',
`last_run` int(11) NOT NULL,
`disabled` tinyint(4) NOT NULL
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;

--
-- Dataark for tabell `system_cronjobs`
--

INSERT INTO `system_cronjobs` (`cron_id`, `user_id`, `title`, `filepath`, `interval`, `run_on_day`, `run_on_time`, `last_run`, `disabled`) VALUES
(1, 1, 'SMS: Check for new SMS', '/var/www/mywebpage/admin/modules/sms/cron/check_for_sms.php', 15, 0, 0000, 1421756852, 0);

cron.php
This script was written in just a few minutes and should NOT be used in a production enviroment without the knowledge to qualify it your self!

";
		echo "Time: " . date('Hi') . "
"; echo "
"; } $queryCronJobs = "SELECT * FROM system_cronjobs WHERE disabled='0'"; $resultCronJobs = $mysqli->query($queryCronJobs); while ($rowCronJobs = $resultCronJobs->fetch_array()) { $runThis = false; $runThisDay = false; $runThisTime = false; if ($debug) echo "Running: {$rowCronJobs['title']}
"; // Check timeinterval $timeDiff = (time() - $rowCronJobs['last_run']); if ($rowCronJobs['interval'] > 0) { if ($timeDiff > $rowCronJobs['interval']) { $runThis = true; if ($debug) echo "Run on this interval
"; } } // Check day/time elseif (!empty($rowCronJobs['run_on_day']) || !empty($rowCronJobs['run_on_time'])) { if ($debug) echo "Run on day/time. Day:{$rowCronJobs['run_on_day']}, Time:{$rowCronJobs['run_on_time']}
"; // If day matches if ($rowCronJobs['run_on_day'] > 0 && $rowCronJobs['run_on_day'] == date('N')) { $runThisDay = true; if ($debug) echo "Run on this day
"; } // If time matches if ($rowCronJobs['run_on_time'] > 0 && $rowCronJobs['run_on_time'] == date('Hi')) { $runThisTime = true; if ($debug) echo "Run on this time
"; } // Both day and time if ($rowCronJobs['run_on_day'] > 0 && $rowCronJobs['run_on_time'] > 0) { if ($runThisDay == true && $runThisTime == true) $runThis = true; if ($runThis == true && $timeDiff < (86400 + 100)) $runThis = false; // Check min interval to prevent spamming } // Only day elseif ($rowCronJobs['run_on_day'] > 0 && $rowCronJobs['run_on_time'] == 0) { if ($runThisDay == true && $runThisTime == false) $runThis = true; if ($runThis == true && $timeDiff < (86400 + 100)) $runThis = false; // Check min interval to prevent spamming } // Only time elseif ($rowCronJobs['run_on_day'] == 0 && $rowCronJobs['run_on_time'] > 0) { if ($runThisDay == false && $runThisTime == true) $runThis = true; if ($runThis == true && $timeDiff < (120)) $runThis = false; // Check min interval to prevent spamming } } // Run cronjobs if ($runThis == true) { if ($debug) echo "Run this == TRUE
"; // Update last run timestamp $queryUpdateCronJobs = "UPDATE system_cronjobs SET last_run='". time() ."' WHERE cron_id='".$rowCronJobs['cron_id']."'"; $resultUpdateCronJobs = $mysqli->query($queryUpdateCronJobs); //include(ABSPATH . $rowCronJobs['filepath']); include($rowCronJobs['filepath']); if ($debug) echo "result2: $resultUpdateCronJobs
"; if ($debug) echo "query2: $queryUpdateCronJobs
"; } else { if ($debug) echo "Run this == FALSE
"; } if ($debug) echo "
"; } ?>