Stopping a program/application using a python script - python-3.x

Is there any way to terminate an application, for instance Google Chrome, using a python script or command?
Additionally, is there any python script/command to stop an application from starting.

You can use Windows taskkill from os.system() inside a Python script like that:
import os
os.system("taskkill /f /im process_im_goes_here.exe"")
Where /f means forcefully kill the process & /im is the process itself (ImageProcess) which means after that you get to specify the full path to the ImageProcess.
You can also use tasklist to find what processes are currently running.
Here are some taskkill usage examples:
taskkill /pid 1230 /pid 1241 /pid 1253
taskkill /f /fi "USERNAME eq NT AUTHORITY\SYSTEM" /im notepad.exe
taskkill /s srvmain /f /im notepad.exe
taskkill /s srvmain /u maindom\hiropln /p p#ssW23 /fi "IMAGENAME eq note*" /im \*
taskkill /s srvmain /u maindom\hiropln /fi "USERNAME ne NT*" /im \*
taskkill /f /fi "PID ge 1000" /im \*
Alternatively, you can use psutil.
You can learn more about taskkill here and/or python os.system.
Also, this has been answered before
Hope this helped!

Related

Issue using The Task Scheduler with cmd

Can Windows 10 The Task Scheduler run Reg.exe commands from command prompt?
When I past this command to make changes in reg.exe in CMD -- its works! But not works at Task Scheduler!
Reg.exe add "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize" /v "SystemUsesLightTheme" /t REG_DWORD /d "0" /f"
able to scheduler but not work command line (trying scheduler work at 18 PM)
schtasks /create /sc daily /tn "Tema" /tr "Reg.exe add "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize" /v "SystemUsesLightTheme" /t REG_DWORD /d "0" /st {hora} /f
deleting command thas works too
schtasks /delete /tn "Tema" /f
I able to Create and Delete it as well, but I can't make it runs appropriately from Task Scheduler -- nothing happens!

WVD outlook prompts for unlicensed/ non-commercial product

We are delivering outlook, word and excel as remote apps to our users, however whenever the users launch the remote application they are being prompted to recreate profile, suggesting it can't find the previous days OST file.
the prompt states "sign in to set up office", however if you go through the sign-in process, you are confronted with the same the next morning.
We are running Windows 10 multi-session with Office Pro-Plus.
I've confirmed the image is correct and the reg keys are for the shared computer setting. As outlined in the doc below:
https://learn.microsoft.com/en-us/deployoffice/troubleshoot-shared-computer-activation
Is there something I've missed?
Thanks in advance :)
Looks like you just need to Reset Microsoft 365 Apps for an enterprise activation state please refer to this link: https://learn.microsoft.com/en-us/office/troubleshoot/activation/reset-office-365-proplus-activation-state#step-2-remove-cached-identities-in-hkcu-registry to Clear Office credentials and activation state for managed devices
I have summarized the required code for you below, but you have to download the vbs files for link above 1st:
taskkill /IM WinWord.exe /F
taskkill /IM Excel.exe /F
taskkill /IM Outlook.exe /F
taskkill /IM PowerPoint.exe /F
taskkill /IM Lync.exe /F
taskkill /IM MSACCESS.EXE /F
taskkill /IM Teams.exe /F
taskkill /IM MSPub.exe /F
taskkill /IM OneNote.exe /F
taskkill /IM OneNoteM.exe /F
taskkill /IM iexplore.exe /F
taskkill /IM OfficeClickToRun.exe /F
taskkill /IM OneDrive.exe /F
cscript 1-HKLM-O365-no-prompt.vbs
cscript OLicenseCleanup.vbs
del /F /Q %localappdata%\Microsoft\Office\16.0\Licensing\
REG DELETE HKCU\Software\Microsoft\Office\16.0\Common\Identity /f
REG DELETE HKCU\Software\Microsoft\Office\16.0\Registration /f
REG ADD HKCU\Software\Microsoft\Office\16.0\Common\Identity /v EnableADAL /t REG_DWORD /d 00000000 /f
REG ADD HKCU\Software\Microsoft\Office\16.0\Common\Identity /v DisableADALatopWAMOverride /t REG_DWORD /d 00000001 /f
I come across this issue all the time at work, especially when users switch devices. It seems that sometimes Windows retains Office license info and when a new user tries to activate it doesnt update. Its relatively easy to fix and this site has a quick guide on how to remove the old license info
http://itwalkthroughs.com/knowledge-base/office-apps-show-unlicensed-product/

How to restart node.js server gracefully?

How to correctly close all connections before restarting/shutting down the web server?
This is a pretty nice npm module which I use to do some cleanups and so on... before the node.js process exists due to any reason (may be due to kill command from outside the process).
https://github.com/sindresorhus/exit-hook
To do the actual cleanup it depends on what you have running.
For example if its expressjs do server.close(); and so on....
I'm using Windows, so in my cmd, I used to use netstat -ano and findstr command to find a node.js process and then taskkill /pid 1234 /F ( 1234 is an example of pid)
However, it was so much work. Now, I use an easier way.
taskkill /F /IM node.exe
This command will close all the node.js processes in one command.

Nodejs Build in SublimeText 3 : The system can not find the path specified

The system can not find the path specified.
The build code is
[cmd: ['taskkill /F /IM node.exe & node', '$path']]
which is substituted by
[cmd: ['taskkill /F /IM node.exe & node', 'C:\\path\\server\\server.js']]
node is accessible as a command and is embedded in PATH
I do not understand what happens. It seems all right.
I believe the original code for taskkill is the following:
cmd: ['taskkill /F /IM node.exe & node', '$file']
ie '$file' instead of '$path'

CMD Batch file: How to use a text file to input commands

I am creating a batch file to siliently install nodejs on a Windows XP machine.
I am trying to automate the installation of node module dependencies (npm install).
I would normally issue npm install from the cmd prompt in the target installation directory.
I am struggling to automate the interaction with the command prompt from a batch file.
The following line in my batch script seems to make it possible for me to pipe a text file of commands to cmd:
for /F "usebackq delims=," %%i in ("c:\foo\source\npm_install.txt") do echo %%i | "c:\windows\system32\cmd.exe"
The batch file is located in c:\foo\source. I need to issue 'npm install' from c:\foo\bin.
If my npm_install.txt file is such:
cd /d c:\foo\bin,
npm install
The cmd prompt will perform the first command changing the directory from c:\ to c:\foo\bin.
It will then perform the second command but starting from c:\ again. The previous command to change directories doesn't persist. It seems every command in the text file will be issued from c:\ .
I next tried to issue both commands from a combined statment:
cd /d c:\foo\bin && npm install
It seems this approach will allow me to overcome the prior path problem but I then have an issue with the space between npm and install.
The cmd prompt performs c:\foo\bin>npm and causes npm to trip on the space.
I have tried enclosing the command without success: 'npm install', "npm install", (npm install).
Can anyone tell me what I am doing wrong?
Thanks.
The problem is that your batch-processing script is creating a new subshell to handle each line in your install file. Thus your cd command is executed in a subshell, changing its working directory, but then the subshell exits and you're back in the parent shell's working directory.
Can your main script simply call your install script (whose extension would have to be changed to ".bat")? call lets you run another batch file in the same shell and then continue running your original script.
You do not need this:
do echo %%i | "c:\windows\system32\cmd.exe". Simply put your commands in a block.
do (
command
command
...
)
With your previous statement, you start new cmd interpreter, ask it to execute a command for you and exit - that's why you loose effect of that cd.
If you do not specify tokens in for loop, only 1st is read. Plus, all tokens must be on same line (I'm not sure if what you show is not a byproduct of formatting)
Use "delims=" to read full line.
Do not mix commands with arguments if you do not have to: put only directories in your file:
c:\foo\bin
c:\bar\bin
so finally it becomes (I replaced cd with pushd/popd so you'll end up in the same dir you started from):
for /F "usebackq delims=" %%i in ("c:\foo\source\npm_install.txt") do (
pushd %%i
npm install
popd
)
Edit: if npm install is batch itself, you will need to use call, as ebohlman noted
NOTE: These three examples do not require you to put CALL in front of any batch files, that functionality is built-into each.
The smallest and simplest, it just executes the commands with their arguments:
#echo off
for /f "tokens=*" %%x in (c:\foo\source\npm_install.txt) do (
call %%x
)
To mimic a person typing the commands at the keyboard:
#echo off
for /f "tokens=*" %%x in (c:\foo\source\npm_install.txt) do (
setlocal enabledelayedexpansion
:: Mimic commandline
call set "cmd=%%cd%%"
echo !cmd!^>%%x
endlocal
:: Preform command
call %%x
echo.
)
To end up in the same directory you started in, no matter where the script ends:
#echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%x in (c:\foo\source\npm_install.txt) do (
:: Mimic commandline
call set "cmd=%%cd%%"
echo !cmd!^>%%x
:: Preform command
call %%x
echo.
)
endlocal

Resources