Solved

End application using batch file

Posted on 2003-11-21
28
795 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
  • 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
 

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 83

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 83

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 83

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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 83

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 83

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 83

Accepted Solution

by:
oBdA earned 500 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 83

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 83

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Moving applications to the cloud or switching services to cloud-based ones, is a stressful job.  Here's how you can make it easier.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now