Solved

Php script to KILL cron job

Posted on 2009-04-05
16
1,001 Views
Last Modified: 2013-12-12
Since I am unable to run terminal commands, how would I create another php script to stop a runaway cron job. I have already gotten the runaway to output:

Current user : flyxxxx
PID : 14379
Script owner's user ID : 33025
Script owner's group ID : 33027

This is running on a publicly hosted Linux server.

Yes, I know a possible answer is to write better code in the first place!! But those infinite loops are so fun to create.

Thanks,
David

0
Comment
Question by:dolan2go
[X]
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
  • 6
  • 4
  • 2
  • +3
16 Comments
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 24071708
Depending on what the loop is doing (accessing a database, including other scripts, etc), you could try some temporary renaming to cause an error.

If you have control of system options, you could set the maximum script run time to a small number.
0
 
LVL 1

Author Comment

by:dolan2go
ID: 24071731
yodercm,

The script is executing a do-while loop where a preg_match doesn't return correct results. Yes, I now have a correct stopping if statement in the loop.

I'd like to learn how to Kill a cron job using the info I provided.

Thanks for your reply,
David
0
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 24071814
I don't see how one php script could stop another that is already running.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 1

Author Comment

by:dolan2go
ID: 24071828
I thought it must be possible to give the cron daemon instructions to execute on another process (PID) especially if it is from the same user.

I am looking into your suggestion about setting a script runtime limit. That may be the answer. It's dependent on safe mode, so I'll check.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24072623
A PHP Cron job should obey the time_limit() functions.  You can ask the server administrators to kill it for you, but it is unlikely that you can kill it yourself.  If you add a little code to look for an incremented counter or something like that, you can stop after a certain number of iterations.
0
 
LVL 13

Assisted Solution

by:WizRd-Linux
WizRd-Linux earned 150 total points
ID: 24073238
Because PHP is usually run by the user apache you would have to either use the kill command with http://au2.php.net/shell_exec (Doesn't work if safe_mode is enabled) if the cron job is already executed by the apache user.  Or if the cronjob is running as someone else you will need to configure sudo to allow apache to run sudo kill without a password, but a warning, this can open up a massive can of worms and should only be used as a desperate last resort as every webpage can call sudo kill, even sudo kill -9 1 will work.
0
 
LVL 1

Author Comment

by:dolan2go
ID: 24077369
WizRd-Linux,

Thanks for your reply. That looks promising, yet dangerous. When I spoke to the hosting company (they were very vague), they asked about the process id. Do you think it's possible to run a php script with the shell_exec ( ) command, and what wouldn't there have to be a reference to the PID and other details in that sudo kill ?

I only want to kill one 'runaway' script that's been executed via cron daemon from my domain.

?? Still looking for the answer.
0
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 24077932
If you have contact with the hosting company, just give them whatever you can and tell them to kill it.  They don't want a permanently running script any more than you do.  

If you don't know the process id, ask them how you find it.

Then put time limits into any script that might be a problem.  Frankly, I can't imagine any hosting company that doesn't have built-in time limits on all jobs.
0
 
LVL 7

Accepted Solution

by:
Morne Lategan earned 350 total points
ID: 24080162
Remove the runaway script from cron, so that it won't start again.

Then insert another script into cron which kills the first one. If you put the killer into cron as well, it should be able to kill the first one becaus it runs as the same user. A bash script would be:

#!/bin/bash
kill -9 14379

A php script would be

<?php
   shell_exec("kill -9 14379");
?>
Hopefully the ISP won't restrict you on doing this.
0
 
LVL 13

Expert Comment

by:WizRd-Linux
ID: 24082205
If it is a run away script you can get the pid of the process running that particular script using" lynx --dump http://localhost/server-status"

In the apache configuration you need to make sure that the "ExtendedStatus On" directive exists or you won't get the correct output.

You can then cut out the line and use the second field which is the PID of the apache process running the script.  An example output looks like:

0-3 19204 0/135/236 _ 21.17 45 0 0.0 0.48 0.65 219.90.162.252
   www.domain.com GET /screen.php HTTP/1.1

The 19204 number on the first line is the PID of the apache process.

0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24093386
the best solution is to replace your croned file that does nothing...

<?php

?>
0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24093391
replace croned file with a blank php and let it run as normal without any effect
<?php
 
?>

Open in new window

0
 
LVL 1

Author Comment

by:dolan2go
ID: 24149480
Thanks to WizRd-Linux & Uberpappa for your suggestions.

The php shell_exec ( ) did work for me on a public server. This is a two step process made easy by the runaway script 'writing' output to a fopen ( ) file when the html output will never happen. So, with the first line of the output file being the output in my question, ( I only need the PID ), and using a second file /end_proc.php?pid=12345, I can quickly and easily kill a runaway program with another program.

The keys to this is being able to :
Write the php function 'getmypid ( )' to a file (for PID viewing) and,
Use shell_exec ( ) to stop it.

Not being familiar with bash, I did not test that method.
<?php
 
if ( @isset ( $_GET['pid'] ) )
{
	$pid = $_GET['pid'];
}
 
$output = shell_exec ( "kill -9 $pid" );
 
echo "<pre>$output</pre>";
 
?>

Open in new window

0
 
LVL 1

Author Closing Comment

by:dolan2go
ID: 31566774
WizRd-Linux, thanks for your suggestion to use shell_exec ( ). Uberpappa, your suggestion really made the solution possible.
0
 
LVL 1

Author Comment

by:dolan2go
ID: 24149582
I forgot to include two thoughts:

I think safe_mode IS enabled on this server, but it seemed to kill the runaway anyway. Maybe because they are both from the SAME user.

Also, the $output echoed to the screen returned absolutely nothing. I had no idea that the script had been killed until looking at the file ( $fp ) being written to.
0
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 24149611
Well, you're welcome for my help as well, even if you were rude enough not to acknowledge it.
0

Featured Post

Don't Miss ATEN at InfoComm 2017!

Visit booth #2167 to see the  new ATEN VM3200 32 x 32 Modular Matrix Switch. Other highlights include the VE8950 4K HDMI Over IP Extender, VS1912 12-Port DP Video Wall Media Player  and VK2100 ATEN Control System. Register now with Free Pass Code ATEN288!

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

751 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