I have a PowerShell script that is run automatically when our monitoring service detects that a website is down.
It is supposed to stop the AppPool (using Stop-WebAppPool -name $AppPool;), wait until it is really stopped and then restart it.
Sometimes it the process does not actually stop, manifested by the error
Cannot Start Application Pool:
The service cannot accept control messages at this time.
(Exception from HRESULT: 0x80070425)"
when you try to start it again.
If it takes longer than a certain number of seconds to stop (I will chose that amount of time after I have timed several stops to see how long it usually takes), I want to just kill the process.
I know that I can get the list of processes used by workers in the AppPool by doing dir IIS:\AppPools\MyAppPool\WorkerProcesses\,
Process ID State Handles Start Time
---------- ----- ------- ----------
7124 Running
but I can't figure out how to actually capture the process id so I can kill it.
In case that Process ID is really the id of process to kill, you can:
$id = dir IIS:\AppPools\MyAppPool\WorkerProcesses\ | Select-Object -expand processId
Stop-Process -id $id
or
dir IIS:\AppPools\MyAppPool\WorkerProcesses\ | % { Stop-Process -id $_.processId }
In Command Prompt on the server, I just do the following for a list of running AppPool PIDs so I can kill them with taskkill or Task Mgr:
cd c:\windows\system32\inetsrv
appcmd list wp
taskkill /f /pid *PIDhere*
(Adding answer from Roman's comment, since there maybe cache issues with stej's solution)
Open Powershell as an Administrator on the web server, then run:
gwmi -NS 'root\WebAdministration' -class 'WorkerProcess' | select AppPoolName,ProcessId
You should see something like:
AppPoolName ProcessId
----------- ---------
AppPool_1 8020
AppPool_2 8568
You can then use Task Manager to kill it or in Powershell use:
Stop-Process -Id xxxx
If you get Get-WmiObject : Could not get objects from namespace root/WebAdministration. Invalid namespace then you need to enable the IIS Management Scripts and Tools feature using:
ipmo ServerManager
Add-WindowsFeature Web-Scripting-Tools
Related
I am running a Node.JS app that is executing some powershell commands to set the DNS services for my WiFi interface.
Unfortunately, the script fails without admin permissions, so I am trying to elevate the script within node.js
Start-Process -FilePath powershell.exe -ArgumentList {
Write-Host "Hello"
Set-DnsClientServerAddress -InterfaceIndex 10 -ServerAddresses ("127.0.0.1", "8.8.8.8")
sleep 5
} -verb RunAs
For some reason, this isn't applying the changes, despite the script outputting "hello" and waiting for 5 seconds. Does anyone know why that might be?
I am on arch linux, accessing an account on a server over SSH. I have run a bash script containing recursion that results in an infinite loop of "no such file or directory" which continues despite any interrupt command ctrl C etc, it is totally uninterruptible. This eventually results in an endless stream of bash: fork: No child processes. I cannot execute any commands whilst this happens, and when it stops with "Resource temporarily unavailable", i am unable to execute any commands to kill the script because "bash: fork: No child processes" starts up again. I have no idea what to do, any help?
ps doesn't work
Looks like you've caused a fork bomb. You can try the methods here to stop it, but you'll most likely end up needing to reboot.
Run kill -9 -1 from the user login that caused the forkbomb . No need to reboot.
PS: Consult your seniors before running it on Prod server
1) ps faux (find PID and place in second command)
2) kill [PID]
If any virus attack then again this process come so you need to enable virus scanner on cpanel and scan and remove.
Important:
Hosting providers must install the following services for this interface to appear:
The ClamAV Scanner plugin in WHM’s Manage Plugins interface (WHM >> Home >> cPanel >> Manage Plugins).
The Exim Mail Server service on the server in WHM’s Service Manager interface (WHM >> Home >> Service Configuration >> Service Manager).
Run ps faux (you might need to run it from other user or with sudo) and search for the offending process (may look like a big branch of the tree)
If needed, kill the process via its PID
When deploying a new version of an existing .net core website. How do I first safely stop the old running kestrel app?
Here is an exmaple of what I would like to write (Pseudo deployment script):
dotnet stop mysite/mysite.dll <---- this line here
mv mysite/ mysite.bak/
cp newly-published-mysite/ mysite/
dotnet run mysite/mysite.dll
killall dotnet seems a little unsafe. How would it work if I were hosting two small sites on one box?
Accordingly to this discussion, there is no safe way to stop Kestrel now. You need to find a PID by name of your dll and kill it:
kill $(ps aux | grep 'MySite.dll' | awk '{print $2}')
In case of process tree, you need to manually grep all child IDs and call kill for each. Like it was done in
Microsoft.Extensions.Internal.ProcessExtensions.KillTree method (correct link from the discussion).
I have decided to use supervisor to monitor and manage the process. Here is an excellent article on getting it set up.
It allows simple control over specific dotnet apps like this:
supervisorctl stop MyWebsiteName
supervisorctl start MyWebsiteName
And it has one huge advantage in that it can try restart the process if it falls over, or when the system reboots... for whatever reason.
I'm using
Start-Process grunt -ArgumentList 'serve' -PassThru
and add it to an ArrayList. When I view said ArrayList it shows cmd process, not actual node.js process that was launched. If I try to Stop-Process it, quite obviously, kills cmd process, not the running grunt. I can locate running grunt with Get-Process node.
The problem is I need multiple grunts running at the same time and I want a way to distinguish between them somehow. Is there any way I can get actual node process into PowerShell upon initializing it?
This happens because grunt is launched via grunt.cmd which is run by CMD. This file launches grunt in node.
Here is an example of how to find notepad2.exe launched from a CMD similarly to your example:
# start a CMD which starts notepad2 and then waits on it
$process = Start-Process -PassThru 'cmd.exe' '/c "C:\Program Files\Notepad2\Notepad2.exe"'
# Wait for notepad2 to be launched
Start-Sleep -Seconds 2
# find the children of CMD, and kill the one which is notepad2
(Get-CimInstance win32_process -Filter "ParentProcessId='$($process.Id)' AND Name = 'Notepad2.exe'") | %{ Stop-Process -Id $_.ProcessId}
Translating this for grunt:
# start Grunt via grunt.cmd
$process = Start-Process grunt -ArgumentList 'serve' -PassThru
# Wait for node to be launched
Start-Sleep -Seconds 2
# find the children of CMD, and kill the one which is node
(Get-CimInstance win32_process -Filter "ParentProcessId='$($process.Id)' AND Name = 'node.exe'") | %{ Stop-Process -Id $_.ProcessId}
grunt isn't a Win32 executable, it's a javascript file that's run by node.exe. The easiest way to get the instance of node that's running grunt would be to start it yourself:
$ps = start-process -passthru node -argumentlist "$env:APPDATA\npm\node_modules\grunt\bin\grunt"
I would like to write shell script to start and stop tomcat server.For stopping the tomcat I am using this command "./bin/shudown.sh" or "./bin/catalina.sh stop". This is not working most of the times, tomcat is still running.So I would like to kill the tomcat after giving shutdown command and wait for sometime(say 5min). Can anybody help me how to do it?
./bin/catalina.sh should support this. If you run that command without any options, it will print out its usage, which describes:
stop n -force Stop Catalina, wait up to n seconds and then use kill -KILL if still running
In order to make this work, you need to set the environment variable CATALINA_PID to a file name that will be used to hold the Tomcat process ID. To start Tomcat, use:
export CATALINA_PID=/tmp/catalina.pid
./bin/catalina.sh start
And then to stop it:
export CATALINA_PID=/tmp/catalina.pid
./bin/catalina.sh stop 600 -force
This will try to stop it, wait for 5 minutes, then kill it if necessary. Note that this will run in the foreground by default (locking up the terminal instance); use a trailing & to run the command in the background.
You can use: pkill -9 tomcatServiceName or killall -9 tomcatServiceName.