Location: PHPKode > projects > Cancerbero - The watchdog of the ports > cancerbero-0.6/cancerberod.php
#!/usr/bin/php
<?php

# do NOT run this script through a web browser
if (!isset($_SERVER["argv"][0])) {
        die("<br><strong>This script is only meant to run at the command line.</strong>");
}

if ($file = fopen( "/var/run/cancerberod.pid",  "w+")) {

        $pid = pcntl_fork();
        if ($pid == -1) {
                die("no se puede hacer fork");
        } else if ($pid) {
                fwrite($file,$pid);
                fclose($file); // hemos terminado, cerramos el apuntador
                exit(); // somos el proceso padre
        } else {
                if (!posix_setsid()) {
                        die ("No pude soltarme de la terminal");
                }
                chdir("/");
                umask(0);
        }

	$wwwroot = "/var/www/html/cancerbero";
	$push_command = "$wwwroot/push_alerts.php >> /var/log/cancerbero/cancerbero.log";

       # Declaring vars

        $host_db = "";
        $user_db = "";
        $pass_db = "";
        $name_db = "";

        while (1) {

                # Reading config from file.

                if ($file = fopen( "/etc/cancerbero/cancerbero.cfg",  "r")) {
                        while (!feof($file)) {
                                $linea = fgets($file, 255);
                                if (!(stristr($linea,'<') || stristr($linea,'>'))) {
                                        eval ($linea);
                                }
                        }
                        fclose($file); // hemos terminado, cerramos el apuntador
                }

                $status_connect = mysql_connect($host_db, $user_db, $pass_db);
                $status_select = mysql_select_db($name_db);

                if ($status_connect && $status_select) {
                        break;
                }
                sleep (10);
        }

	# Runs push_alerts.php first time.
	exec ($push_command);

        while (1) {

		# Get time check info.
		$last_running = mysql_query("SELECT * FROM program_values WHERE status_sweep = 'Push Alerts'") or die ('Querry failed: ' . mysql_error());
		$last_running_data = mysql_fetch_assoc($last_running);
		# Check number of scans done from last checking.
		$scans = mysql_query("SELECT COUNT(scan_rec_id) FROM scans WHERE ($last_running_data[date_prev] < d_scan ) ORDER BY scan_rec_id ASC") or die ('Querry failed: ' . mysql_error());
		$number_scans = mysql_result($scans, 0);

		# Run push_alerts if something is pending.
		if ($number_scans > '0') {
			exec ($push_command);
		}

		# Waits a while.
                sleep (60);
	}

} else {
        echo "Unable open /var/run/cancerberod.pid\n";
}


?>
Return current item: Cancerbero - The watchdog of the ports