Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

End application using batch file

Posted on 2003-11-21
28
Medium Priority
?
809 Views
Last Modified: 2010-04-14
On a Win2000 SP4 Workstation, can a batch file be written and run on the local workstation to shut down an application? I can use a restart Windows command, and set Windows to automatically login, but prefer not to for security reasons.
0
Comment
Question by:esc_toe_account
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 18
  • 8
28 Comments
 

Author Comment

by:esc_toe_account
ID: 9799235
I want to close Outlook XP, if it is running.
0
 

Author Comment

by:esc_toe_account
ID: 9799460
(See also question http://www.experts-exchange.com/Applications/MS_Office/Outlook/Q_20805390.html Am trying to get a backup of the Outlook .pst file, which needs Outlook to be shut down first.)
0
 
LVL 9

Expert Comment

by:svenkarlsen
ID: 9799839
Yes,

if you're ok with advanced batch-coding, what you basically do is:

 - use TLIST -m [exe-filename] to get the PID of the Outllok XP
 - use KILL [PID] to stop the program

(don't be scared by kill, - it's gracefull as lon as you don't use -f (FORCE!))

Have a look at "FOR /F...:" in the OS help-file, - that will do it in one line!



Regards,
Sven




0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:esc_toe_account
ID: 9800043
That is a little advanced for me. I find this example for getting the PID of a running batch process:

for /f "Tokens=*" %%I in ('f:\tlist ^| grep %0 ^| grep CMD ^| awk "{ print $1 }"') do call :SETPID %%I
:next
.......
:SETPID
set MASTER_PID=%1
goto :next
.......

Could I edit this? The application is OUTLOOK.EXE

Thanks!
0
 
LVL 85

Expert Comment

by:oBdA
ID: 9800092
No real need for that construction.
kill.exe accepts a pattern as well as a PID. A simple
kill outlook.exe
should do the trick.
0
 

Author Comment

by:esc_toe_account
ID: 9800148
How would I call kill? I've tried a couple of ways without success
0
 

Author Comment

by:esc_toe_account
ID: 9800173
I've tried looking up the PID in Task Manager to get such as

Kill 1096

.. but that hasn't worked either.
0
 

Author Comment

by:esc_toe_account
ID: 9800208
I see this reference:

Windows NT/2000 does not come with a command-line 'kill' utility. You can get one in the Windows NT or Win2K Resource Kit.

So, I'm going to try Sysinternals PSKILL utility.
0
 
LVL 85

Expert Comment

by:oBdA
ID: 9800244
Stupid me. Let me guess, it tells you that the command can't be found.
Install the Support Tools; they're on the W2k installation CD (support\tools), but do NOT install them from there. Some of them get updated by Service Packs as well; so download (and install) the version matching your service pack's version:
Windows 2000 Service Packs
http://www.microsoft.com/windows2000/downloads/servicepacks/
0
 
LVL 85

Expert Comment

by:oBdA
ID: 9800264
PsKill should do the trick as well, but the Support Tools are worth installing anyway. tlist.exe comes with them as well, by the way.
0
 

Author Comment

by:esc_toe_account
ID: 9800303
I have SP4, but it's not clear to me how to install the support tools from there.
0
 

Author Comment

by:esc_toe_account
ID: 9800320
pskill OUTLOOK.EXE does give the Outlook application focus (the button depresses on the taskbar when all windows are minimized), but it doesn't end or change it in any way.
0
 

Author Comment

by:esc_toe_account
ID: 9800328
Ditto regardless of whether I use the PID (1096, for now) or OUTLOOK.EXE
0
 

Author Comment

by:esc_toe_account
ID: 9800333
The entire content of my test.bat file is:
------------------------------------------------------------
echo off
pskill OUTLOOK.EXE
cls
------------------------------------------------------------
0
 

Author Comment

by:esc_toe_account
ID: 9800349
PSKILL Usage:
------------------------------------------------------------------------
Running PsKill with a process ID directs it to kill the process of that ID on the local computer. If you specify a process name PsKill will kill all processes that have that name.
------------------------------------------------------------------------
usage: pskill [-?] [\\computer [-u username] [-p password]] <process name | process id>
    -? Displays the supported options.
   \\computer Specifies the computer on which the process you want to terminate is executing. The remote computer must be accessible via the NT network neighborhood.
    -u username If you want to kill a process on a remote system and the account you are executing in does not have administrative privileges on the remote system then you must login as an administrator using this command-line option. If you do not include the password with the -p option then PsKill will prompt you for the password without echoing your input to the display.
    -p password This option lets you specify the login password on the command line so that you can use PsList from batch files. If you specify an account name and omit the -p option PsList prompts you interactively for a password.
    process id Specifies the process ID of the process you want to kill.
    process name Specifies the process name of the process or processes you want to kill.
0
 

Author Comment

by:esc_toe_account
ID: 9800366
However, when I try it from a command prompt, it works fine.
0
 
LVL 85

Expert Comment

by:oBdA
ID: 9800391
It's not included in the Service Pack itself. The link was meant for you to go to "your" service pack site from the link above, then look a bit further down for the "Support Tools" download link.
For Service Pack 4, the direct link is here:
Windows 2000 SP4 Support Tools
http://www.microsoft.com/windows2000/downloads/servicepacks/SP4/supporttools.asp
0
 
LVL 85

Expert Comment

by:oBdA
ID: 9800418
Oh, and since this is in preparation for a backup, you might need the "-f" switch; if a user has an open/unsaved document (email or whatever), the application to be killed will ask whether to save the data, and it won't be shut down until this message box has been answered.
Test this before you apply it in earnest on a "life" system; I don't know how Outlook reacts to being shut down with open documents.
0
 

Author Comment

by:esc_toe_account
ID: 9800433
My error. Thanks! I have done so, and now the error message is: ||'||' is not recognized, etc. -- this is the same error I was getting with pskill

(where || is a nonstandard ASCII character)
0
 

Author Comment

by:esc_toe_account
ID: 9800447
It still works from a command prompt (as pskill did) and still does not work from a batch file.
0
 

Author Comment

by:esc_toe_account
ID: 9800474
.. and my existing batch files still work normally (successfully).
0
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 9800812
Hm? kill.exe should work in bat files as well. Try this little script, which will open three calculators, wait for a key and then close all calculators again.

====8<----[test.cmd]----
@echo off
echo Now starting the calculator ...
start "" calc
start "" calc
start "" calc
echo Hit any key to close any open calculator ...
pause >NUL
kill -f calc.exe
====8<----[test.cmd]----
0
 

Author Comment

by:esc_toe_account
ID: 9814242
1. It opens three calculators.

2. If I restore the focus to the command window, the command window closes when I hit a key.
0
 
LVL 85

Expert Comment

by:oBdA
ID: 9817786
I guess you are starting the command file using explorer? For debugging, it's easier to start it from a command prompt.
Anyway, you left out the most interesting part: did the calculators disappear after you hit a key?
0
 

Author Comment

by:esc_toe_account
ID: 9817879
No calculators close (I only described what DID happen, not what DIDN'T--sorry for the confusion). For example, if I don't restore focus to the command window, pressing a key simply operates on the last calculator open. (If an application has focus, this is expected behavior--right?--regardless of how it was opened?)

Works the same regardless whether run from:
  1. Command prompt (Start-->Run-->cmd or command/Enter)
  2. Explorer
  3. Start-->Run-->test.bat/Enter.

0
 
LVL 85

Expert Comment

by:oBdA
ID: 9817998
While I'm at it: Don't use "command" in NT. That's the "original"  DOS running in the NTVDM and only good for headaches, apart from the reduced functionality.
Just use "cmd" or %comspec%.
When you start it from the command prompt, is there any error output?
Have you tried providing the full path for kill.exe? It's probably something like this:
"C:\Program Files\Support Tools\kill.exe" -f calc.exe
0
 

Author Comment

by:esc_toe_account
ID: 10219701
Sorry for delay .. will finalize this soon .. the original problem PC was rotated out of service and has only just come back into service .. will try again.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
With the evolution of technology, we have finally reached a point where it is possible to have home automation features like having your thermostat turn up and door lock itself when you leave, as well as a complete home security system. This is a st…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question