Vixushr Posted August 8, 2011 Share Posted August 8, 2011 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. Thanx! Quote Link to comment https://forums.phpfreaks.com/topic/244238-help-some-sort-of-php-cron-task-kill-script-needed/ Share on other sites More sharing options...
WebStyles Posted August 8, 2011 Share Posted August 8, 2011 you could have your cron job to start by reading a variable in a .txt file, and use php to change that variable... a simple 1 or 0 will do. if contents of file = 1, run cron job, if not, don't run it. Quote Link to comment https://forums.phpfreaks.com/topic/244238-help-some-sort-of-php-cron-task-kill-script-needed/#findComment-1254449 Share on other sites More sharing options...
Vixushr Posted August 8, 2011 Author Share Posted August 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/244238-help-some-sort-of-php-cron-task-kill-script-needed/#findComment-1254485 Share on other sites More sharing options...
WebStyles Posted August 8, 2011 Share Posted August 8, 2011 then you have some reading to do... a .txt file with a 1 or a 0 inside it. folow these links to find out how to read the contents and how to change them: file_get_contents file_put_contents Quote Link to comment https://forums.phpfreaks.com/topic/244238-help-some-sort-of-php-cron-task-kill-script-needed/#findComment-1254486 Share on other sites More sharing options...
Vixushr Posted August 8, 2011 Author Share Posted August 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/244238-help-some-sort-of-php-cron-task-kill-script-needed/#findComment-1254517 Share on other sites More sharing options...
WebStyles Posted August 8, 2011 Share Posted August 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/244238-help-some-sort-of-php-cron-task-kill-script-needed/#findComment-1254530 Share on other sites More sharing options...
Vixushr Posted August 8, 2011 Author Share Posted August 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/244238-help-some-sort-of-php-cron-task-kill-script-needed/#findComment-1254534 Share on other sites More sharing options...
WebStyles Posted August 8, 2011 Share Posted August 8, 2011 what is the script? do you have access to it? is it php, perl or python? it shouldn't be too hard to figure out. all you need to add to it is a line to read the text file and an if statement. Quote Link to comment https://forums.phpfreaks.com/topic/244238-help-some-sort-of-php-cron-task-kill-script-needed/#findComment-1254541 Share on other sites More sharing options...
Vixushr Posted August 8, 2011 Author Share Posted August 8, 2011 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>"; } } } } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/244238-help-some-sort-of-php-cron-task-kill-script-needed/#findComment-1254542 Share on other sites More sharing options...
Vixushr Posted August 8, 2011 Author Share Posted August 8, 2011 those xxxx_download tasks tend to get hanging. Quote Link to comment https://forums.phpfreaks.com/topic/244238-help-some-sort-of-php-cron-task-kill-script-needed/#findComment-1254544 Share on other sites More sharing options...
WebStyles Posted August 8, 2011 Share Posted August 8, 2011 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) Quote Link to comment https://forums.phpfreaks.com/topic/244238-help-some-sort-of-php-cron-task-kill-script-needed/#findComment-1254548 Share on other sites More sharing options...
WebStyles Posted August 8, 2011 Share Posted August 8, 2011 man, that script is weird... are you sure it's a cron job? it expects a url variable... also, why is this repeated 7 times? if($_GET['action']=='xxxx' || ($argv[1] == 'download' && $argv[2]=='xxxx')){ xxxx_download(); } Quote Link to comment https://forums.phpfreaks.com/topic/244238-help-some-sort-of-php-cron-task-kill-script-needed/#findComment-1254553 Share on other sites More sharing options...
jcbones Posted August 8, 2011 Share Posted August 8, 2011 I thought he just xxxx'd out a bunch of stuff he didn't want people to see. Quote Link to comment https://forums.phpfreaks.com/topic/244238-help-some-sort-of-php-cron-task-kill-script-needed/#findComment-1254560 Share on other sites More sharing options...
WebStyles Posted August 8, 2011 Share Posted August 8, 2011 @ jcbones yeah, at first I thought that too, then I figured it's better to ask than assume. Quote Link to comment https://forums.phpfreaks.com/topic/244238-help-some-sort-of-php-cron-task-kill-script-needed/#findComment-1254562 Share on other sites More sharing options...
Vixushr Posted August 8, 2011 Author Share Posted August 8, 2011 Yes you are right, i replaced relevant data that is not for public with XXXX. Quote Link to comment https://forums.phpfreaks.com/topic/244238-help-some-sort-of-php-cron-task-kill-script-needed/#findComment-1254579 Share on other sites More sharing options...
WebStyles Posted August 8, 2011 Share Posted August 8, 2011 cool, then the code makes a bit more sense! have you created the text file? post it's name and complete path please. (also, check the file's permissions, you need to allow user www to write and also the user that executes the cron job) Quote Link to comment https://forums.phpfreaks.com/topic/244238-help-some-sort-of-php-cron-task-kill-script-needed/#findComment-1254583 Share on other sites More sharing options...
Vixushr Posted August 8, 2011 Author Share Posted August 8, 2011 in the mean time, create the text file and figure out the complete path OK, Iw done that. Quote Link to comment https://forums.phpfreaks.com/topic/244238-help-some-sort-of-php-cron-task-kill-script-needed/#findComment-1254589 Share on other sites More sharing options...
WebStyles Posted August 8, 2011 Share Posted August 8, 2011 what's the file's name and complete path? (did you set the proper permissions?) Quote Link to comment https://forums.phpfreaks.com/topic/244238-help-some-sort-of-php-cron-task-kill-script-needed/#findComment-1254590 Share on other sites More sharing options...
Vixushr Posted August 8, 2011 Author Share Posted August 8, 2011 i kept that cronControl.txt name, i'w set proper path and set 777 path: /home/vixushr/supplierinfo iw copied path from existing cron: /usr/bin/php -c /home/vixushr /home/vixushr/supplierinfo Quote Link to comment https://forums.phpfreaks.com/topic/244238-help-some-sort-of-php-cron-task-kill-script-needed/#findComment-1254592 Share on other sites More sharing options...
WebStyles Posted August 8, 2011 Share Posted August 8, 2011 man, what is the path? (c'mon! it's like the third time I've asked) I'm trying to help you, but you're making it painfully slow.. (it's 10 to midnight here, I'll be going to bed soon, so hurry up man!) Quote Link to comment https://forums.phpfreaks.com/topic/244238-help-some-sort-of-php-cron-task-kill-script-needed/#findComment-1254594 Share on other sites More sharing options...
Vixushr Posted August 8, 2011 Author Share Posted August 8, 2011 path: /home/vixushr/supplierinfo iw copied path from existing cron: /usr/bin/php -c /home/vixushr /home/vixushr/supplierinfo/request.php Quote Link to comment https://forums.phpfreaks.com/topic/244238-help-some-sort-of-php-cron-task-kill-script-needed/#findComment-1254595 Share on other sites More sharing options...
WebStyles Posted August 8, 2011 Share Posted August 8, 2011 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'); Quote Link to comment https://forums.phpfreaks.com/topic/244238-help-some-sort-of-php-cron-task-kill-script-needed/#findComment-1254599 Share on other sites More sharing options...
Vixushr Posted August 8, 2011 Author Share Posted August 8, 2011 OK, Done. Quote Link to comment https://forums.phpfreaks.com/topic/244238-help-some-sort-of-php-cron-task-kill-script-needed/#findComment-1254602 Share on other sites More sharing options...
WebStyles Posted August 8, 2011 Share Posted August 8, 2011 well, that's it... (when the value in the text file is 0, the cron job won't execute, when the value is 1, it will. you can use the 2 last scripts to enable and disable the cron job whenever needed.) Quote Link to comment https://forums.phpfreaks.com/topic/244238-help-some-sort-of-php-cron-task-kill-script-needed/#findComment-1254603 Share on other sites More sharing options...
Vixushr Posted August 8, 2011 Author Share Posted August 8, 2011 hm... Let me just get clear on one thing... i don't want to stop cron from executing the script. I want to terminate tasks that are remined active after they are executed. Will this do it? Quote Link to comment https://forums.phpfreaks.com/topic/244238-help-some-sort-of-php-cron-task-kill-script-needed/#findComment-1254604 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.