Check if backup is already in progress There is SQL Agent Job which is doing backup of database and after backup there are several steps for delete database objects, creating some files, etc... Job is pretty much generic one, but I need some kind of check if it's there some backup of database which is running and if it's running how to implement some kind of WHILE loop, to wait until backup is finished and then to start this one. 

DECLARE @jobId BINARY(16)
EXEC @ReturnCode =  msdb.dbo.sp_add_job @job_name=N'DB_Backup', 
        @enabled=1, 
        @notify_level_eventlog=0, 
        @notify_level_email=0, 
        @notify_level_netsend=0, 
        @notify_level_page=0, 
        @delete_level=0, 
        @description=N'No description available.', 
        @category_name=N'[Uncategorized (Local)]', 
        @owner_login_name=N'sa', @job_id = @jobId OUTPUT
IF (@@ERROR &lt;&gt; 0 OR @ReturnCode &lt;&gt; 0) GOTO QuitWithRollback
/****** Object:  Step [Create Backup]    Script Date: 11/28/2016 1:41:01 PM ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Create Backup', 
        @step_id=1, 
        @cmdexec_success_code=0, 
        @on_success_action=3, 
        @on_success_step_id=0, 
        @on_fail_action=2, 
        @on_fail_step_id=0, 
        @retry_attempts=0, 
        @retry_interval=0, 
        @os_run_priority=0, @subsystem=N'TSQL', 
        @command=N'USE [DB]
GO
DBCC SHRINKFILE (N''DB_log'' , 2)
GO
DBCC SHRINKFILE (N''DB_log2'' , 0)
go
DBCC SHRINKFILE (N''DB'' , 2)
GO
USE MASTER
GO

BACKUP DATABASE [DB] TO  DISK = ''C:\DBbackup''
GO
'
You can customize this to your needs - (I agree that you should not be shrinking files as part of a scheduled maintenance task - Read these words of wisdom.)

DECLARE @SqlText VARCHAR(max)
DECLARE @BackupIsRunning BIT = 1 --Force initial value to 1

WHILE @BackupIsRunning = 1
BEGIN
    SELECT @SqlText = s.TEXT --Check for any BACKUP commands currently running
    FROM sys.dm_exec_requests r
    CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) s
    WHERE r.command IN (
            'BACKUP DATABASE'
            ,'BACKUP LOG'
            )

    IF @@ROWCOUNT = 0 --If @@ROWCOUNT = 0, then no backups are running
    BEGIN
        SET @BackupIsRunning = 0 --Drop out of WHILE loop

        BREAK
    END

    WAITFOR DELAY '00:00:10' --Take a break and check again
END