I just created a console application that will be started by a Job on a daily basis. This is a proof of concept. My main aim is to check for some records on a DB. This proof is just an console application that creates a file in the HD with a timestamp on it. Like this
class Program
{
static void Main(string[] args)
{
WriteToFile();
}
static void WriteToFile()
{
StreamWriter SW;
DateTime invocationTime = DateTime.Now;
SW = File.CreateText("c:\\HelloJobOutput\\MyTextFile"+invocationTime.Millisecond.ToString()+".txt");
SW.WriteLine("I was created at:" + invocationTime.ToString());
SW.Close();
}
}
The job is uncategorized, because I don't know what it means, I'm really new to job scheduling. There is one only step in the job of type Operating System cmdExec and its command is the full path to the executable file of the console application, and scheduled to run each 10 seconds the whole day. on success it quits reporting success.
What's wrong? Well, I start the job and it start running well, files are created and timestamps are OK, then I close the job execution dialog windows and the files continue to be created each 10 seconds or so. So I go to Management studio and stop the job but It throws an error saying it cannot be stopped because it is not running.
[nkvu - moving this out of the comments as it's perhaps getting to detailed/not readable in there. Also, I'll do my best to explain but happy to have corrections/edits as I am not expert.]
The SQL Server Agent is a separate Windows service which runs the jobs you define if they are not disabled and have an active schedule (e.g. every 10 seconds in your case). Because it runs as a Windows service and is, for this purpose, independent, you don't need any manual intervention to get your job running - SQL Server Agent will check the active jobs and their schedules, and start jobs automatically as appropriate (in this case every 10 seconds).
The key thing here is that if a job has an active schedule it does not mean that it is running, just that it is capable of running. If we trace through the steps (I have put a description of the "state" the job is in next to each step):
In the above, your job is only actually considered running in that 5 second interval in Step 3 and will only report as running for that 5 seconds. Also, that is the only time period when the Stop
option through Management Studio will do anything effective.
Also, what the Start
option does it not start your schedule running but it is there if, for example, your DBA needs to run a job as a once-off outside its normal/regular schedule (or if there is a job which only needs to be run manually and on rare occasions). As a scenario, lets say I have a job which runs once a night @ 11pm to dump the contents of a table to a CSV file for another application to pick up but I come in the next morning (err, there is no monitoring in this scenario) and see that the CSV file did not get created...I can then go to the job and manually run it to create the CSV.
If your DBA needs to :
Stop
. Hope that helps.