1

I am writing a test application in python and to test some particular scenario, I need to launch my python child process in windows SYSTEM account. I can do this by creating exe from my python script and then use that while creating windows service. But this option is not good for me because in future if I change anything in my python script then I have to regenerate exe every-time.

If anybody have any better idea about how to do this then please let me know.

Bishnu

Bishnu
  • 73
  • 7

4 Answers4

2
  1. Create a service that runs permanently.
  2. Arrange for the service to have an IPC communications channel.
  3. From your desktop python code, send messages to the service down that IPC channel. These messages specify the action to be taken by the service.
  4. The service receives the message and performs the action. That is, executes the python code that the sender requests.

This allows you to decouple the service from the python code that it executes and so allows you to avoid repeatedly re-installing a service.

If you don't want to run in a service then you can use CreateProcessAsUser or similar APIs.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Thanks for your reply. but my requirement is to relaunch the process in every test run, because I need to get the process id and using that process id I will decide whether I want to monitor that process or not at runtime. Since this test suite is going to be run with our automation I can't have one dedicated service running at my test machine. And another reason is that I have written Python script which get launched in local user context and I want to use same python script to launch in SYSTEM mode so that same code is executed in both contexts. – Bishnu Aug 13 '14 at 07:53
  • I answered based on what is present in the question. In any case, your ever running service can launch processes. So what I describe will solve your problem. – David Heffernan Aug 13 '14 at 08:23
  • @Bishnu: From your description, it sounds like you can still have one dedicated SYSTEM service running; you will still run the Python script once for each test, and the process id and any other information that's relevant can be part of the information that it sends over the IPC to the service. This really is both the safest and the simplest way to do it in almost all cases. – abarnert Aug 13 '14 at 08:24
0

You could also use Windows Task Scheduler, it can run a script under SYSTEM account and its interface is easy (if you do not test too often :-) )

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

To run a file with system account privileges, you can use psexec. Download this : Sysinternals

Then you may use :

os.system 

or

subprocess.call

And execute:

PSEXEC -i -s -d CMD "path\to\yourfile"
Martin Evans
  • 45,791
  • 17
  • 81
  • 97
Anonymous
  • 596
  • 1
  • 9
  • 26
0

Just came across this one - I know, a bit late, but anyway. I encountered a similar situation and I solved it with NSSM (Non_Sucking Service Manager). Basically, this program enables you to start any executable as a service, which I did with my Python executable and gave it the Python script I was testing on as a parameter.

So I could run the service and edit the script however I wanted. I just had to restart the service when I made any changes to the script.

One point for productive environments: Try not to rely on third party software like NSSM. You could also achieve this with the standard SC command (see this answer) or PowerShell (see this MS doc).

Chris Tophski
  • 930
  • 1
  • 6
  • 23