3

I have a cronjob that is set to run constantly. It's a php file that streams data from twitter. However twitter goes down every once a while or we lose connection so I have cron checking back every minute to try and run the file. The php script checks a lock file to make sure multiple copies are not run. The thing is this script will run for hours on end and I want to be able to kill it. However I can't seem to find it using commands like

ps ux

The cronjob was started with my user and I have looked to see it under root. I cannot seem to find the process running at all.

I am using

/usr/bin/php /var/www/twitter/stream.php

to execute in my cron file and I know it is running because every minute it writes a file to a directory. I can stop cron, but the process keeps going, I simply need to kill it. Not sure if this belongs here or on Stack Overflow.

Caimen
  • 133
  • 6

3 Answers3

3

To show daemons in ps you need to include the -e option (System V/POSIX) or a (BSDish). Try ps aux or ps -ef.

geekosaur
  • 7,175
  • 1
  • 20
  • 19
3

You're Doing It Wrong. (At least in my humble opinion :-)

You can do what you want the way Geekosaur or James Yale suggested, but I think you really want PHP's pcntl_alarm() function (and its relative pcntl_signal) which will allow your PHP script to take advantage of the SIGALRM facility and kill itself when things go wrong.

What you do with the alarm clock is your call (it can be as simple as "Exit if I've been running for an hour" or as complex as "Check to see if we've gotten data from twitter. If not re-initialize the connection, and if we still don't get data exit and let the script run again later" - it all depends on how complex your script is).

More questions on SIGALRM and signal handling should probably go to StackOverflow - they're (hopefully) familiar with it and can give you more guidance from a programming point of view :-)

voretaq7
  • 79,879
  • 17
  • 130
  • 214
2

Usually processes write the PID into the lockfile, that way you can find both if the process is running and what it's ID is in one place.

I'd suggest you modify your script to do that, then you can just posix_kill or similar it as required.

James Yale
  • 5,182
  • 1
  • 17
  • 20