HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Template for PHP service (/etc/init.d) script

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
scripttemplatephpinitserviceforetc

Problem

I wanted a template for a service script that is versatile and easily configurable.

Along with producing similar outputs to the SSH and Samba daemons, I request the code to be as clean, simple and self explanatory as possible. Hence I removed all the comments.

Sample usage:

$ service mydaemon restart
mydaemon stop/waiting
mydaemon start/running, process 30549


Sample of similar command with the SSH daemon:

$ service ssh restart
ssh stop/waiting
ssh start/running, process 30577


Here is my service.php script:

#!/usr/bin/php
> '.LOG_FILE.' 2>&1 & printf $!;');
            file_put_contents(PID_FILE, $new_pid);
            service::status();

            return service::is_running();
        }

        public static function stop()
        {
            if (!service::is_running())
            {
                writeln('stop: Unknown instance:');
                return true;
            }

            shell_exec('kill '.service::get_pid());

            for ($seconds=0; $seconds2) writeln('Too many arguments given.');
        $argv=array($argv[0], '');
    }

    switch (strtolower($argv[1]))
    {   
        case 'start':

            service::start();
            break;

        case 'stop':

            service::stop();
            break;

        case 'restart':

            service::restart();
            break;

        case 'status':

            service::status();
            break;

        default:

            if (!empty($argv[1])) writeln('Unknown option: '.$argv[1]);
            writeln('Usage: '.$argv[0].' {start|stop|restart|status}');
            break;
    }

?>


Any suggestions for improvements will be appreciated.

Solution

from stop:

if (service::is_running())
        {
            writeln('Not stopped; may still be shutting down or shutdown may have failed.');
            return false;
        }

        service::status();

        return !service::is_running();


from start:

service::status();

        return service::is_running();


Hmm, looks like a similar pattern... Can we combine this in a separate function?

What does service::status do?

public static function status()
    {
        $running=service::is_running();
        if ($running) writeln(NAME.' start/running, process '.service::get_pid()); else writeln(NAME.' stop/waiting');
        return $running;
    }


... Wait, it prints a few things and then returns whether it's running.

So why the double is_running call?

I'd just return the result of service::status in these cases. It'd be more correct too; what if a service stops between your logging and the return statement? You'd log that it's running and then return that it's ... not running. Good way to have logs showing "everything is fine" whilst things aren't doing what they're supposed to do.

Code Snippets

if (service::is_running())
        {
            writeln('Not stopped; may still be shutting down or shutdown may have failed.');
            return false;
        }

        service::status();

        return !service::is_running();
service::status();

        return service::is_running();
public static function status()
    {
        $running=service::is_running();
        if ($running) writeln(NAME.' start/running, process '.service::get_pid()); else writeln(NAME.' stop/waiting');
        return $running;
    }

Context

StackExchange Code Review Q#125129, answer score: 2

Revisions (0)

No revisions yet.