taskkill has no effect when issued under scheduled task while user is logged off - taskkill

I have a batch file containing the command taskkill /f /im program.exe that kills all currently running instances of program.exe.
However, I want to kill program.exe by means of a scheduled task while the user initiating the scheduled task is logged off.
In this case, the batch file runs at the scheduled time but taskkill /f /im program.exe apparently has no effect. Other commands like running a program are not affected, however.
In short: How can I schedule to kill a program when the initiating user is deactivated by 'Switch user' or 'Lock' (i.e. logged off)?

Try to change the file extension of your batch file to '.cmd' instead of '.bat' and make sure your file is ANSI encoded instead of UTF-8 (the UTF-8 Byte Order Mark i.e. BOM could make your batch file invalid and throws an unrecognized character at position or line x error). I use Notepad++ for this kind of encoding checks / conversions.

Related

taskkill command to delete a program

A scheduled task runs every night to trigger a batch file. This batch file executes the application “AProgram.exe” with a configuration file to import data into a system. Occasionally this program fails to exit properly, leaving a “headless” process behind. When the scheduled task runs again the following night, the error below is produced:
[ERROR] PROCESS ALREADY RUNNING
We believe adding the command “taskkill” into this batch file should prevent this error from occurring again. Add the “taskkill” command with any required parameters into the batch file below to force “AProgram.exe” to end before it runs again, along with any other commands you deem necessary.
from dos prompt type : Taskkill /? this will explain all possible scenarios.
i think that the correct solution for your problem is to add in batch script :
TASKKILL /F /IM AProgram.exe /T
/F : For FORCE the end of task
/T : terminate all child process

Batch Script not Releasing after Execution

I have a batch script which performs file version controlling after a Backup event has taken place. This batch script, writing to a normal txt logfile, calls a PowerShell script to send this Logfile as an attachment with a success notification email. I have managed to release the writing lock on the log file, to allow PS to attach and send the file, but the Batch script does not stop after the entire sequence has been completed.
When I check the log file, I see that the shell instance has placed a 'Pause' in the script, instead of a self-termination (as it is instructed), and results in:
Press any key to continue... with a waiting shell
an app locked logfile, which won't allow the script to run again, unless the logfile is released.
This is the sequence of events:
The only Pause I have, is in < bak_send_exec.bat > - its sole purpose is to start a PS script:
PowerShell.exe -noprofile -executionpolicy bypass
If I remove it, the PS does not start. If I have it in there, the PS starts and executes flawlessly, but the logfile stays locked in a shell instances which is in Paused state, until someone kills the cmd.exe instances which locked the file.
This runs on a weekend at 01:00 am, so user intervention should not be required.
VC Script Summary:
This inter-connected batch files renames two identical files (in different locations) with timestamps. The timestamp is written to a variable for use in a notification email, which is sent using a PowerShell command. The entire process is logged to a txt log file (file overwritten when script runs again), and the log file is included with a Notification Email, mentioned earlier.
Script Calls:
Initial Start Command: Triggers the Version Control Procedures and Logs Progress with versioncontrol_post.bat > TSLog.txt 2>&1
versioncontrol_post.bat: Performs main procedure, then ends with CALL bak_send_exec.bat
bak_send_exec.bat: The suspected cause... Coding of entire file is three lines long, but required as mentioned earlier, for policy relaxation:
#ECHO OFF
PowerShell.exe -noprofile -executionpolicy bypass -file bak_send.ps1
PAUSE
bak_send.ps1: Performs main procedure to make a copy of the temporary log (TSLog.txt) to its final home, releases the TSLog file to work with the new duplicate of it, and continues to take that new duplicate and attach it to an email and sends email. The final line in the procedure is EXIT.
Fault finding tells me that the issue is not with the PowerShell script, but rather with the script that calls it. Taking out the PAUSE command results in the PowerShell not starting.
Does anyone have a possible solution to this "feature"?

Call multiple .bat from another .bat without waiting for one to finish

So, I want to make a script that will execute 2 .bat files and start some .exe files.
However, the .bat files are supposed to keep running.
I have something like this :
pushd tools\wamp64
start wampmanager.exe
pushd ..\..\server\login
call startLoginServer.bat
pushd ..\test
call startTestServer.bat
start "C:\DEV\P2\Test\client" P2.bin
The problem is that call startLoginServer.bat will not exit and therefore, I'm stucked here.
How can I run my 2 .bat files and let them keep running.
(Ideally, I want them to run in 2 different command prompt windows)
Also, there is probably a better way to handle relative path than using pushd if you can correct me on this.
Thanks
You could use:
start "Wamp Manager" /B /D "%~dp0tools\wamp64" wampmanager.exe
start "Login Server" /B /D "%~dp0server\login" startLoginServer.bat
start "Test Server" /B /D "%~dp0server\test" startTestServer.bat
start "Text Client" /B /D "%~dp0" "C:\DEV\P2\Test\client.exe" P2.bin
Run in a command prompt window start /? for help on this command explaining the options.
"..." ... title for new console window which is optional, but must be often specified on program to start is or must be enclosed in double quotes. The START command in last command line in batch file code in question interprets C:\DEV\P2\Test\client as window title. It is also possible to use an empty window title, i.e. "" which is best if the started application is a Windows GUI application on which no console window is opened at all.
/B ... run without opening a new window, i.e. in "background". This option can be omitted to see what the started applications and batch files output to console if the executables are not Windows GUI applications.
/D "..." or also /D"..." defines the directory to set first as current directory before running the command specified next. %~dp0 references the directory of the batch file containing these commands. This path always ends with a backslash. Therefore no backslash must be added on concatenating the directory of the batch file with a file or folder name or path.
Run in a command prompt window call /? for help on %~dp0 explaining how arguments of a batch file can be referenced from within a batch file.
See also the answer on How to call a batch file that is one level up from the current directory? explaining in total four different methods to call or run a batch file from within a batch file.
Finally read also the Microsoft documentations about the Windows kernel library function CreateProcess and the structure STARTUPINFO used by cmd.exe on every execution of an executable without or with usage of its internal command start. The options of start become more clear on having full knowledge about the kernel function and the structure used on Windows to run a program.

How do schedule a task in 1 hour using command prompt

I need some help setting up a batch file in Windows 7. I want the batch file to be able to create a scheduled task that would execute 1 hour from the moment I click it. I don't want to manually have to put the date and time in, just want it to schedule a task that will execute a set number of hours after I have run the batch file (I'm using 1 as an example).
Please can somebody help me out. I've been searching for an answer all day to no avail.
Recent versions of Windows come with a DOS utility called WAITFOR. Depending on how interactive you want your batch file, and whether it should run a single static command or run whatever you need at the time, you could easily make it work. Like for instance, you could create a batch file on your desktop and drag a program to it and drop it on the batch file. The first thing it would do is prompt for the number of minutes to delay, then it could run the program you dropped on it.
#echo off
setlocal enabledelayedexpansion
set /p _min=Enter the minutes to delay:
set /a _min*=60
waitfor /t !_min! delay
start "" %1
setlocal
Using the start command makes it possible to drop other things too, like a BMP or Word DOC. Anything that you can launch by double-clicking it from Windows Explorer should launch just fine.
After you enter the minutes to delay, just minimize the DOS window. It will close automatically after the delay and after it launches the program or file you dropped on the batch file.
invoke windows task scheduler directly from command line
schtasks /create /TN "Task Name" /TR script.bat /ST 18:00 /SD 21/03/2014 /SC ONCE

Killing inactive form session through oracle application server

I am trying use host command in Oracle forms. I get the process id as input from the user and on clicking ok the form should kill the session related to process id.
PS: Users will be entering only frmweb.exe process id which are inactive.
cmd := 'CMD /C taskkill /F /FI /pid 'process which is got as input' /IM frmweb.exe';
host(cmd)
I also tried by writing the above command into a .bat file in application server.When trying to execute the bat file it din work. But when tryin to run the bat file by double clicking in Application server the session was killed.
On executing the above I am unable to kill the process.
I would also like to know whether the host command was successful or not.
Could you please help me and guide me in proceeding. Orakill and alter session are working but I don want to use it.
I tried with writing the command execution to a text for debugging and was able to find the solution.
cmd := 'CMD /C taskkill /F /FI /pid 'process which is got as input' /IM frmweb.exe>>output.txt';
There was some special character written in at the end of the command due to which the command was failing when calling from Oracle Forms.Special character was due to a typo in the code the Oracle Forms.