sub get_running_job_info { my ($dbh) = @_; $dbh->do( <<'END_TEMP_TABLE' ); CREATE TABLE #tmpy ( job_id UNIQUEIDENTIFIER NOT NULL, last_run_date INT NOT NULL, last_run_time INT NOT NULL, next_run_date INT NOT NULL, next_run_time INT NOT NULL, next_run_schedule_id INT NOT NULL, requested_to_run INT NOT NULL, -- BOOL request_source INT NOT NULL, request_source_id sysname COLLATE database_default NULL, running INT NOT NULL, -- BOOL current_step INT NOT NULL, current_retry_attempt INT NOT NULL, job_state INT NOT NULL ) END_TEMP_TABLE ; # It's entirely possible this will break if run on some other version of MS SQL $dbh->do(q{INSERT #tmpy EXECUTE master.dbo.xp_sqlagent_enum_jobs 1, 'sa'}); my $sql = 'SELECT job_id,current_step FROM #tmpy WHERE running != 0'; my @job_info = @{$dbh->selectall_arrayref( $sql, {Slice=>{}} )}; for my $info_ref ( @job_info ) { my $job_id = $info_ref->{job_id}; $job_id =~ tr/a-z/A-Z/; $job_id =~ s/^(..)(..)(..)(..)(..)(..)(..)(..)(.{4})(.{12})$/$4$3$2$1-$6$5-$8$7-$9-$10/; my $name_sql = "SELECT [name] FROM msdb.dbo.sysjobs WHERE job_id = '$job_id'"; my ($job_name) = $dbh->selectrow_array( $name_sql ); $info_ref->{name} = $job_name // $info_ref->{job_id}; } return \@job_info; }