Solved

Php script to KILL cron job

Posted on 2009-04-05
16
984 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
  • 6
  • 4
  • 2
  • +3
16 Comments
 
LVL 27

Expert Comment

by:yodercm
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:yodercm
ID: 24071814
I don't see how one php script could stop another that is already running.
0
 
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 108

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:yodercm
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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:yodercm
ID: 24149611
Well, you're welcome for my help as well, even if you were rude enough not to acknowledge it.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now