icanhazstring/systemctl-php

Can't invoke systemctl --user.

tommyvdv opened this issue · 8 comments

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.

Hi @tommyvdv

u guessed correct. Currently the lib does not support the parameter --user. This has to be added to invoke systemctl on a user level.

I will mark this as enhancement and will try to implement it asap :)
Thanks for the feedback tho. 👍

Hi @icanhazstring
Thanks for confirming this and for sharing your project.
Here's looking forward to v1.0.

@tommyvdv is just pushed a commit into dev branch 1.x.
This should add the ability to switch the scope of the SystemCtl instance to either user oder system.

$systemCtl = new SystemCtl();
$systemCtl->getService(...); // same as $systemCtl->system()->getService()
$systemCtl->user()->getService(...); // uses the --user argument on systemctl binary

You can switch the scope with each call. Just remember to use ->user() oder ->system() before getting the unit you want.

You can check this out by using current dev branch.

$ composer require icanhazstring/systemctl-php:dev-1.x

Hope this works for you. I will try to finish the next milestone coming this weekend and tag the finished v1.0.

Hi @tommyvdv can you confirm the implementation. So i can close the issue. And possibly tag a release for it.

I've tried to test this on an ubuntu 16.04 box but can't confirm it's working.
While I can get a system service using ->getService('apache2.service') and list the units using ->listUnits(), it seems ->user()->listUnits() returns an empty array or throws a UnitNotFoundException when using ->user()->getService(…).

Running the commands directly I can retrieve a couple of services like obex.service from the --user instance;

$ systemctl --user status obex
● obex.service - Bluetooth OBEX service
   Loaded: loaded (/usr/lib/systemd/user/obex.service; disabled; vendor preset: enabled)
   Active: inactive (dead)

I'm probably going about this the wrong way but in case I'm not; hope this helps.

Hm. Thanks for the feedback tho. I will look into this.

Good news @tommyvdv. I probably found out what went wrong.
The problem was, that there is the possibility that a unit is not running and my implementation does not cover getting loaded but inactive units. But I figured a way of supporting this.

Bad news tho, I discovered some minor other bugs on the way which need attention as well ;)