[Python] Using xyntservice to convert a python program into a service
-
1. Use of xyntservice
-
-
1.1. Introduction
-
1.2. xyntservice
-
2. Use xyntservice to change the python program to a service.
1. xyntservice usage 1.1. Introduction
Generally, the NT Service is a console application without a message loop. The NT Service can be started without waiting for the user to log on to the computer, and it will not stop running after the user logs out. However, it is difficult for many existing ActiveX controls to be used in console programs. You can directly write the NT Service in the form of a Windows program, but xyntservice provides a simpler solution. The source code of this article is a simple NT Service Program, which can start and close other programs as services. All you need to do is install this service and modify the. ini file. The following are the benefits of using xyntservice:
You can test and debug your program outside the NT Service. For example, you can run your program in the devstudio debugger and find errors in the source code in a single step. After "debugging is successful", you can configure it as a product and start it from the NT Service.
1.2. xyntservice
Xyntservice.exe is the name of the executable file of this NT Service Program, which can be downloaded here.
By default, the installed Service is automatically started when the computer restarts. You can also use the service icon to start or close a service from the control panel. When the service is started, it will create all processes defined in the xyntservice. ini file one by one. When the service is closed, it terminates these processes (in reverse order ). The xyntservice. ini file should be in the same directory as the executable file. The following is an example of this file.
[Settings]
Servicename = xyntservice
Proccount = 3
Checkprocess = 30
[Process0]
CommandLine = C:/mydir/xyroot.exe
Workingdir = C:/mydir
Pausestart = 1000
Pauseend= 1000
Userinterface = Yes
Restart = Yes
[Process1]
CommandLine = C:/mydir/xydatamanager.exe
Workingdir = C:/mydir
Pausestart = 1000
Pauseend= 1000
Userinterface = Yes
Restart = Yes
[Process2]
CommandLine = Java xyroot. xyroot xyrootjava. ini
Userinterface = No
Restart = No
The servicename attribute specifies the name to be used for this NT Service. The default name is xyntservice. If you copy the executable and. Int files to different directories, modify the servicename attribute in the. Int file to install and configure a different service.
The proccount attribute specifies the number of processes that you want to create for this service. Sections [process0] and [process1] define attributes of related processes. As you can see, three processes, xyroot.exe, xydatamanager, and Java, are created in this example. You can specify parameters for each process in the CommandLine attribute. The full path of the executable file must be specified for the corresponding process in the CommandLine attribute, unless the executable file is in the system path.
The checkprocess attribute specifies whether to check the processes started by xyntservice and the check frequency. If the value of this attribute is 0, no check is performed. If the attribute value is 30, xyntservice queries the operating system every 30 minutes to check whether the started process is still running. If the restart attribute value of the process (backdoor explanation) if yes, the service will be restarted. The default value of this attribute (if you do not specify it) is 60.
The workingdir attribute is the working directory of the current process. If this attribute is not specified, the working directory of the current process will be X:/winnt/system32. The pausestart attribute is the time (in milliseconds) that the service will wait after starting the current process ). This option is useful when the next process depends on the previous process. For example, the second process has to connect to the first process so that it can run again after the first process completes initialization. If you do not specify the pausestart attribute, the default value is 100 milliseconds.
When xyntservice is disabled, it first delivers the wm_quit message to the process it creates, and then calls the Win32 function terminateprocess. The pauseend attribute is the waiting time (in milliseconds) before the service calls terminateprocess ). This attribute can be used to clear and close the process. If you do not specify the pauseend attribute, the default value is 100 milliseconds. The userinterface attribute controls whether a login user can see the process created by xyntservice. However, it takes effect only when xyntservice runs under the local system account. This is the default mode. In this case, processes created by xyntservice cannot access the settings of specific users. You can configure xyntservice to run under a user account and easily achieve this in the control panel (double-click the Service icon, and then double-click the xyntservice in the list of installed services to bring up the configuration dialog box ).
The restart attribute is used to determine whether you want to restart the terminated process of xyntservice. If this attribute is no (this is the default value), the corresponding process will not be restarted. If yes, the terminated process will be restarted by xyntservice. The frequency of starting and terminating processes is specified by the preceding checkprocess attribute.
You can rebound from the command line (STOP and restart) Any process defined in the. Int file. For example, run the following command: xyntservice-B 2 to stop and restart the process defined in [process2] In. ini. Xyntservice can also be used to start and stop other services from the command line. The following are the commands for starting (run) and stopping (kill) Other services. Xyntservice-r nameofservicetorun xyntservice-K nameofservicetokill
Specifically, you can use the preceding command on the command line to start and stop the xyntservice itself. Note that you must start xyntservice with the parameter.
All errors that occur when running xyntservice are written to the log file, which is in the same directory as the executable file. The error code in the log file is the decimal number returned by the getlasterror API. You can view the details in msdn.
2. Use xyntservice to change the python program to a service.
After the above introduction, we can see that xyntservice can start any executable program as a service, so the python program can also be used. The following is a simple example:
[Settings]
Servicename = so.net
Checkprocessseconds = 10
[Process0]
CommandLine = C:/python25/python.exe C:/11.py
Workingdir = C :/
Pausestart = 1000
Pauseend= 1000
Userinterface = No
Restart = Yes
Install the so.net service: xyntservice.exe-I.
After the above configuration, you can directly start the so.net service without logon. The so.net service will call the C:/11. py program. Check whether the 11. py process is activated every 10 seconds. If the process is not active, restart it.
See:
Http://www.codeproject.com/system/xyntservice.asp