Heim php教程 PHP源码 PHP-Daemon mit Start/Stopp/Neustart/Stat-Funktion

PHP-Daemon mit Start/Stopp/Neustart/Stat-Funktion

May 19, 2018 pm 01:49 PM

Springe zu                                                                                                   [Vollbildvorschau]

pidfile = __DIR__ . '/' . self::PIDNAME . '.pid';
    }
    /**
     * @return int
     */
    private function daemon() {
        if (file_exists($this->pidfile)) {
            echo "The file $this->pidfile exists.\n";
            exit();
        }
        $pid = pcntl_fork();
        if ($pid == -1) {
            die('could not fork');
        } else if ($pid) {
            // we are the parent
            exit($pid);
        } else {
            // we are the child
            file_put_contents($this->pidfile, getmypid());
            posix_setuid(self::uid);
            posix_setgid(self::gid);
            cli_set_process_title(self::PROCESS_NAME);
            pcntl_signal(SIGHUP, [$this, 'signoH']);
            pcntl_signal(SIGTERM, [$this, 'signoH']);
            pcntl_signal(SIGCHLD, [$this, 'signoH']);
            pcntl_signal(SIGQUIT, [$this, 'signoH']);
            pcntl_signal(SIGINT, [$this, 'signoH']);
            pcntl_signal(SIGUSR1, [$this, 'signoH']);
            return (getmypid());
        }
    }
    /**
     *
     */
    private function run() {
        do {
            pcntl_signal_dispatch();
            if ($this->stop) {
                break;
            }
            echo "I am alive" . mt_rand(0,20) . "...\n";
            sleep(5);
        } while (true);
        echo ("进程退出\n");
    }
    public function restart() {
        $this->stop();
        $this->start();
        print "重启成功!\n";
    }
    /**
     *
     */
    private function start() {
        $pid = $this->daemon();
        $this->run();
    }
    /**
     *
     */
    private function stop() {
        if (file_exists($this->pidfile)) {
            $pid = file_get_contents($this->pidfile);
            posix_kill($pid, SIGKILL);
            unlink($this->pidfile);
        }
    }
    /**
     * @param $proc
     */
    private function help($proc) {
        printf("%s start | stop | restart | stat | help \n", $proc);
    }
    /**
     * @param $argv
     */
    public function main($argv) {
        if (count($argv) < 2) {
            printf("please input help parameter\n");
            exit();
        }
        if ($argv[1] === &#39;stop&#39;) {
            $this->stop();
        } else if ($argv[1] === &#39;start&#39;) {
            $this->start();
        } else if ($argv[1] === &#39;restart&#39;) {
            $this->restart();
        } else if ($argv[1] === &#39;stat&#39;) {
            if (is_file($this->pidfile)) {
                posix_kill(file_get_contents($this->pidfile), SIGHUP);
            } else {
                print "\n_______程序没有启动________\n";
            }
        } else {
            $this->help("command list :");
        }
    }
    /**
     * @param $instance
     * @param $channelName
     * @param $message
     */
    public function handle($instance, $channelName, $message) {
        file_put_contents(__DIR__ . "/$channelName.txt", $message . "\n", FILE_APPEND);
    }
    /**
     * @param $signo
     */
    public function signoH($signo) {
        switch ($signo) {
            case SIGHUP :
                print "\n___________运行状态___________\n";
                print "HOST :" . self::HOST . "\n";
                print "PORT :" . self::PORT . "\n";
                print "NAME : " . self::PROCESS_NAME . "\n";
                print "PID : " . file_get_contents($this->pidfile) . "\n";
                print "________________________________\n";
                break;
            case SIGTERM:
                posix_kill(file_get_contents($this->pidfile), 9);
                break;
            default :
                print "\n________________________________\n";
                print "呀!~有人想杀掉我!\n";
                print "________________________________\n";
        }
    }
}
Nach dem Login kopieren

Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
2 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
Repo: Wie man Teamkollegen wiederbelebt
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Abenteuer: Wie man riesige Samen bekommt
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)