Skip to content

Latest commit

 

History

History
169 lines (110 loc) · 6.35 KB

service_library.md

File metadata and controls

169 lines (110 loc) · 6.35 KB
title menus permalink
Service Library
library
/service-library

return to home

The Service library for PHP

This library is available here for help you to use the win32service extension for PHP.

Install

This library requires the Win32Service extension for PHP.

c:\> composer require win32service/service-library

Use for register or unregister the service

See the example 'example/adminAction.php'. The code in this example needs the administrator right.

Security : Do not execute this from the web request for security reasons.

Use to manage any services

See the example 'example/managementActions.php'. The code in this example needs the administrator rights or a specific account with limited rights.

With Administrator right

If you execute their actions with the administrator right, please limit the action to the local host.

Security : Do not execute this from the web request for security reasons.

With a service account with limited rights

The recommended way for use remotely their actions is to make a new service account in your ActiveDirectory without any right.

Open the right of the account for each service needed to manage. To open the right, see the Microsoft Documentation

After the service is registered run this command to read the right set on the service

C:\> sc sdshow my_test_service

Replace the my_test_service by your service identifier to read the information.

Note : Run this command in cmd administrator console.

The result example:

D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWLOCRRC;;;IU)(A;;CCLCSWLOCRRC;;;SU)S:(AU;FA;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;WD)

Get the SID for the service account, with this command:

C:\> wmic useraccount where (name='<username>' and domain='<domain_short_name>') get name,sid

The response:

Name        SID
<username>  S-1-5-21-1553544295-1745644848-8500016-126000

Add the string into the right after changing the :

(A;;RPWPRCDT;;;<SID>)

The resulst will be gone:

(A;;RPWPRCDT;;;S-1-5-21-1553544295-1745644848-8500016-126000)

Note : the CCLCRPWPRCDT represent the right to set for the SID. See the Microsoft Documentation to know the representation.

The final right is like this:

D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWLOCRRC;;;IU)(A;;CCLCSWLOCRRC;;;SU)(A;;RPWPRCDT;;;S-1-5-21-1553544295-1745644848-8500016-126000)S:(AU;FA;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;WD)

Now, set the new right for the service with the Administrator CMD window:

C:\> sc sdset my_test_service D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWLOCRRC;;;IU)(A;;CCLCSWLOCRRC;;;SU)(A;;RPWPRCDT;;;S-1-5-21-1553544295-1745644848-8500016-126000)S:(AU;FA;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;WD)

Replace the my_test_service by your service identifier to write the security descriptor.

Running a service

See the example 'example/service.php'.

For running a script as a service, you must be extending the Win32Service\Model\AbstractServiceRunner class. The run function is called into the loop and do return before 30s. If the run execution duration is over 30s, the service is marked stopped by the Windows Service Manager. In this case, the method lastRunIsTooSlow is called and the first argument contains the last run execution duration.

Automatic restart the service

Since the version 0.4.0 of the Win32Service extension, you can define the exite mode and exit code. If the exit mode is not gracefully and the exit code is geater than 0, the recovery setting of the Windows service manager can be used.

In this case, define the action for first, second and next fail of the service to restart value.

Usage with Symfony

For use with Symfony, define an Command and inject into your service class. Add an InputOption named max-run for define the number of loop executed between each service restart.

In the execute function call the service doRun function with the max-run value.

Run tests

Prerequisites : Composer is installed on your computer.

After clone or download the repository open CMD window and go to the project directory. Run this command for download needed dependencies:

c:\> composer install -o

And run Atoum for tests:

c:\> vendor/bin/atoum.bat

Help

Crash, feature request, or suggest, please open an issue.

Contributing

If you want to contribute, you can open an issue to propose your idea. If you can write the code, fork the repository and write the code, finally submit your enhancement by a pull request.

<script type="text/javascript"> var _paq = window._paq = window._paq || []; /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ _paq.push(["setDocumentTitle", document.domain + "/" + document.title]); _paq.push(["setDoNotTrack", true]); _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u="https://analytics.nahan.fr/"; _paq.push(['setTrackerUrl', u+'matomo.php']); _paq.push(['setSiteId', '3']); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript'; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); })(); </script>