How do I write a script to run when PC shuts down?

glnz

Solid State Member
Messages
9
My office PC is WinXPPro SP3 with 2GB RAM, a Dell Optiplex 785 I think.

When it shuts down, it hangs, and I'm fairly certain that's because a particular running application, GoToMyPC, doesn't terminate well. (GoToMyPC lets me access the office PC from home or elsewhere.) So I'd like to run a script automatically when I shut down the PC to first turn off GoToMyPC before the rest of the PC shutdown continues.

One of the contributors here told me to go to (Start -> run -> gpedit.msc -> computer configuration -> windows settings -> scripts) as the place to put the script. OK, but I've never written a script, so what script should I write to exit from the GoToMyPC application nice and smooth?

Is there an app that writes the script for me just by watching my actions -- like recording a macro? If yes, could I "record" myself doing a manual exit from GoToMyPC as the script?

Thanks.
 
The best way i reckon would be to write a batch file to kill the Gotomypc process.

It would look something like this;

Code:
@echo off

chdir [B]ROOT DIRECTORY WHERE THE GOTOMYPC EXECUTABLE IS[/B]

taskkill /IM [B]EXECUTABLE NAME[/B] /F

So say the executable is in C:\Program Files\Gotomypc\ and the exe name is gotomypc.exe, the code would look like this:

Code:
@echo off

chdir C:\programs files\gotomypc\

taskkill /IM [B]gotomypc.exe[/B] /F


Anyway, write the script in notepad and save it as a *.BAT file (FILENAME.bat)
 
Dear Mr. dude: Great! Before I try this, should I add a couple of lines that exit from your script if, for some reason, the GoToMyPC process is NOT running when I shut down the PC? Maybe something like "IFERROR ... Quit" (which I remember only from WordPerfect's simple macro language)?

That way, I could put this script into the PC shut down process and never have to remember it.

Also, after your taskkill, do we need something like "WAIT 2 seconds"? I found that helpful when doing my WordPerfect macros ten years ago.

By the way -- dumb question -- what should I read to learn the basics about the scripting you are doing?

Thanks very much.
 
Good thing with batch files, if it errors it'll just close itself and once the commands are finished it will also close itself. So if GOTOMYPC isnt running, it will simple open,produce an error and then instantly close itself.

Batch files are pretty much DOS Commands. So you can just put DOS Commands into notepad and save the file as a BAT File, and voila you have a script. All @echo off does is make sure the commands dont appear in the DOS window, only the results.

If you want more info, you can read up on them here.
 
Dear dude: Don't think your advice is wasted on us noobies! I've been doing some research, inspired by your info and link. Have some follow-up Qs for you:

1) a) Taskkill doesn't seem to require going to the directory/folder where the application lives. I just confirmed that on my home PC, where taskmgr is running. I went to CMD, then did "chdir" to a couple of unrelated directories/folders, then typed "taskkill /im taskmgr.exe", and that works to turn off task manager wherever I am at the moment. Do you agree, or might I be missing something?

b) But since my office PC is on an office network, it would be safer to make sure I'm starting somewhere on my C:\ drive, JUST IN CASE my cmd starts in a network drive like I:\ or L:\. How do I do that in my batch file? Start with a line that says "chdir C:\" ?

2) I just checked the GoTOMyPC processes that are running on my office PC. There are at least 8 processes running at the moment (and right now I'm at home using GoToMyPC to look at my office PC so maybe there are fewer running when I'm at the office PC and not linked in from home). Fortunately, all these processes have names that start with "g2". Therefore, from what I can tell, the command could be something like
taskkill /fi "IMAGENAME eq g2*"
Do you agree?

3) Do you think I should also use the f/ switch to terminate FORCEFULLY? (As opposed to what? Politely?)

4) Is there a command to tell the PC to bring me a scotch and soda?

Thanks again!
 
Taskkill doesn't seem to require going to the directory/folder where the application lives.
If my long lost memories of it are holding true then this is correct - it doesn't.

b) But since my office PC is on an office network, it would be safer to make sure I'm starting somewhere on my C:\ drive, JUST IN CASE my cmd starts in a network drive like I:\ or L:\. How do I do that in my batch file? Start with a line that says "chdir C:\" ?
This won't have any affect whatsoever, I'm pretty sure taskkill doesn't take the drive it's executing from into account at all.

Therefore, from what I can tell, the command could be something like
taskkill /fi "IMAGENAME eq g2*"
Looks fine to me!

Do you think I should also use the f/ switch to terminate FORCEFULLY? (As opposed to what? Politely?)
Windows will "politely" tell programs to shut down when it shuts down anyway - the /f switch will try and force them shut (though be warned, sometimes even this doesn't work. I'm quite frankly astounded that Windows still can't guarantee to kill a program forcefully... heck I've never had kill -9 fail on *nix! Anyway...)
So yes, the /f switch is definitely required.

Is there a command to tell the PC to bring me a scotch and soda?

Code:
while(methirsty) {
    fetchScotch();
    fetchSoda();
}
 
berry120 summed it up quite nicely! The only reason why i Chdir is because i personally find it easier to do that when modifying scripts, so just personal preference really :)
 
Dude and Berry: Many thanks!

______ ADDED:

Dude and Berry: Well, now I'm trying it in the field (sitting at my office PC), and of course nothing is so simple. The .bat file works, but two issues:

1) Of the various running g2 applications, one of them, g2comm.exe, takes maybe 7 seconds to die. So, ideally, my batch file should wait 10 seconds after taskill before quitting to permit the PC shutdown to continue. How do I add "Wait 10 seconds" to the batch file? (Funny there's no direct Wait command in MS-DOS.)

2) So I ran my batch file and saw the g2 processes die. Then I hit Start - Shutdown and found that firefox asked me a question whether or not I wanted to Quit or Save & Quit, with the shutdown process waiting for my answer. Now, if I'd been at home, I wouldn't be able to answer because GoToMyPC would already be off, so the office PC would halt and not restart. I can add a taskkill firefox to my batch file, but is there a more global or elegant solution? (And taskkill firefox would cause Firefox to restart on its own when the PC reboots, which might not be desirable. And what if Firefox is not actually running to begin with - wil taskkill firefox hang?)

Thanks again.
 
Back
Top Bottom