PHP wrapper for systemctl

Overview

systemctl-php

CI Pipeline Code Climate Test Coverage Join the chat at https://gitter.im/icanhazstring/systemctl-php

PHP wrapper for systemctl

How to install

$ composer require icanhazstring/systemctl-php

Current supported units

See Units

If you like to add support for more units, feel free to grab an issue and contribute.

Current supported commands

  • start
  • stop
  • enable
  • disable
  • reload
  • restart
  • isEnabled
  • isActive

If you like to add support for more commands, feel free to contribute.

How to change the binary

SystemCtl::setBinary('/bin/systemctl');

How to change command timeout

To change command tmeout simply call the static method setTimeout.

SystemCtl::setTimeout(10);

The default timeout is set to 3 seconds

"I need sudo to run commands"

If you need sudo, you should execute the bin executable with sudo. The incode support was dropped due to security reason.

How do I start/stop/restart a unit?

Simply is that. First we instantiate a SystemCtl instance an load a unit from a specific type. Here we use a Service. You will always get back true if the command succeeded. Otherwise the method will throw a CommandFailedException.

$systemCtl = new SystemCtl();

// start/stop/enable/disable/reload/restart
$systemCtl->getService('nginx')->start();
$systemCtl->getService('nginx')->stop();

How to Contribute

Clone the repo and install using composer

$ composer install

Make your changes and make sure you run test, codesniffer and phpstan.

$ composer test
> vendor/bin/phpunit
PHPUnit 9.5.3 by Sebastian Bergmann and contributors.

...............................................................  63 / 128 ( 49%)
............................................................... 126 / 128 ( 98%)
..                                                              128 / 128 (100%)

Time: 00:00.033, Memory: 10.00 MB

OK (128 tests, 192 assertions)

$ composer cs
> vendor/bin/phpcs --standard=PSR2 src/ && vendor/bin/phpcs --standard=PSR2 tests/

$

$ composer analyse
> vendor/bin/phpstan analyse --no-progress
Note: Using configuration file /data/systemctl-php/phpstan.neon.


 [OK] No errors

$

Credits

This library is heavily influenced by @mjanser php-systemctl.

Comments
  • Create services

    Create services

    Are there any plans to add the ability to create services? man page: https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/sect-Managing_Services_with_systemd-Unit_Files.html

    enhancement 
    opened by Gemorroj 5
  • Run command with sudo

    Run command with sudo

    If you need sudo, you should execute the bin executable with sudo. The incode support was dropped due to security reason.

    How can I do this? If I do this: $cmd = "php_systemctl"; \SystemCtl\SystemCtl::setBinary("sudo $cmd"); I get an error: sh: sudo /home/.../php_systemctl: No such file or directory

    opened by wadealer 4
  • Add new information methods (`isActiveRaw`, `isEnabledRaw` and `show`)

    Add new information methods (`isActiveRaw`, `isEnabledRaw` and `show`)

    This PR adds three new methods to help get additional information about services. We are aiming to use this library to display information about several services on a web dashboard, with the potential management of these being a secondary concern.

    To this end, I've added some additional info methods:

    • isActiveRaw(): retrieve the raw string output of the is-active command. We need this as we use some 'oneshot' services, which can be 'inactive' (as opposed to failed) under normal circumstances. Simply checking them to see if they are 'active', as the normal isActive() method does, is not enough for us: we need to raw response from the systemctl command.
    • isEnabledRaw(): retrieve the raw string output of the is-active command. We don't strictly need the output of this but I added it for completeness.
    • show(): retrieve the raw string output of the show command but parsing it to an array. We need some more detailed information like the time a 'oneshot' last ran, and potentially the ExecStart line to see the command actually run for debugging. This method takes the (extensive) output of the show command and makes it available to be easily consumed via a correctly keyed array of the output of the command.

    Happy to make changes to the above to better conform with your library. I just quickly added these methods so we could access this additional systemd information.

    opened by deancsmith 4
  • Change assertEquals to assertSame.

    Change assertEquals to assertSame.

    AssertSame reports an error if the two elements do not share type and value. For example: this code $this->assertEquals(3, true); give uns true :-//// Please use assertSame, not assertEquals

    opened by wesolowski 3
  • Compatibility with symfony/process ^4.0

    Compatibility with symfony/process ^4.0

    https://github.com/symfony/process/blob/master/CHANGELOG.md#400

    The changelog does not look like something that would concern this library. Could you make the constraint allow either ^3.0 or ^4.0, please?

    opened by func0der 2
  • Re-arrange tests to differenciate between integration and unit tests.

    Re-arrange tests to differenciate between integration and unit tests.

    I have restructured the tests a little into integration and unit tests.

    All of your test are still there and functional. Included are some new ones and a little change regarding the instance name obtain process. It did not take the suffixes into account, which one could possibly add to the name of the unit.

    As you will see, I have add a UnitStub, which is used to test your AbstractUnit class now and in my other PR which regards new commands like 'isActive' and 'isEnabled'.

    It would be great if this PR would be merged, because my other one relies on it. I have split those two up, because it appeared a little heavy to me to restructure tests and introduce a new future in one go. I hope you like it :)

    If you have any questions, I'd be glad to answer them.

    opened by func0der 2
  • Use binary-only images for installing Composer

    Use binary-only images for installing Composer

    See: https://blog.codito.dev/2022/11/composer-binary-only-docker-images/

    Additionally I added support for missing PHP versions through Docker Compose 🙂

    opened by Wirone 1
  • Using assertSame to make assertion strict

    Using assertSame to make assertion strict

    Change log

    • Using the assertSame to make assertion equals strict.
    • Fixing the following coding style via PHP_CodeSniffer:
    FILE: ...hp/systemctl-php/src/Exception/UnitTypeNotSupportedException.php
    ----------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    ----------------------------------------------------------------------
     11 | ERROR | [x] Opening brace must not be followed by a blank
        |       |     line (PSR12.Classes.OpeningBraceSpace.Found)
    ----------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ----------------------------------------------------------------------
    
    opened by peter279k 1
  • Consider migrating PHPUnit configuration to be compatible with latest PHPUnit version

    Consider migrating PHPUnit configuration to be compatible with latest PHPUnit version

    When running the vendor/bin/phpunit with php-7.4 and PHPUnit 9.4.x versions, it presents following message:

    PHPUnit 9.4.2 by Sebastian Bergmann and contributors.
    
    Warning:       Your XML configuration validates against a deprecated schema.
    Suggestion:    Migrate your XML configuration using "--migrate-configuration"!
    

    And I think we can consider using the vendor/bin/phpunit --migrate-configuration to resolve this issue.

    opened by peter279k 1
  • Add tests to prove broken behavior.

    Add tests to prove broken behavior.

    Fix OutputFetcher to properly fetch only the unit name.

    Introduce new abstract method 'getUnitSuffix'

    Revert AbstractUnit tests back to use UnitStub.

    Fix command execution.

    Fix Integration test for unit.

    Change fetching of specific units from simply instacing them to properly fetching them from the systemctl output.

    Fix command execution to apply to correct unit.

    Add PHPDoc.

    opened by func0der 1
  • Add Unit 'device'

    Add Unit 'device'

    First of all, the Device unit type should implement the UnitInterface similar to the Timer or Service. Despite that everything, that is possible with a Timer should also be possible with a Device.

    In the most simple case, you only need to provide the Device class implenting the UnitInterface extending AbstractUnit. The class should provide the correct suffix.

    For more information about a Unit or Device see systemd documentation: https://www.freedesktop.org/software/systemd/man/systemd.unit.html https://www.freedesktop.org/software/systemd/man/systemd.device.html

    Also see PR #27 from @peter279k for reference.

    enhancement help wanted hacktoberfest 
    opened by icanhazstring 1
  • Improve Composer option strategy

    Improve Composer option strategy

    Changed log

    • Improving the Composer option strategy for adding the --ignore-platform-req=php when running tests with PHP 8.x.
    • Improving the Composer option strategy for without using the --ignore-platform-req=php when running tests with PHP 7.x.
    opened by peter279k 0
  • Add user scope for UnitInstaller

    Add user scope for UnitInstaller

    Currently the UnitInstaller does not work with user scope. The $installPath is fix for system scope installations.

    This should work similar to switching the scope to find a unit.

    $systemCtl->user()->install($template); // default: ~/.config/systemd/user/
    $systemCtl->system()->install($template); // default: /etc/systemd/system/
    

    user home should be retrieved automatically. http://php.net/manual/en/function.posix-getpwuid.php

    To resolve the paths we should use the currently empty PathResolver

    enhancement 
    opened by icanhazstring 0
  • Can't invoke systemctl --user.

    Can't invoke systemctl --user.

    I'm no expert on systemctl or its related technology, far from it.
    I do know that systemctl and systemctl --user are two separated concepts on the linux distro I'm working with.

    At first glance it doesn't seem possible to add the option user to the command.

    Running ->getService('test') works if the unit test is configured as a system unit.

    $ systemctl status test
    

    Running ->getService('test') works if the unit test is configured as a user unit but throws an (expected) UnitNotFoundException exception.

    $ systemctl --user status test
    

    I hope this is on the road map or, better yet, I'm just missing the obvious solution.

    enhancement 
    opened by tommyvdv 8
Releases(0.8.2)
Owner
Andreas Frömer
Proud daddy, Gamer and Lead Software Developer
Andreas Frömer
A developer-friendly wrapper around execution of shell commands.

ptlis/shell-command A developer-friendly wrapper around execution of shell commands. There were several goals that inspired the creation of this packa

brian ridley 18 Dec 31, 2022
CLI wrapper for smalot/pdfparser.

CLI wrapper for smalot/pdfparser.

Valerij Ivashchenko 1 Jan 6, 2022
PHP Interminal is a command-line tool that gives you access to PHP Internals discussions in your terminal.

PHP Interminal is a command-line tool that gives you access to PHP Internals discussions in your terminal. ??

Nuno Maduro 32 Dec 26, 2022
PHP Reverse Shell > reverse-shell.php

PHP Reverse Shell > reverse-shell.php PHP Cmd Shell > cmd.php JPG cmd Shell > cmd.jpg /etc/passwd Pulling Shell > etc-passwd.php Configuration Pulling

Dark-Network 5 Feb 24, 2022
Web Shell Detector – is a php script that helps you find and identify php/cgi(perl)/asp/aspx shells.

Web Shell Detector – is a php script that helps you find and identify php/cgi(perl)/asp/aspx shells. Web Shell Detector has a “web shells” signature database that helps to identify “web shell” up to 99%. By using the latest javascript and css technologies, web shell detector has a light weight and friendly interface.

Maxim 763 Dec 27, 2022
A REPL for PHP

PsySH PsySH is a runtime developer console, interactive debugger and REPL for PHP. Learn more at psysh.org and in the manual. PsySH manual ?? Installa

Justin Hileman 9.4k Jan 6, 2023
CRON for PHP: Calculate the next or previous run date and determine if a CRON expression is due

PHP Cron Expression Parser NOTE This fork has been deprecated and development moved to https://github.com/dragonmantank/cron-expression. More informat

Michael Dowling 4.9k Jan 5, 2023
A tiny REPL for PHP

Boris A tiny, but robust REPL for PHP. Announcement: I'm looking to add one or two additional collaborators with commit access. If you are actively in

null 2.2k Dec 30, 2022
🖥 Build beautiful PHP CLI menus. Simple yet Powerful. Expressive DSL.

Contents Minimum Requirements Installation Upgrading Usage Quick Setup Examples API Appearance Menu Title Colour Width Padding Margin Borders Exit But

PHP School 1.9k Dec 28, 2022
PHP's best friend for the terminal.

Running PHP from the command line? CLImate is your new best bud. CLImate allows you to easily output colored text, special formats, and more. Installa

The League of Extraordinary Packages 1.8k Dec 30, 2022
An Elegant CLI Library for PHP

Commando An Elegant PHP CLI Library Commando is a PHP command line interface library that beautifies and simplifies writing PHP scripts intended for c

Nate Good 793 Dec 25, 2022
Cilex a lightweight framework for creating PHP CLI scripts inspired by Silex

Cilex, a simple Command Line Interface framework Cilex is a simple command line application framework to develop simple tools based on Symfony2 compon

null 624 Dec 6, 2022
A powerful command line application framework for PHP. It's an extensible, flexible component, You can build your command-based application in seconds!

CLIFramework CLIFramework is a command-line application framework, for building flexiable, simple command-line applications. Commands and Subcommands

Yo-An Lin 428 Dec 13, 2022
[ABANDONED] PHP library for executing commands on multiple remote machines, via SSH

#Shunt Inspired by Ruby's Capistrano, Shunt is PHP library for executing commands on multiple remote machines, via SSH. Specifically, this library was

The League of Extraordinary Packages 436 Feb 20, 2022
A PHP library for command-line argument processing

GetOpt.PHP GetOpt.PHP is a library for command-line argument processing. It supports PHP version 5.4 and above. Releases For an overview of the releas

null 324 Dec 8, 2022
An object-oriented option parser library for PHP, which supports type constraints, flag, multiple flag, multiple values, required value checking

GetOptionKit Code Quality Versions & Stats A powerful option parser toolkit for PHP, supporting type constraints, flag, multiple flag, multiple values

Yo-An Lin 140 Sep 28, 2022
PHP Version Manager for the CLI on Windows

This package has a much more niche use case than nvm does. When developing on Windows and using the integrated terminal, it's quite difficult to get those terminals to actually listen to PATH changes.

Harry Bayliss 49 Dec 19, 2022
PHP CLI tool which allows publishing zipped MODX extra to modstore.pro marketplace

MODX Extra Publisher PHP CLI tool which allows publishing zipped MODX extra to modstore.pro marketplace. Installation global? local? To install packag

Ivan Klimchuk 3 Aug 6, 2021