diff --git a/modules/metarefresh/hooks/hook_cron.php b/modules/metarefresh/hooks/hook_cron.php
new file mode 100644
index 0000000000000000000000000000000000000000..4065ff43c55473d9c61c549efd84a68674cd63a7
--- /dev/null
+++ b/modules/metarefresh/hooks/hook_cron.php
@@ -0,0 +1,43 @@
+<?php
+/**
+ * Hook to run a cron job.
+ *
+ * @param array &$croninfo  Output
+ */
+function metarefresh_hook_cron(&$croninfo) {
+	assert('is_array($croninfo)');
+	assert('array_key_exists("summary", $croninfo)');
+	assert('array_key_exists("tag", $croninfo)');
+
+	SimpleSAML_Logger::info('cron [metarefresh]: Running cron in cron tag [' . $croninfo['tag'] . '] ');
+
+	try {
+		$config = SimpleSAML_Configuration::getInstance();
+		$mconfig = $config->copyFromBase('mconfig', 'config-metarefresh.php');
+		
+		$sets = $mconfig->getValue('sets');
+		if (count($sets) < 1) return; 
+	
+		foreach ($sets AS $setkey => $set) {
+			// Only process sets where cron matches the current cron tag.
+			if (!in_array($croninfo['tag'], $set['cron'])) continue;
+	
+			SimpleSAML_Logger::info('cron [metarefresh]: Executing set [' . $setkey . ']');
+	
+			$maxcache = NULL; if (array_key_exists('maxcache', $set)) $maxcache = $set['maxcache'];
+			$maxduration = NULL; if (array_key_exists('maxduration', $set)) $maxcache = $set['maxduration'];
+			$metaloader = new sspmod_metarefresh_MetaLoader($maxcache, $maxduration);		
+			
+			foreach($set['sources'] AS $source) {
+				SimpleSAML_Logger::debug('cron [metarefresh]: In set [' . $setkey . '] loading source ['  . $source['src'] . ']');
+				$metaloader->loadSource($source);
+			}
+			$metaloader->writeMetadataFiles($config->resolvePath($set['outputDir']));
+		}
+	
+	} catch (Exception $e) {
+		$croninfo['summary'][] = 'Error during metarefresh: ' . $e->getMessage();
+	}
+	
+}
+?>
\ No newline at end of file
diff --git a/modules/metarefresh/lib/MetaLoader.php b/modules/metarefresh/lib/MetaLoader.php
index 8d7b67df79b9a6111c56c2627c55941922d59ce4..4bc0947a8ef8bd38d6ce962a011e40d49dfbdf49 100644
--- a/modules/metarefresh/lib/MetaLoader.php
+++ b/modules/metarefresh/lib/MetaLoader.php
@@ -30,21 +30,21 @@ class sspmod_metarefresh_MetaLoader {
 	 *
 	 * @param $src  Filename of the metadata file.
 	 */
-	public function loadSource($src, $validateFingerprint = NULL, $template = NULL) {
+	public function loadSource($source, $validateFingerprint = NULL, $template = NULL) {
 
-		$entities = SimpleSAML_Metadata_SAMLParser::parseDescriptorsFile($src);
+		$entities = SimpleSAML_Metadata_SAMLParser::parseDescriptorsFile($source['src']);
 	
 		foreach($entities as $entity) {
 			if($validateFingerprint !== NULL) {
 				if(!$entity->validateFingerprint($validateFingerprint)) {
-					echo('Skipping "' . $entity->getEntityId() . '" - could not verify signature.' . "\n");
+					SimpleSAML_Logger::info('Skipping "' . $entity->getEntityId() . '" - could not verify signature.' . "\n");
 					continue;
 				}
 			}
 	
 			if($ca !== NULL) {
 				if(!$entity->validateCA($ca)) {
-					echo('Skipping "' . $entity->getEntityId() . '" - could not verify certificate.' . "\n");
+					SimpleSAML_Logger::info('Skipping "' . $entity->getEntityId() . '" - could not verify certificate.' . "\n");
 					continue;
 				}
 			}
@@ -122,7 +122,7 @@ class sspmod_metarefresh_MetaLoader {
 		}
 	
 		if(!file_exists($outputDir)) {
-			echo('Creating directory: ' . $outputDir . "\n");
+			SimpleSAML_Logger::info('Creating directory: ' . $outputDir . "\n");
 			mkdir($outputDir, 0777, TRUE);
 		}
 	
@@ -130,11 +130,11 @@ class sspmod_metarefresh_MetaLoader {
 	
 			$filename = $outputDir . '/' . $category . '.php';
 	
-			echo('Writing: ' . $filename . "\n");
+			SimpleSAML_Logger::debug('Writing: ' . $filename . "\n");
 	
-			$fh = fopen($filename, 'w');
+			$fh = @fopen($filename, 'w');
 			if($fh === FALSE) {
-				echo('Failed to open file for writing: ' . $filename . "\n");
+				throw new Exception('Failed to open file for writing: ' . $filename . "\n");
 				exit(1);
 			}