Jump to content

HELP! Some sort of PHP cron task kill script needed ! ! !


Vixushr

Recommended Posts

Helo dear people.

 

I have a following problem, i got script that calls xml data from remote servers and it is set on cron job in cpanel.

It works ok... but from time to time job's tend to remain active and when i reach my limit (20 tasks) entire site is shut by my host.

 

Is there a way to create php script that can be called by cron job (after my script) to close / kill all active tasks.  :shrug:

 

Thanx!

 

 

Link to comment
Share on other sites

If i unedrstaind you correctly i will have a case that cron wont run my job becouse it is allready in active state.

But i need my cron to run jobs as it shud but i need to kill all remaiming tasks that are for some reason still active.

 

Thanx.

Link to comment
Share on other sites

Iw found some solution in  pearl... can someone help me translate it to php?

 

Say your cronjob spawns a process with the commandline "/usr/sbin/larch.pl /etc/trees.conf", then you could add a cronjob like this:

 

Code:

#!/bin/sh

# Sleep for 30 seconds.

sleep 30s

# Find process by commandline and -KILL it.

pkill -9 -f "/usr/sbin/larch.pl /etc/trees.conf"

# Exit the shellscript the right way.

exit 0

 

It should be scheduled to run at the same interval as your other cronjob.

 

Link to comment
Share on other sites

you need to understand something: Cron jobs are scheduled scripts that will run at selected times. You can only terminate a job if it is running at the time you run your kill script... if the cron job only takes about half a second to execute, chances are you're never going to be able to kill it that way. killing an active job is different from disabling a cron job. If you disable a cron, you will have to re-enable it at some point.

 

On the other hand, if you have a cron job that you sometimes do not want to execute, then it's the wrong script for the job. My advice: think things over before trying to put tape over them.

Link to comment
Share on other sites

My advice: think things over before trying to put tape over them.

 

I agree, but in this case i am stuck with "tape" becouse i am not the one that made the script and one that can make changes to it but i am looking for a solution within my options and possibilities.... wich is pretty limited in this case.  :(

Link to comment
Share on other sites

it is PHP script:

 

<?php
set_time_limit(0);
include('curl.php');

echo "<pre>";

$products = test_getxmlinfo();
//print_r($products);

if(empty($_GET['action']) && $argc != 3) 
die('wrong args');

if($_GET['action']=='price' || ($argv[1] == 'action' && $argv[2]=='price')){
    getxml();
    updatePrice($products);
}

if($_GET['action']=='state' || ($argv[1] == 'action' && $argv[2]=='state')){
    getxml();
    updateState($products);
}

if($_GET['action']=='xxxx' || ($argv[1] == 'download' && $argv[2]=='xxxx')){
    xxxx_download();
}

if($_GET['action']=='xxxx' || ($argv[1] == 'download' && $argv[2]=='xxxx')){
    xxxx_download();
}

if($_GET['action']=='xxxx' || ($argv[1] == 'download' && $argv[2]=='xxxx')){
    xxxx_download();
}

if($_GET['action']=='xxxx' || ($argv[1] == 'download' && $argv[2]=='xxxx')){
    xxxx_download();
}

if($_GET['action']=='xxxx' || ($argv[1] == 'download' && $argv[2]=='xxxx')){
    xxxx_download();
}

if($_GET['action']=='xxxx' || ($argv[1] == 'download' && $argv[2]=='xxxx')){
    xxxx_download();
}

if($_GET['action']=='xxxx' || ($argv[1] == 'download' && $argv[2]=='xxxx')){
    xxxx_download();
}

function updatePrice($products) {
    foreach($products as $key=>$value) {
        if(updateProductPrice($value)) {
		echo "<pre>";
            print_r($key);
            echo "success";
            echo "<pre>";
        }
        else {
		echo "<pre>";
            print_r($key);
            echo "not-change";
            echo "<pre>";
        }
    }
}

function updateState($products) {
    foreach($products as $key =>$value) {
        //updateProductState($value);
        $id = $key;
        $id_supplier = getAllProducts($id);
        if($id_supplier) {
            if($id_supplier[0]['id_supplier']==89) {
                $supplier = "xxxx";
            }
            else if($id_supplier[0]['id_supplier']==3) {
                $supplier = "xxxx";
            }
            else if($id_supplier[0]['id_supplier']==4) {
                $supplier = "xxxx";
            }
            else if($id_supplier[0]['id_supplier']==90) {
                $supplier = "xxxx";
            }
            else if($id_supplier[0]['id_supplier']==91) {
                $supplier = "xxxx";
            }
            else if($id_supplier[0]['id_supplier']==92) {
                $supplier = "xxxx";
            }
            else if($id_supplier[0]['id_supplier']==169) {
                $supplier = "xxxx";
            }
            else if($id_supplier[0]['id_supplier']==167) {
                $supplier = "xxxx";
            }
            else {
                $supplier = "";
            }
            if($supplier=="") {
                echo "<pre>";
                print_r($key);
                echo "not-change";
                echo "<pre>";
            }
            else {
                if(checkQTY($products[$id][$supplier],$supplier,$id)) {
                    echo "<pre>";
                    print_r($key);
                    echo "success";
                    echo "<pre>";
                }
                else {
                    //updatePrice($products);
                    if(updateProductPrice($value)) {
                        echo "<pre>";
                        print_r($key);
                        echo "success";
                        echo "<pre>";
                    }
                    else {
                        echo "<pre>";
                        print_r($key);
                        echo "not-change";
                        echo "<pre>";
                    }
                }
            }

        }

    }
}
?>

Link to comment
Share on other sites

ok. this should be easy enough...

 

the only thing you need is to create a .txt file somethere on the server, and figure out the complete path to it...

 

lets call it cronControl.txt, and let's imagine it's in /usr/local/home/ (just for arguments sake, you'll need to replace these variables.

 

add this to your cron script, at the start:

 

<?php
$controlFileName = 'cronControl.txt';
$controlFilePath = '/usr/local/home/';
$controlValue = file_get_contents($controlFilePath.$controlFileName);
if(trim($controlValue) == 0){
    exit();
}
// ... rest of code follows
?>

this will basically read the text file, and if the value inside is 0, it will force the script to abort. Make sense?

 

(I'll be right back to explain the rest, in the mean time, create the text file and figure out the complete path)

Link to comment
Share on other sites

ok, so you'll need to put this at the top of the file, to prevent it from executing if the value in the file is not = 1:

 

$controlFileName = 'cronControl.txt';
$controlFilePath = '/home/vixushr/supplierinfo/';
$controlValue = file_get_contents($controlFilePath.$controlFileName);
if(trim($controlValue) != 1){
    exit();
}

 

and now you need to create two scripts, I'll call them allowCron.php and notAllowCron.php, you can put this code into functions and call from other scripts, or include these files in other scripts, whatever you want. One will set the file contents to 0, and the other will set it to 1:

 

allowCron.php:

$controlFileName = 'cronControl.txt';
$controlFilePath = '/home/vixushr/supplierinfo/';
file_put_contents($controlFilePath.$controlFileName,'1');

 

notAllowCron.php:

$controlFileName = 'cronControl.txt';
$controlFilePath = '/home/vixushr/supplierinfo/';
file_put_contents($controlFilePath.$controlFileName,'0');

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.