I'm trying to execute a PHP file in background on a Linux server with a cool script found on a blog :
NAME=servicename
DESC="Daemon for my magnificent PHP CLI script"
DAEMON="/usr/bin/php"
DAEMON_OPTS="/var/www/html/phpscript.php"
test -x $DAEMON || exit 0
set -e
case "$1" in
start)
echo -n "Starting ${DESC}: "
nohup php $DAEMON_OPTS &
;;
stop)
ps faux | grep php
echo -n "Can't Stop process, use kill -9 pid"
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop}" >&2
exit 1
;;
esac
exit 0
I created the shell script into /etc/init.d/
execute
chmod +x phpscript.php
... and
update-rc.d servicename defaults
commands, but after starting the script by
/etc/init.d/servicename start
..., the service stops after a while. I don't know why.
Here, the phpscript :
[...]
function start(){
while (1) {
[...]
if (mysqli_num_rows($result) > 0) {
$this->checkAPNS();
if ($this->fp) {
while ($data = mysqli_fetch_array($result)) {
$token = $data[0];
$payload = $data[1];
$sendPush = sendPush($this->fp, $token, $payload);
if (!$sendPush) {
$retry = 0;
while (1) {
if ($this->reconnectToAPNS()) {
return;
}
if ($retry++ > 3) {
file_put_contents("push.log", "Error at ".msTimeStamp()."\n", FILE_APPEND | LOCK_EX);
sleep(10);
return;
}
}
}
[...]
}
}
usleep(500000);
}
}
}
[...]
How can i keep this process running ?