php cronjob中断

时间:2011-11-15 13:22:34

标签: php cron

我在php中有一个cronjob计算一些业务规则(例如:净转速,总转速,估计转速等......使用标准差和其他数学算法)

这个cronjob使用exec调用多个cron调用3个PHP脚本

对于每次调用我在后台启动一个进程并告诉系统x已启动。例如:

这是我的逻辑

start 
 main cron start - message
  cron sub 1 start - message
  run cron sub 1
  cron sub 1 end - message

  wait until cron sub 1 stop

  process stuff  

  cron sub 2 start - message
  run background cron sub 2 // this will automaticaly send a message when this jobs end

  cron sub 3 start - message
  run background cron sub 3 // this will automaticaly send a message when this jobs end

 main cron end - message

end

我需要解决的是这个

如果有人手动运行作业并取消它(ctrl + c)或 在cron中发生了一些不好的事情(致命错误或无法连接到db) 或其他任何事情(如没有足够的记忆)

我想停止cronjob并告诉它发生了什么以及什么时候停止所以我可以回到它原来的位置。

有可能吗?感谢

2 个答案:

答案 0 :(得分:34)

这是一个你必须尝试/调试的简单代码:

<?php
ignore_user_abort(false);

function shutdown() {
  echo "shutdown function\n";
}
register_shutdown_function('shutdown');

class shutdown {
  function __construct() {
    echo "shutdown::construct\n";
  }
  function __destruct() {
    echo "shutdown::destruct\n";
  }
}

$s = new shutdown();

declare(ticks = 1);

function sig_handler($signo) {

  switch ($signo) {
    case SIGTERM:
      echo "SIG: handle shutdown tasks\n";
      break;
    case SIGHUP:
      echo "SIG: handle restart tasks\n";
      break;
    case SIGUSR1:
      echo "SIG: Caught SIGUSR1\n";
      break;
    case SIGINT:
      echo "SIG: Caught CTRL+C\n";
      break;
    default:
      echo "SIG: handle all other signals\n";
      break;
  }
  return;
}

echo "Installing signal handler...\n";

pcntl_signal(SIGTERM, "sig_handler");
pcntl_signal(SIGHUP,  "sig_handler");
pcntl_signal(SIGUSR1, "sig_handler");
pcntl_signal(SIGINT, "sig_handler");

echo "Generating signal SIGTERM to self...\n";

# killing the script using bad memory allocation
/*
ini_set('memory_limit', '1K');
$data = '';
for($i = 0; $i < 1000; $i++) {
  $data .= str_repeat($i . time(), time());
}
*/

# simulating CTRL+C
// for($i = 0; $i < 100000000; $i++) {  } // don't forget to press CTRL+C

echo "Done\n";

如果cronjob正常运行,你会得到:

shutdown::construct
Installing signal handler...
Generating signal SIGTERM to self...
Done
shutdown function
shutdown::destruct

如果您收到致命错误:

shutdown::construct
Installing signal handler...
Generating signal SIGTERM to self...
PHP Fatal error:  Possible integer overflow in memory allocation (11 * 1321404273 + 1) in /var/www/domain.com/php.php on line 51
shutdown function

如果你杀了这个过程:

kill <processid>

shutdown::construct
Installing signal handler...
Generating signal SIGTERM to self...
SIG: handle shutdown tasks
Done
shutdown function
shutdown::destruct

您也可以在每种情况下使用posix_kill(posix_getpid(), <signal you like>);

阅读更多内容:

http://php.net/manual/en/function.pcntl-signal.php

http://en.wikipedia.org/wiki/Signal_%28computing%29

http://users.actcom.co.il/~choo/lupg/tutorials/signals/signals-programming.html

答案 1 :(得分:0)

如果这是一个bash脚本,你可以像这样捕获ctrl + c信号:

is_borked()
{
    echo "Somebody set up us the bomb"
    exit
}

trap is_borked SIGINT

如果你需要进行某种清理,你应该跟踪(通过变量吗?)这一步发生了什么。