Getting the process ID of a Yii Console Application

Hi, I am new to Yii and it’s been great so far!

I have a console application that serves as a listener for incoming xmpp messages. So I run it by


nohup php console.php Listener

If I need to restart it, I get its process ID by


ps aux | grep console.php

then kill its process ID.

The console app is supposed to run for always. However, sometimes it gets killed (by mysql error "Mysql server has gone away."). So what I would like to do is to automatically restart the console application. My idea to do this is by following the suggestion from stackoverflow (stackoverflow.com/questions/117226/how-to-check-if-a-php-script-is-still-running) by writing a shell script like below:




#!/bin/bash

while [true]; do

    if ! pidof -x console.php;

    then

        nohup php console.php Listener &

    fi

done



To test, I tried to run in the linux terminal


pidof -x console.php

, but it just returns blank.

I also tried


pidof -x console.php Listener

, and


pidof -x "console.php Listener"

and got the same blank result.

Do you have any idea on how to get the pid of the Yii console application?

Or can you suggest any other approach to automatically restart the Yii console application? Thanks!

Try this:


<?php


class MyApp extends CConsoleApplication

{

  protected $pid = null;


  public function __construct( $action, $params){{

    parent::__construct( $action, $params );

    $this->pid = posix_getpid();

    file_put_contents('/tmp/my-console-app-name.pid');

  }


  public function __destruct(){{

    unlink('/tmp/my-console-app-name.pid');

    parent::__destruct();

  }


}


?>

And kill script:


#!/bin/bash

PID=($( /bin/cat /tmp/my-console-app-name.pid ))

kill $PID