Chapter 02 – Running Programs, Part IIn the first chapter we learned how to create a simple script, and to load that script as a standalone job.
We also learned how to add that same script to our master schedule to have it execute on-time.
In this chapter we’ll learn how to run a program (.EXE, .BAT, .CMD, etc..)
Fortunately, TaskGhost ships with several pre-made scripts that do exactly what we want.
Lets take a look at the CreateProcess.vbs script:
Sub main(commandline)
Dim exit_code
exit_code = TGCtrl.CreateProcess(0,commandline)
If exit_code = -1 Then
TGCtrl.Print(commandline & " failed to start!")
Else
TGCtrl.Print(commandline & " returned " & exit_code)
End If
End Sub
As you can see, this script takes one parameter, ‘commandline’ and feeds it to the CreateProcess function. (The CreateProcess.vbs script simply wraps the TGCtrl.CreateProcess() function for convenience.)
So. If we wanted to execute my_prog.exe, then we would call the CreateProcess.vbs from Schedule.vbs like this:
If TGCtrl.CheckTime (8, tc, "* * * * * * * *") = true Then TGCtrl.Run 0, "CreateProcess.vbs", "my_prog.exe"
Okay, so lets give this a try.
There isn’t really a program called; “my_prog.exe”, so we’ll use the ‘test’ program that ships with TaskGhost; “TG_TEST.EXE”.
Add the following line to TaskGhost\Sctipts\Schedule.vbs
If TGCtrl.CheckTime (8, tc, "* * * * * * * *") = true Then TGCtrl.Run 0, "CreateProcess.vbs", "tg_test.exe -return 2"As always, make sure that the job identifier (
8) is unique. Give it a new id if (8) is already in use.
Save Schedule.vbs and then
Load it.
After it runs once or twice, press the
Stop button.
You should see something like the following:
Tick 5:41:49 PM
tg_test.exe -return 2 returned 2
tg_test.exe -return 2 returned 2
STOP: Schedule.vbs exiting... If you see something similar to what’s displayed above, then you’ve successfully run the tg_test.exe program from a TaskGhost schedule.
In Part II of this chapter, we’ll learn a how TaskGhost finds programs to run, and a little bit about the tg_test program.