PHP code example of abdulbasset / nssm-php

1. Go to this page and download the library: Download abdulbasset/nssm-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

abdulbasset / nssm-php example snippets


use Abdulbasset\NssmPhp\Nssm;

$nssm = new Nssm('MyService');

// Start the service
$nssm->start();

// Stop the service
$nssm->stop();

// Restart the service
$nssm->restart();

use Abdulbasset\NssmPhp\Nssm;

$nssm = new Nssm('MyService');

// Install service (with application path and arguments)
// The bin() method sets the path to the executable (e.g., php.exe)
$nssm->bin('C:\path\to\your\app.exe')->install('arg1', 'arg2');

// Remove service
$nssm->remove();

use Abdulbasset\NssmPhp\Nssm;
use Abdulbasset\NssmPhp\Startup;
use Abdulbasset\NssmPhp\NssmSet;

$nssm = new Nssm('MyService');

$nssm->set(function (NssmSet $set) {
    $set->displayName('My Custom Service Name')
        ->description('This is a custom service managed by PHP')
        ->startup(Startup::Automatic)
        ->appDirectory('C:\path\to\app')
        ->output('C:\path\to\logs\stdout.log')
        ->error('C:\path\to\logs\stderr.log');
});

use Abdulbasset\NssmPhp\Nssm;
use Abdulbasset\NssmPhp\NssmSet;
use Abdulbasset\NssmPhp\NssmRotation;

$nssm = new Nssm('MyService');

$nssm->set(function (NssmSet $set) {
    // Basic rotation (enables AppRotateFiles)
    $set->rotation();

    // Advanced rotation configuration
    $set->rotation(function (NssmRotation $rotation) {
        // Rotate every 1 hour (accepts seconds or DateInterval)
        $rotation->everySeconds(new \DateInterval('PT1H'));

        // Rotate when file size exceeds 1 MB (in bytes)
        $rotation->everyBytes(1024 * 1024);

        // Enable rotation while the service is running (online rotation)
        $rotation->online();
    });
});

// To disable rotation
$nssm->set(fn(NssmSet $set) => $set->rotation(false));

use Abdulbasset\NssmPhp\Nssm;
use Abdulbasset\NssmPhp\Status;

$nssm = new Nssm('MyService');
$status = $nssm->status();

if ($status === Status::Running) {
    echo "Service is running!";
}

// Convenient helper methods:
if ($status?->running()) { ... }
if ($status?->pending()) { ... } // Returns true for StartPending or StopPending
if ($status?->exists()) { ... }  // Returns true if the service exists (not NotFound)

use Abdulbasset\NssmPhp\Nssm;
use Abdulbasset\NssmPhp\NssmSet;
use Abdulbasset\NssmPhp\NssmRotation;
use Abdulbasset\NssmPhp\Startup;
use Abdulbasset\NssmPhp\Status;

$nssm = new Nssm('MyAppOctaneServer');

$nssm->bin(PHP_BINARY)
    ->install(
        base_path('artisan'),
        'octane:start',
        '--quiet',
        '--port:8000'
    )
    ->set(fn(NssmSet $set) => $set
        ->displayName(config('app.name') . ' Octane Server')
        ->description('High-performance HTTP server for Laravel using ' . config('octane.driver') . '.')
        ->startup(Startup::Delayed)
        ->appDirectory(base_path())
        ->error(storage_path('logs/octane-err.log'))
        ->output(storage_path('logs/octane-out.log'))
        ->rotation()// enable it on service start/restart
        ->rotation(false) // disable rotation
        ->rotation(function (NssmRotation $rotation) {
            $rotation
                ->online()
                ->everySeconds(\Carbon\CarbonInterval::minutes(2))
                ->everyBytes(64 * 1024);
        })
    );

use Abdulbasset\NssmPhp\Nssm;

// During instantiation
$nssm = new Nssm('MyService', 'C:\path\to\nssm.exe');

// Or using the nssm() method
$nssm->nssm('C:\path\to\nssm.exe');
bash
composer