windows the process did not stop after the service was shut down - windows

I created a batch file named runServer.bat to run activeMq,redis...
#echo off
echo run activemq
start cmd /k "title activemq&&cd .\apache-activemq-5.16.5\bin\win64&&activemq.bat"
TIMEOUT /T 3
echo run Redis
start cmd /k "title redis&&cd .\Redis&&redis-server redis.windows.conf"
TIMEOUT /T 3
I put the batch file register windows server as a service.
#echo off
echo 获取Administrator权限
cacls.exe "%SystemDrive%\System Volume Information" >nul 2>nul
if %errorlevel%==0 goto Admin
if exist "%temp%\getadmin.vbs" del /f /q "%temp%\getadmin.vbs"
echo Set RequestUAC = CreateObject^("Shell.Application"^)>"%temp%\getadmin.vbs"
echo RequestUAC.ShellExecute "%~s0","","","runas",1 >>"%temp%\getadmin.vbs"
echo WScript.Quit >>"%temp%\getadmin.vbs"
"%temp%\getadmin.vbs" /f
if exist "%temp%\getadmin.vbs" del /f /q "%temp%\getadmin.vbs"
exit
:Admin
echo 成功取得Administrator权限
C:\Users\zps\Desktop\test\instsrv.exe CecWlwServerTest3 C:\Users\zps\Desktop\test\srvany.exe
reg add HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\CecWlwServerTest3\Parameters
reg add HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\CecWlwServerTest3\Parameters /v AppDirectory /d "E:\batTest\package" /t reg_sz /f
reg add HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\CecWlwServerTest3\Parameters /v Application /d "E:\batTest\package\runServer.bat" /t reg_sz /f
enter image description here
When I close this windows service, the corresponding process is not closed.
activeMq and redis processes are still in progress.
enter image description here
enter image description here
How can I close the process started by the service at the same time when I close the service

Related

Batch file that deletes itself and folder that contains it

I'm trying to do the following but deleting the downloaded folder which contains the batch file fails:
NOTE: All exe's, apps, batch file etc. are contained in file.zip.
User downloads file.zip to any directory and unzips.
User runs an exe which is located in the unzipped folder.
This in turn runs two portable apps and some other things.
Once duties are performed, I remote in and run the same exe but this time I select an option that runs a batch file (located in unzipped folder) that starts a 30 second timer then is supposed to stop the apps and delete file.zip and the unzipped folder including the batch file itself.
Below is the batch file:
#echo off
mode con: cols=32 lines=7
color 4f
title
echo 30 Second Delay
echo Close window to abort
echo/
echo/
echo 0%% 100%%
SET /P var= <NUL
set count=0
:loop
PING -n 2 127.0.0.1 >NUL 2>&1
call :printline _
set /a count=count+1
if %count%==30 goto finish
goto loop
:printline
REM Print text passed to sub without a carriage return.
REM Sets line variable in case %1 intereferes with redirect
set line=%1
set /p var=%line%<NUL
exit /b
:finish
cls
color 0f
title Finished
mode con: cols=80 lines=25
echo Do NOT close this window!
echo/
echo Killing processes...
echo/
echo/
echo/
taskkill /t /f /im app1mainprocess.exe >nul
timeout /t 5 >nul
taskkill /t /f /im app2mainprocess.exe >nul
timeout /t 5 >nul
echo Do NOT close this window!
echo/
rem echo Restarting Windows Explorer...
rem timeout /t 10 >nul
rem taskkill /f /im explorer.exe >nul
rem start explorer.exe
echo Do NOT close this window!
echo/
echo Deleteing files and folders...
echo/
rem timeout /t 10 >nul
Set "Folder2Del=%~dp0"
cd ..
IF EXIST "file.zip" DEL "file.zip" /s /q >nul
rem echo %scrptDir%
echo Do NOT close this window!
echo/
echo Still working...
timeout /t 10 >nul
rd %Folder2Del% /s /q
(goto) 2>Nul & RD /S /Q "%Folder2Del%" & exit
The problem I encounter is that the folder never gets deleted. I realize my code is not correct but another reason is because one of the dll files in the unzipped folder is sometimes still in use by the dllhost.exe process.
I'm not sure if it is safe to add a line that kills the dllhost.exe process or not but my code still won't work because I have something wrong with how it deletes the batch file itself and the folder that contains it.
What lines do I need to edit and is it safe to kill dllhost.exe?
According to a link from dbenham
This does the trick:
#Echo off
Echo Ref: "http://www.dostips.com/forum/viewtopic.php?f=3&t=6491"
Set "Folder2Del=%~dp0"
cd "%~d0"
pause
(goto) 2>Nul & RD /S /Q "%Folder2Del%"
Take care the folder containing the batch is deleted
including any other files/folders without any further question!
Ok... I THINK I figured out how to do what I want by trying to delete the dll file, first, before trying to delete the entire directory. The code below looks for the problem dll and tries to delete it. If it still exists, it will try to delete the file every 30 seconds for up to 15 minutes. As soon as the dll gets deleted, the entire folder will also be deleted. If after 15 minutes the dll cannot be deleted, the remaining files in the folder will be deleted.
I still have a small issue. If I add code that kill/restarts Windows Explorer, the folder does not get deleted. Why and is there a workaround?
Below is the latest code:
#echo off
mode con: cols=32 lines=7
color 4f
title
echo 30 Second Delay
echo Close window to abort
echo/
echo/
echo 0%% 100%%
SET /P var= <NUL
set count=0
:loop
PING -n 2 127.0.0.1 >NUL 2>&1
call :printline _
set /a count=count+1
if %count%==30 goto finish
goto loop
:printline
REM Print text passed to sub without a carriage return.
REM Sets line variable in case %1 intereferes with redirect
set line=%1
set /p var=%line%<NUL
exit /b
:finish
cls
color 0f
title Uninstall
mode con: cols=80 lines=25
echo Do NOT close this window!
echo/
echo Killing processes...
tasklist /fi "imagename eq app1mainprocess.exe" |find ":" > nul
if errorlevel 1 taskkill /t /f /im "app1mainprocess.exe" > nul
tasklist /fi "imagename eq app2mainprocess.exe" |find ":" > nul
if errorlevel 1 taskkill /t /f /im "app2mainprocess.exe" > nul
timeout /t 5 >nul
rem echo Do NOT close this window!
rem echo/
rem echo Restarting Windows Explorer...
rem timeout /t 10 >nul
rem taskkill /f /im explorer.exe >nul
rem start explorer.exe
echo/
echo Deleteing file.zip if it exists...
timeout /t 5 >nul
Set "Folder2Del=%~dp0"
cd ..
IF EXIST "file.zip" DEL "file.zip" /s /q >nul
rem echo %Folder2Del%
rem Loops for 30 times in 30 second intervals (Total 15 minutes) to confirm deletion. Loop will exit after 30 loops and move on if dll cannot be deleted.
for /l %%i in (1,1,30) do (
del "%Folder2Del%name*.dll"
if not exist "%Folder2Del%name*.dll" goto Folder2Del
echo/
echo File locked! May take up to 15 minutes to delete.
echo Will stop trying 15 minutes after first attempt.
timeout /t 30 >nul
)
:Folder2Del
echo/
echo Attempting to delete the Connector folder and it's contents...
timeout /t 5 >nul
rd "%~dp0" /s /q & exit

Windows Service fails to start when the service is already running using windows batch script

I am writing a windows batch script that will Install a service. If its already running, then I am stopping and deleting the service and then installing the service again.
I am facing a peculiar behavior. When the service is already running, and when I am running the below batch file, the service gets installed again, but it fails to start. When I reboot the system, and start the service, it gets started.
How to solve this start service issue without rebooting computer. I also added taskkill /f /PID to kill the process libertyserver.exe.
This is my code : test.bat. I am running test.bat from command line.
test.bat:
#echo off
setlocal
#echo off
set JAVA_SERVICE_EXE=libertyserver.exe
...
....
set SERVICE_DISPLAY_NAME="%LIBERTY_SERVICE_NAME%"
set SERVICE_DESCRIPTION="%LIBERTY_SERVICE_NAME%"
echo Stopping and Deleting existing services
for /F "tokens=3 delims=: " %%H in ('sc query %SERVICE_NAME% ^| findstr "STATE" ') do (
if /I "%%H" EQU "RUNNING" (
sc stop %SERVICE_NAME% >nul 2>&1
timeout /t 30 /nobreak >nul 2>&1
)
sc delete %SERVICE_NAME% >nul 2>&1
timeout /t 30 /nobreak >nul 2>&1
)
for /f "tokens=2" %%a in ('tasklist^|find /i "libertyserver.exe"') do (taskkill.exe /f /pid %%a >nul 2>&1)
echo Installing liberty profile %SERVICE_NAME%
set INSTALL_SERVICE_COMMAND=%DAEMON_EXEC% //IS//%SERVICE_NAME% --Startup=manual --DisplayName=%SERVICE_DISPLAY_NAME% --Description=%SERVICE_DESCRIPTION% ++DependsOn=Tcpip --LogPath=%LOG_PATH% --StdOutput=auto --StdError=auto --StartMode=exe --StartPath=%SERVER_START_COMMAND_PATH% --StartImage=%SERVER_START_COMMAND% --StartParams=start#SIServer --StopMode=exe --StopPath=%SERVER_STOP_COMMAND_PATH% --StopImage=%SERVER_STOP_COMMAND% --StopParams=stop#SIServer
%INSTALL_SERVICE_COMMAND%
echo Installed liberty profile %SERVICE_NAME%

Reboot & continue batch script

Below is the script created for increasing the swaps & mounting the driver. Now in this script I want to add the feature that after setting the page file the system will be rebooted & once reboot is done, it will resume with next step which is mounting driver. Can you please help in this.
#echo off
wmic pagefileset create name="D:\pagefile.sys"
wmic pagefileset where name="D:\\pagefile.sys" set InitialSize=20480,MaximumSize=25480
echo "Pagefile created.
Need to add script to reboot the windows & after reboot continue with next step
DISKPART /s C:\Users\Desktop\param_files\instructions.txt
echo "Drive mounted successfully"
Regards
You could mark where you want to restart your script like:
#echo off
REM Initialization here
if "%~1" neq "" goto :%~1
REM Do some stuff1 here
call :markReboot stuff2
REM Making sure to not execute some part of stuff2 before rebooting
goto :eof
:stuff2
REM Do some stuff2 here
call :markReboot stuff3
goto :eof
REM ...
:stuffn
REM Do some stuffn here
goto :eof
:markReboot
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce /t REG_SZ /d "\"%~dpf0\" %~1" /v RestartMyScript /f
shutdown /r /t 0
NOTE: The /f is not really needed in the reg add command.
EDIT: Adapting my answer to your specific should look like:
#echo off
if "%~1" neq "" goto :%~1
wmic pagefileset create name="D:\pagefile.sys"
wmic pagefileset where name="D:\\pagefile.sys" set InitialSize=20480,MaximumSize=25480
echo "Pagefile created.
call :markReboot stuff2
goto :eof
:stuff2
DISKPART /s C:\Users\Desktop\param_files\instructions.txt
echo "Drive mounted successfully"
goto :eof
:markReboot
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce /t REG_SZ /d "\"%~dpf0\" %~1" /v RestartMyScript /f
shutdown /r /t 0

Using command prompt , How can I get the path where chrome exe is installed?

I am writing a batch file and I need to start chrome exe using that batch file . So for that I need to get the path of the directory where chrome is installed .
Usually it is installed in " C:\Program Files (x86)\Google\Chrome\Application" but some users change the path while they install chrome.
I will like to get the path of that chrome exe on run time using command prompt and start it from there.
The content of my batch file are : Where for the highlighted code I want to get on run time instead of hard coding it into my .bat file
(I took help of this to get admin rights.)
#echo
:: BatchGotAdmin
:-------------------------------------
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"=""
echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:gotAdmin
pushd "%CD%"
CD /D "%~dp0"
:----------------------https://stackoverflow.com/questions/1894967/how-to-request-administrator-access-inside-a-batch-file----------------
echo Closing all instances of chrome.....
taskkill /f /im chrome.exe
echo All instances of chrome closed. Now going to chrome exe location
cd **C:\Program Files (x86)\Google\Chrome\Application**
echo Reached to chrome exe location. Ready to start new chrome with web security off.
chrome.exe --user-data-dir="C:/Chrome dev session" --disable-web-security
Get the installed location of Google Chrome from the registry
"HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet\Google Chrome\shell\open\command"
"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Clients\StartMenuInternet\Google Chrome\shell\open\command"
Example:
set "Command="
for /f "tokens=2,*" %%A in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet\Google Chrome\shell\open\command" /ve 2^>nul') do set "Command=%%~B"
if not defined Command for /f "tokens=2,*" %%A in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Clients\StartMenuInternet\Google Chrome\shell\open\command" /ve 2^>nul') do set "Command=%%~B"
if not defined Command echo Google Chrome was not found.
if defined Command start "Browser" "%Command%"

Windows BATCH: How to disable QuickEdit Mode for individual scripts?

QuickEdit mode can be useful if you wish to quickly highlight and copy text directly from the command prompt instead of redirecting output to a file. However, it has its drawbacks. If you have a batch script running, selecting text in the console will pause the script execution until the text is deselected. This can be a problem if the script is expected to continue without pause.
How can one disable QuickEdit mode for certain BATCH scripts?
A way that will affect the current command prompt session.
Here's quickEdit.bat . It is a self-compiled .net script so it requires .net installed (not installed by default on Winsows XP/2003).
Usage:
Enable:
quickEdit 1
Disable:
quickEdit 2
Get State:
quickEdit 3
Already answered here, update "QuickMode" setting in Windows Registry:
reg add HKCU\Console /v QuickEdit /t REG_DWORD /d 0 /f
However it will not affect currently opened window. But you can reopen a window:
:: Get QuickEdit Mode setting from Windows Registry
FOR /F "usebackq tokens=3*" %%A IN (`REG QUERY "HKCU\Console" /v QuickEdit`) DO (
set quickEditSetting=%%A %%B
)
if %quickEditSetting%==0x1 (
:: Disable QuickEdit Mode
reg add HKCU\Console /v QuickEdit /t REG_DWORD /d 0 /f
:: Open script in a new Command Prompt window
start "" "%~dpnx0" %* && exit
)
... script logic here ...
exit
Additional info about HKEY_CURRENT_USER\Console Registry configuration - https://renenyffenegger.ch/notes/Windows/registry/tree/HKEY_CURRENT_USER/console/index
Unfortunately, there is no way to edit the QuickEdit setting of the current CMD Console instance from command line. We can, however, temporarily disable the global QuickEdit setting and start a new console instance. There are a couple ways to do this, each with its own perks (pros) and drawbacks (cons). Both of the following solutions require the ability to modify the registry.
REGEDIT
PRO: Compatible with any common Windows system
CON: Requires the creation of temporary REG files
Code (goes at the beginning of your script):
if exist "%TEMP%\consoleSettingsBackup.reg" regedit /S "%TEMP%\consoleSettingsBackup.reg"&DEL /F /Q "%TEMP%\consoleSettingsBackup.reg"&goto :mainstart
regedit /S /e "%TEMP%\consoleSettingsBackup.reg" "HKEY_CURRENT_USER\Console"
echo REGEDIT4>"%TEMP%\disablequickedit.reg"
echo [HKEY_CURRENT_USER\Console]>>"%TEMP%\disablequickedit.reg"
(echo "QuickEdit"=dword:00000000)>>"%TEMP%\disablequickedit.reg"
regedit /S "%TEMP%\disablequickedit.reg"
DEL /F /Q "%TEMP%\disablequickedit.reg"
start "" "cmd" /c "%~dpnx0"&exit
:mainstart
REG
PRO: Does not require creation of temp files
CON: Not available on Windows 2000 and earlier without Resource Kit
CON: Different versions have different syntax (accounted for in code below)
Code (goes at the beginning of your script):
set reg50=::&set reg51=::&(reg /?>nul 2>&1 && set reg51=)
if %errorlevel%==5005 set reg50=
set qkey=HKEY_CURRENT_USER\Console&set qprop=QuickEdit
%reg51%if defined qedit_val (echo y|reg add "%qkey%" /v "%qprop%" /t REG_DWORD /d %qedit_val%&goto :mainstart)
%reg50%if defined qedit_val (reg update "%qkey%\%qprop%"=%qedit_val%&goto :mainstart)
%reg51%for /f "tokens=3*" %%i in ('reg query "%qkey%" /v "%qprop%" ^| FINDSTR /I "%qprop%"') DO set qedit_val=%%i
%reg50%for /f "tokens=3*" %%i in ('reg query "%qkey%\%qprop%"') DO set qedit_val=%%i
if "%qedit_val%"=="0" goto :mainstart
if "%qedit_val%"=="0x0" goto :mainstart
%reg51%echo y|reg add "%qkey%" /v "%qprop%" /t REG_DWORD /d 0
%reg50%if "%qedit_val%"=="" reg add "%qkey%\%qprop%"=0 REG_DWORD
%reg50%if "%qedit_val%"=="1" reg update "%qkey%\%qprop%"=0
start "" "cmd" /c set qedit_val=%qedit_val% ^& call "%~dpnx0"&exit
:mainstart
If you have another solution, feel free to post.
Slight update for option 1 that worked for me, that doesn't run it twice, on Win10, thanks.
if exist "c:\temp\consoleSettingsBackup.reg" regedit /S "c:\temp\consoleSettingsBackup.reg" & DEL /F /Q "c:\temp\consoleSettingsBackup.reg" & goto START
regedit /S /e "c:\temp\consoleSettingsBackup.reg" "HKEY_CURRENT_USER\Console"
reg add "HKCU\Console" /v QuickEdit /t REG_DWORD /d 0 /f
start "" "cmd" /c ""%~dpnx0" & exit"
exit
: START
rem your commands\scripts here
exit
quickedit.bat
::: TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAwAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5vdCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAAClmdnY4fi3i+H4t4vh+LeL4fi2i+T4t4si9+qL4vi3i7Xbh4vg+LeLUmljaOH4t4sAAAAAAAAAAAAAAAAAAAAAUEUAAEwBAQBvnfBjAAAAAAAAAADgAA8BCwEG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==
#setlocal disabledelayedexpansion enableextensions
#echo off
if not exist quickedit.exe (
>quickedit.b64 (
for /f "delims=: tokens=1" %%# in ('findstr "^:::" "%~f0"') do echo %%#
)
certutil -f -decode quickedit.b64 quickedit.exe >nul
del /f /q quickedit.b64
)
quickedit.exe %*
on: quickedit 1
off: quickedit 0

Resources