Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to make a crontrolpanel for apache sites with cronjobs, a2ensite, a2dissite and force-reload

Posted on 2009-05-14
2
671 Views
Last Modified: 2013-12-26
Hi.

I have created a php script that integrates with a mysql database with all my sites in it.
The script works perfectly when i run it in console on account with access to apache with the php script.php command.
But when i add it to crontab to run every 5 minute i get an e-mail saying "command not found: a2ensite". same thing with a2dissite.

Is there anything i am doing wrong in my script?
Or do i have to learn myself cgi?

The command beeing run is in the codefield.
<?php
/**************************************\
|** Denne filen skal kjøre i crontab **|
\**************************************/
$date = date("Y-m-d H:i:s");
 
// Lage funksjoner for å forenkle kroppen på filen
function mysqlconnect(){ // Koble til MySQL
	include("config.inc.php");
	@mysql_connect($server,$username,$password) or die("DB: Access Denied");
	@mysql_select_db($database) or die("Unable to select database");
}
function mysqlrun($var){
	$result = mysql_query($var);
	return $result;
}
/* UTFÃÜR ENDRINGER */
mysqlconnect();
$db = mysqlrun("SELECT id, sitename, enabled FROM `a2_sites` WHERE cronthis = '1'");
$execforce = 0;
	while($sql = mysql_fetch_array($db)) {
	$execforce = 1;
	$sitename  = ""; //Tømme stringen
	$sitename  = $sql['sitename'];
		if($sql['enabled'] == 1) {
			mysqlrun("UPDATE `a2_sites` SET `cronthis` = '0' WHERE `id` =$sql[id] LIMIT 1;");
			exec("a2ensite $sitename");
			//echo "a2ensite $sitename\r\n";
		} else {
			mysqlrun("UPDATE `a2_sites` SET `cronthis` = '0' WHERE `id` =$sql[id] LIMIT 1;");
			exec("a2dissite $sitename");
			//echo "Croned disabled thingy<br>";
			//echo "a2dissite $sitename\r\n";
		}
	}
	if($execforce == 1) exec("/etc/init.d/apache2 force-reload");
mysql_close();
 
/* SJEKK SYSTEMET */
// Sjekke sitene som eksisterer
array(exec("ls /etc/apache2/sites-available/", $sitesavailable)); // Sider som er tilgjengelige
array(exec("ls /etc/apache2/sites-enabled/", $sitesenabled)); // Sider som er aktiverte
$num_sitesavailable = count($sitesavailable);
$num_sitesenabled = count($sitesenabled);
 
$i = 0;
while($i < $num_sitesavailable){
 
	// Sjekke om siden er aktivert
	$enabled = 0; //Resette enabled status til Ikke Aktivert
	$in = 0;
	while($in < $num_sitesenabled){
		if(!$enabled){
		if($sitesenabled[$in] == "000-default") $sitesenabled[$in] = "default"; // Fiks for lokal standard fil
			if($sitesenabled[$in] == $sitesavailable[$i])
				$enabled = 1;
		}
		$in++;
	}
 
	$sitename = $sitesavailable[$i];
 
		mysqlconnect();
		$db = mysqlrun("SELECT sitename FROM `a2_sites` WHERE sitename = '$sitename'");
		$num = mysql_num_rows($db);
			if($num == 0){ 
				$isthere = 0;
			} else { $isthere = 1; }
		if($isthere == 0){ // insert
			mysqlrun("INSERT INTO `a2_sites` ( `id` , `sitename` , `lastupdate` , `cronthis` , `enabled` , `health` ) VALUES (NULL , '$sitename', '$date', '0', '0', '0');");
			//echo "Finnes ikke i SQL, inserting: $sitename\r\n";
		} else { // update
			//echo "UPDATE `a2_sites` SET `lastupdate` = '$date', `enabled` = '$enabled', `health` = '1',  WHERE `sitename` = '$sitename' LIMIT 1;\r\n";
			mysqlrun("UPDATE `a2_sites` SET `lastupdate` = '$date', `enabled` = '$enabled', `health` = '1'  WHERE `sitename` = '$sitename' LIMIT 1;");
		}
		mysql_close();
 
	$i++;
}
 
// Check site health
mysqlconnect();
$db = mysqlrun("SELECT id, sitename FROM `a2_sites` WHERE health = '1'");
while($sql = mysql_fetch_array($db)) {
	$id			= $sql['id'];
	$sitename	= $sql['sitename'];
	$isthere = file_exists("/etc/apache2/sites-available/$sitename");
	if($isthere == 0) mysqlrun("UPDATE `a2_sites` SET `health` = '0', `enabled` = '0' WHERE `id` = '$id' LIMIT 1;");
	//echo "Isthere = $isthere | UPDATE `a2_sites` SET `health` = '0', `enabled` = '0' WHERE `id` = '$id' LIMIT 1;\r\n";
 
}
mysql_close();
 
?>

Open in new window

0
Comment
Question by:us-pata
2 Comments
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 125 total points
ID: 24387755

You will need to supply the COMPLETE path to the command, cron does NOT execute the .profile (or .bash_profile) scripts and therefore you need to supply the complete path to your command and inside your script you would need to set the environment.
;)
 
0
 
LVL 1

Author Closing Comment

by:us-pata
ID: 31581568
Thank you. it worked like a charm :)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question