Stop process/program without admin rights - python - windows

Is it possible to end/stop/pause/freeze a process/program running in background with python without having administrator rights on a Windows 7/8/10? If yes how? If no why?

If it is yours, you can. Read about taskkill, taskmgr and wmic. Call them using os.system. Also, you may use os.kill, like:
os.kill (your-process-id, signal.SIGKILL)
(determining process ID requires reading process table, either parsing tasklist output or doing it via API)
If it is not yours, sorry... It means you should not kill it. But always, there is a way to kill the process - shut down the computer.

That depends.
If you have the rights to end a process running in background, you can do so.
You can execute commands (e.g. for killing) in python like in cmd by using the call method of the subprocess module.
from subprocess import call
call(['command','with','parameters'])
The command to kill a program in Windows is taskkill.
So to kill iexplore.exe by its name u use taskkill /F /IM iexplore.exe in cmd and call(['taskkill','/F','/IM','iexplore.exe']) in python code.
The documentation of call can be found here: https://docs.python.org/2/library/subprocess.html#subprocess.call
Manual of taskkill: https://technet.microsoft.com/de-de/library/bb491009.aspx
Hope this helps ^^

Related

cmd batch - How to set the "Taskkill" command blocking or non-blocking in Windows command line?

We have a group of machines running scripts. Most of our machines fail to finish the task after last weekend.
After comparing the working machines and non-workings machines we find out something weired that the behavior of Taskkill command has changed from non-blocking command into a blocking one on broken machines.
For example, to kill a process:
taskkill /im notexist.exe /f, if the process 'notexist.exe' does not exist, command will return immediatelly on working machines but keep a 5s timeout before return on broken machines.
They share the same harware and os version and the 'taskkill.exe' version.
The blocking one:
The non-blocking one:
Is there anything in the registry matters? How could I make the behavior consistently?

Gracefully terminate a command line application on Windows

I'm creating a command-line application, which spawns a process (command defined by a user, usually an HTTP server) and when the application's job is done, I want to let the process know it should terminate.
In UNIX, I can do that by sending SIGTERM and if the process doesn't end, then I can kill it brutally by SIGKILL.
In Windows, I struggle to find an alternative to the SIGTERM scenario. I learned there's taskkill /PID XXXX (without /f!), but
I found no information about what taskkill /PID XXXX does under the hood, hence I can't test it. I can't find how to handle whatever taskkill /PID XXXX sends on the process side.
It doesn't seem to work with commands in cmd.exe. I tried to run a simple server process in one cmd.exe, get its PID and in another window to taskkill /PID XXXX it, but taskkill refused to do that: ERROR: The process with PID XXXX could not be terminated. Reason: This process can only be terminated forcefully (with /F option).
So my question is: How to inform a command-line process in Windows that it should terminate without forcefully terminating it? How to receive and act upon such message on the process-to-be-terminated side?
GenerateConsoleCtrlEvent signals a console application as if the user had pressed Ctrl+C or Ctrl+Break. Console applications can ignore these signals (using SetContolCtrlHandler), but, by default, they just do an ExitProcess on themselves.
This is the same signal Windows uses when the user closes the console window, logs off, or shuts down.
TaskKill uses PostMessage(hwnd, WM_CLOSE, 0, 0); (basically the same as pressing the X on a window) if the process has a visible window.
If you know the application is a console application you can use GenerateConsoleCtrlEvent instead.

Run many commands using PID of a single Win32Process

I create a Win32Process (cmd.exe) on a remote machine using WMI.
C:\Users\ayush_m>wmic /node:10.0.0.0 /user:ayush_m /password:pwd PROCESS CALL Create "cmd.exe"
Executing (Win32_Process)->Create()
Method execution successful.
Out Parameters:
instance of __PARAMETERS
{
ProcessId = 10088;
ReturnValue = 0;
};
The ProcessID returned by WMI (10088) is the PID of the cmd.exe process started at the remote machine. Now i would like to run some other commands such as mkdir or copy or run an exe file using above PID .i.e use the PID that i have above (10088) and run many more commands on that machine.
I basically do not want to create a new Win32Process everytime i run a command. Please suggest any ideas or observations.
Thanks in advance for help.
From what I understand you could be looking for some remote shell - did you try using PsExec? Wmic just spawns a process but doesn't grant any kind of control over it. As RRUZ suggests, create a BAT/VBS file to execute what you need, or if your environment supports PowerShell, have PowerShell do the stuff.
To sum up:
To have an interactive console - use PsExec
To execute a set of tasks (ex. create C:\MyDirectory and put some logs there) - create a BAT/VBS file and call it using wmic just as you did with cmd.exe
PowerShell could certainly be helpful here if you need to automate things. It was created with automation in mind, so take advantage of it :-)
Best regards, AlexP
(I'm not sure if I'm not grave-digging at the moment, the question is quite old what I noticed just now, but still didn't have an answer)

In batch programing can one command run before the previous command finishes executing?

In batch programing is one command waited until completed until the next one is run? What I mean is for example
net stop wuauserv
net start wuauserv
Since net stop wuauserv takes a while to complete is it given time to complete or do I need another command to wait until it completes?
The NET STOP command does wait (or timeout while waiting) for a service to stop or start.
You can check the %ERRORCODE% from the command to get more information about if there was a problem or if it worked as expected.
In general most system command line tools return control once they are done executing. A few specialized programs will call into other services or systems and may return control before execution is complete. You will need to check the docs for whatever you are trying to run, but generally processes exit once the 'task' they perform is complete.
In a batch file, all commands are run sequentially, and execution waits for the command to complete.
In your example, net stop wuauserv would complete before net start wuauserv gets run.
You could confirm that by running something you know will take a long time, such as
ping www.google.com
ping www.stackoverflow.com
and you'll see that the second ping does not start until the first completes.
In your case, yes the second command will not execute until the first finishes.
However, GUI apps will start up and return control the batch file.
For example,
PING localhost
NOTEPAD
DIR
The DIR command will execute even if NOTEPAD is still running.

start without inheritance of parents file descriptors

I need to start some process on winXP with "start" command.
Sounds simple.
But is there a way that the started process would not inherit any ports from parent?
I start children in my program using:
system "start x -params"
Now when parent is being killed, I can't start it again because I'm learned by errors that some process is already occupying port (which killed parent was using).
I don't want to use:
createProcess (from winAPI, where this can be setup to not inherit fds)
use python in my start string (or any similar interpreters)
Is there a way to start my child process in a way I want them to start?
Is there any "start" alternative?
So after diggin a while, i've found:
psexec
with commandline like:
psexec -d -s myprogram > logfile.log 2>&1
everything is solved.
powershell -command "Start-Process myprogram.exe"

Resources