In the first chapter, we learned about job creation and scheduling.
In this chapter I wanted to talk about the different ways to launch (run) external programs.
As we’ve seen, the
-action directive can invoke internal actions or functions.
The
-command directive on the other hand, is used to invoke external programs like .EXE, .BAT, .CMD, etc..
Here is the list of the
-command directives:
(1) -command [ -wait ] [ -detach ] [-hide] program_name [arguments](2) -command -shell document_or_media_file(3) -command [ -wait ] -user -password [-profile] [-netcredentials_only] program_name [arguments]When executing an external program or batch file, you must decide how you want the program executed.
-command os_command [arguments]If you don’t pass any switches to the
-command directive, it will execute the command asynchronously.
What this means, is that WinCron will not wait around for the program to finish execution, but will instead continue executing the script beginning with the next line.
While this is a very powerful feature, you need to also be aware that this behavior can cause problems if the remaining statements in the job are dependant on the completion of the program just run.
Take for example this scenario:
# Move a File from location A to location B
# We implement a file move with a Copy/Delete pair.
{
# Start now
-start
# Copy the source file to the destination location
-command mycopy.exe c:\temp\foo.bar d:\snort\foo.bar
# Now delete the source file. This completes our ‘file move’
-command mydel.exe c:\temp\foo.bar
}
Now if you think about the implication of asynchronous execution, you will realize the the above job is flawed. The problem is that if WinCron Launches mydel.exe
before mycopy has finished, the file delete will fail. This is certainly not what was intended.
This fix is however easy:
# Move a File from location A to location B
# We implement a file move with a Copy/Delete pair.
{
# Start now
-start
# Copy the source file to the destination location and WAIT for it to complete
-command
-wait mycopy.exe c:\temp\foo.bar d:\snort\foo.bar
# Now delete the source file. This completes our ‘file move’
-command
-wait mydel.exe c:\temp\foo.bar
}
There are many instances where an asynchronous process spawn (launch) is desirable, but it is important to know when to use them and when not to.
Another switch supported is the
-detach directive.
The
-detach directive is especially useful for launching programs like perl.exe and others that create a console window (the black DOS windows.)
When you specify the
-detach directive in conjunction with the
-command directive, the process is spawned with special flags that tell the operating system not to create a console window.
Obviously, you don’t want to do this if the console window is likely to prompt the user for input, or if the output (text printed on the console) is needed.
Finally there is the
-shell directive.
This handy little switch instructs WinCron to use Windows ShellExecute function.
This function is interesting because while it will launch a normal .EXE file, it will also launch a .DOC, .HTML, .JPG or even a URL like
http://www.wincron.com !
You see, Windows looks at the argument as a
document type instead of an executable program.
Windows then looks up the program assigned to that document type in the Windows Registry and runs that program, passing to it the document you passed to
-command.
If this all seems complicated, maybe an example would help:
-command -shell http://www.wincron.comOkay, what do you think the above line does?
If you guessed ‘launched your browser and go to the specified URL’ you would be correct.
Lets try another:
-command -shell c:\windows\media\Chord.wavThis example would launch whatever program you have
registered to play .WAV files.
Actually, I use the above technique to make my scripts play sounds.
(Like when an exception occurs, I have the exception handler play a sound.)