How to log the SQL Job failure to the Log Table? - sql

SQL Jobs are running from the SQL Agent. At this moment, we don't have the Email Notification configured for failure from the SQL Job.
I am considering to fetch the errors from the log table. Further then to have a stored procedure to generate the error report.
select top 10 * from msdb.dbo.sysjobhistory
How to log the SQL Job failure to the Log Table? What will happen, when a job is scheduled to run for every 5 mins and the error will get updated (or) error will get inserted as new record?

Follow these steps:
Open Properties of job
Go under Steps
Get into the step by pressing Edit button
Navigate under Advanced
Change the on Failure Action to Quit the job reporting failure
Then Check Log To table

I ran into the limitations of the built-in logs, job history, etc., where it doesn't capture when a step fails, but the job itself doesn't.
Based upon this sqlshack article, I built and implemented something very similar which maintains a permanent history of job/step failures, etc., even if you delete a job/step. This solution will even notify when this job fails.
Best of luck!

Related

How to kill a SQL Server job which keeps on showing an "In Progress" status?

I am using SQL Server 2014 and I have a job (let's call it JN5) scheduled to run at a specific time on a daily basis. The job runs an SSIS package and has 11 steps in it. Looking at the Job History in SSMS, the job seems to be stuck at Step 9. It is not showing as a failure but "In Progress".
I am having a hard time trying to stop it. I have tried the following:
USE msdb ;
GO
EXEC dbo.sp_stop_job N'JN5' ;
GO
I get the following error after execution:
Msg 22022, Level 16, State 1, Line 2
SQLServerAgent Error: Request to stop job JN5 (from User sa) refused because the job is not currently running.
Then, I tried the following: I restarted the SQL Server Agent through SQL Server Configuration Manager. When I viewed the SQL Job Status (after a Refresh) in SSMS, it was still showing "In Progress" !
My next attempt to solve the issue was to restart SQL Server 2014. I then checked the status again in SSMS. It was still showing the "In Progress" status!
I just had a look at the following on StackOverflow: SQL Server job activity (job failed but history records in job activity still showing 'running' status)
However, I am not sure if the above link relates to my problem.
Any ideas on what I can attempt next?
I encountered a similar issue recently. I had a job running, scheduled via SQL Server Agent Jobs, executing from the SSISDB Catalog. (ie a scheduled SSIS job).
During the job, one of the steps processes an SSIS task which used a connection manager to a file (a sql script). While this was running, someone applied some patches to the server and restarted the SQL services stack.
This resulted in the job looking like it was processing (via job history) and appeared to be running (via Integration Services dashboard)... but there was no longer any IO (and none of the SQL in the script had completed execution)...so I knew it was relatively safe to kill the job.
I got the same error:
Msg 22022, Level 16, State 1, Line 9 SQLServerAgent Error: Request to stop job XXXXX (from User YYYYY) refused because the job is not currently running.
So I referenced the ID of the operation from the Integration Services Dashboard, and issues the following commands:
USE SSISDB
GO
EXEC [catalog].[stop_operation] <OperationId>
This removed the running job from the Integration Services dashboard.
When I had this problem I could only solve it by deleting the job.
Before, I created the script to recreate the job. So I only lost the job history.

Pentaho Logging specify Job or Trans for each line

I am running Pentaho Kettle 6.1 through a java application. All of the Pentaho logs are directed through the java app and logged out into the same log file at the java level.
When a job starts or finishes the logs indicate which job is starting or finishing, but when the job is in the middle of running the log output only indicates the specific step it is on without any indication of which job or trans is executing.
This causes confusion and is difficult to follow when there is more than one job running simultaneously. Does anyone know of a way to prepend the name of the job or trans to each log entry?
Not that I know, and I doubt there is for the simple reason that the same transformation/job may be split to run on more than one machine, by more that one user, and/or launched in parallel in different job hierarchies of callers.
The general answer is to log in a database (right-click any where, Parameters, Logging, define the logging table and what you want to log). All the logging will be copied to a table database together with a channel_id. This is a unique number that will be attributed to each "run" and link together all the logging information that comes from all the dependent job/transformations. You can then view this info with a SELECT...WHERE channel_id=...
However, you case seams to be simpler. Use the database logging with a log_intervale of, say, 2 seconds and SELECT TRANSNAME/JOBNAME, LOG_FIELD FROM LOG_TABLE continuously on your terminal.
You can also follow a specific job/transformation by logging in a specific table, but this means you know in advance which is the job/transformation to debug.

SSIS Agent job keeps running as "inprogress"

I have a SSIS package that monitors a folder. This package will run continuously until it's terminated.
I want to schedule this using a SQL Agent job.This SQL Agent job will utilize two steps. It is kind of heart beat job to to make sure the SSIS package runs.
Step - 1 it checks whether the SSIS package is running. if running quit else step 2.
Step - 2 Execute the SSIS job. if OK then report success and quit else report failure and quit.
uses a daily schedule Mon-Fri every 4 hrs.
When I execute the SQL Job, it starts the SSIS package but the job keeps running and the job monitor and history shows it as "inprogress"
I had to close the job to come out of the dialog but in background the SSIS job is still running as expected.
Is this normal behavior ? Do I need to approach this in a different way ?
Appreciate any pointers or help on this.
Once the job has begun, the Start Jobs dialog box has no impact whatsoever on the running of the job itself - it exists solely to provide a monitoring window for you. Closing it will have no effect on the running job.
From other phrases in your question, I gather that you do not expect the job to ever 'finish' - therefore I would expect it to always show as In Progress unless it errors out or is stopped.
"This package will run continuously until it's terminated."
"The job keeps running and the job monitor and history shows it as in progress"

SQL Server Agent Job is not running

I have a job that is supposed to run every 11 AM and 8 PM. About two weeks ago, it started to not respect the schedule. The "fix" that I found was to start the job manually and then the job would restart respecting the schedule for a while but eventually the issue reappears.
The big problem is that there are no error message what so ever. If the job fails, I am supposed to get a notification Email which I do not. In the sql server agent logs and the Job history, there are no errors. In the job history, I can see clearly that the job skipped the schedule since there are no entries. It looks like it did not even start as if the running time had not arrived.
The schedule is set to run everyday and there are no limits on how long it is supposed to run. The sql Agent is set to restart automatically if it stops unexpectedly.
Did anyone get this problem before?
Check the user which is used to run the job. Maybe the user password is expired or the user itself is no longer active.

how to record both success and fail in SQL Server Agent Job

I am using SQL Server 2008 and I am writing a SQL Server Agent Job. In my Job, there are 3 steps and in each step I will execute a store procedure. I noticed that in default Notification setting, it write information to Windows event log only when it fails.
My question is, any easy solutions to write both success and fail information of each step to windows event log or somewhere to easily monitored? My job runs daily and I need the information for both success and fail to ensure my job runs.
thanks in advance,
George
The Notifications in the job properties also allows you to pick an action when the job succeeds! That button to the right is a dropdown, really - you can pick various outcomes, e.g. write to event log when it fails, and send an e-mail when it succeeds.
If you choose the "If job completes" option, I believe the action (write to event log, send e-mail etc.) will be triggered in both cases - success and failure.
Marc
PS: maybe this here helps?
http://msdn.microsoft.com/en-us/library/ms191130.aspx
If you want to notify an operator by e-mail, check E-mail, select an operator from the list, and then select one of the following:
* When the job succeeds to notify the operator when the job completes successfully.
* When the job fails to notify the operator when the job completes unsuccessfully.
* When the job completes to notify the operator regardless of completion status.