0

i have a simple php worker script which is running in the background.

i use a monitor script to make sure that this worker script is indeed running.

the monitor script is running on crontab.

i would like restart the worker process every now and then, using linux kill.

however this create a problem, which is that the worker script might lose the data which is currently handling, if stopped abruptly.

in .net for example, with winforms, u can simply implement on_form_close, how do i accomplish the same with php (linux,ubuntu)?

thanks

yaron
  • 1,283
  • 1
  • 11
  • 21

1 Answers1

1

PHP's register_shutdown_function allows you to register functions to run just before the script terminates:

register_shutdown_function(function () {
   // ...
});

Since you are using signals to kill the process, you should instead install a signal handler:

pcntl_signal(SIGQUIT, function () {
    // ...
});

See pcntl_signal

Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
  • ahh, great , just to make sure (i will test it later) will register_shutdown_function , will be called on kill? – yaron May 23 '13 at 17:39
  • mmm, yeah, register shutdown, does not work, when kill is issued. – yaron May 24 '13 at 11:59
  • hence the second part of the answer :) – Arnaud Le Blanc May 24 '13 at 12:02
  • si senior! indeed its a valid solution, however i read about pcntl that it inefficiant, espically the tick part. i ended up implementing a msg system between the procs via php inner queing system. thanks fo r your help! – yaron May 24 '13 at 14:03