Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 08-28-2010, 12:01 PM   #1
Solid State Member
 
glnz's Avatar
 
Join Date: Nov 2009
Posts: 9
Default How do I write a script to run when PC shuts down?

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.
__________________

glnz is offline   Reply With Quote
Old 08-29-2010, 08:30 AM   #2
Bogan
 
Neodude112320's Avatar
 
Join Date: Feb 2006
Location: Australia
Posts: 8,471
Send a message via MSN to Neodude112320
Default Re: How do I write a script to run when PC shuts down?

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 ROOT DIRECTORY WHERE THE GOTOMYPC EXECUTABLE IS

taskkill /IM EXECUTABLE NAME /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 gotomypc.exe /F

Anyway, write the script in notepad and save it as a *.BAT file (FILENAME.bat)
__________________

__________________
Desktop:BitFenix Survivor~Gigabyte GA-P67A-UD3R~ i7 2600K @ 4hz~AMD 6870 1GB~12gb Team Elite DDR3 1333~3xWD In RAID5~W7 Ult
Laptop:Alienware M11x~ i7-2637M 1.7GHz~16GB 1333MHz DDR3~GeForce GT 540M~W7 Ult
Neodude112320 is offline   Reply With Quote
Old 08-29-2010, 08:54 AM   #3
Solid State Member
 
glnz's Avatar
 
Join Date: Nov 2009
Posts: 9
Default Re: How do I write a script to run when PC shuts down?

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.
glnz is offline   Reply With Quote
Old 08-29-2010, 09:05 AM   #4
Bogan
 
Neodude112320's Avatar
 
Join Date: Feb 2006
Location: Australia
Posts: 8,471
Send a message via MSN to Neodude112320
Default Re: How do I write a script to run when PC shuts down?

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.
__________________
Desktop:BitFenix Survivor~Gigabyte GA-P67A-UD3R~ i7 2600K @ 4hz~AMD 6870 1GB~12gb Team Elite DDR3 1333~3xWD In RAID5~W7 Ult
Laptop:Alienware M11x~ i7-2637M 1.7GHz~16GB 1333MHz DDR3~GeForce GT 540M~W7 Ult
Neodude112320 is offline   Reply With Quote
Old 08-29-2010, 02:11 PM   #5
Solid State Member
 
glnz's Avatar
 
Join Date: Nov 2009
Posts: 9
Default Re: How do I write a script to run when PC shuts down?

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!
glnz is offline   Reply With Quote
Old 08-29-2010, 03:43 PM   #6
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: How do I write a script to run when PC shuts down?

Quote:
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.

Quote:
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.

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

Quote:
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.

Quote:
Is there a command to tell the PC to bring me a scotch and soda?
Code:
while(methirsty) {
    fetchScotch();
    fetchSoda();
}
__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Old 08-30-2010, 09:13 AM   #7
Bogan
 
Neodude112320's Avatar
 
Join Date: Feb 2006
Location: Australia
Posts: 8,471
Send a message via MSN to Neodude112320
Default Re: How do I write a script to run when PC shuts down?

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
__________________
Desktop:BitFenix Survivor~Gigabyte GA-P67A-UD3R~ i7 2600K @ 4hz~AMD 6870 1GB~12gb Team Elite DDR3 1333~3xWD In RAID5~W7 Ult
Laptop:Alienware M11x~ i7-2637M 1.7GHz~16GB 1333MHz DDR3~GeForce GT 540M~W7 Ult
Neodude112320 is offline   Reply With Quote
Old 08-30-2010, 10:03 AM   #8
Solid State Member
 
glnz's Avatar
 
Join Date: Nov 2009
Posts: 9
Default Re: How do I write a script to run when PC shuts down?

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.
__________________

glnz is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 01:59 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0