2

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 ?

Xavier
  • 131
  • 2
  • 9
  • Maybe I'll get this one wrong, but why should a script run in an endlees loop? Make a cronjob and run it every X minutes: http://www.unixgeeks.org/security/newbie/unix/cron-1.html – swidmann Sep 24 '15 at 12:37
  • I can't use crontab because there's a checkAPNS() function which initializes SSL connection and a reconnectToAPNS() function which try to reconnect this connection. If I use a crontab, i will lost the $this->fp and I will have to call checkAPNS() each time the cron will run. And Apple server don't want this. – Xavier Sep 24 '15 at 12:53

1 Answers1

1

A terminating process can happen for an almost endless supply of reasons. Any unhandled exception that isn't caught or runtime error will cause your process to quit. If your mysql connection has been open awhile then you can receive the dreaded "MySQL server has gone away" error whenever you try to query your database.

If you are not careful with how you allocate resources, then after awhile you can fill up all available memory on the machine and cause PHP to issue "Allowed memory size of x bytes exhausted"

Depending on your OS, you can set PHP to output errors from the command line environment to a specific log file. It sounds like you're not quite sure exactly what the issue is, so this should be your first step. Find the php.ini file being loaded by the command line PHP then find the configuration setting for error_log and inspect the file referenced to determine why your script is terminating. You should also be able to update this via ini_set() if you prefer this specific script to log errors somewhere else.

As a fallback remedy, you can use process management software such as supervisord that runs as a service monitoring specific processes. If they quit or exit for any reason then supervisor will restart them automatically with configurable parameters.

One last point, you should be sure to include try/catch blocks wherever there can be an exception thrown. I don't see any in your abbreviated code, so they may already be there and you just didn't share. Unhandled exceptions will terminate your script, so I always include try/catch in the outermost scope at a minimum just to be sure that all exceptions are caught.

Community
  • 1
  • 1
Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96