Create and run SQL Server Agent jobs on Linux

SQL Server jobs are used to regularly perform the same sequence of commands in your SQL Server database. This topic provides examples of how to create SQL Server Agent jobs on Linux using both Transact-SQL and SQL Server Management Studio (SSMS).

For known issues with SQL Server Agent in this release, see the Release Notes.

Prerequisites

To create and run jobs, you must first install the SQL Server Agent service. For installation instructions, see the SQL Server Agent installation topic.

Create a job with Transact-SQL

The following steps provide an example of how to create a SQL Server Agent job on Linux with Transact-SQL commands. These job in this example runs a daily backup on a sample database SampleDB.

Tip

You can use any T-SQL client to run these commands. For example, on Linux you can use sqlcmd or Visual Studio Code. From a remote Windows Server, you can also run queries in SQL Server Management Studio (SSMS) or use the UI interface for job management, which is described in the next section.

  1. Create the job. The following example uses sp_add_job to create a job named Daily AdventureWorks Backup.

      -- Adds a new job executed by the SQLServerAgent service 
      -- called 'Daily SampleDB Backup'  
      CREATE DATABASE SampleDB
      USE msdb ;  
      GO  
      EXEC dbo.sp_add_job  
          @job_name = N'Daily SampleDB Backup' ;  
      GO
    
  2. Add one or more job steps. The following Transact-SQL script uses sp_add_jobstep to create a job step that creates a backup of the AdventureWlorks2014 database.

     -- Adds a step (operation) to the job  
     EXEC sp_add_jobstep  
       @job_name = N'Daily SampleDB Backup',  
       @step_name = N'Backup database',  
       @subsystem = N'TSQL',  
       @command = N'BACKUP DATABASE SampleDB TO DISK = \
       N''/var/opt/mssql/data/SampleDB.bak'' WITH NOFORMAT, NOINIT, \
               NAME = ''SampleDB-full'', SKIP, NOREWIND, NOUNLOAD, STATS = 10',   
       @retry_attempts = 5,  
       @retry_interval = 5 ;  
      GO
    
  3. Create a job schedule. This example uses sp_add_schedule to create a daily schedule for the job.

     -- Creates a schedule called 'Daily'  
     EXEC dbo.sp_add_schedule  
      @schedule_name = N'Daily SampleDB',  
      @freq_type = 4,  
      @freq_interval = 1,
      @active_start_time = 233000 ;  
    USE msdb ;  
    GO
    
  4. Attach the job schedule to the job. Use sp_attach_schedule to attach the job schedule to the job.

     -- Sets the 'Daily' schedule to the 'Daily AdventureWorks Backup' Job  
     EXEC sp_attach_schedule  
      @job_name = N'Daily SampleDB Backup',  
      @schedule_name = N'Daily SampleDB';  
     GO
    
  5. Assign the job to a target server. Assign the job to a target server with sp_add_jobserver. In this example, the local server is the target.

     EXEC dbo.sp_add_jobserver  
      @job_name = N'Daily SampleDB Backup',  
      @server_name = N'(LOCAL)';  
     GO
    
  6. Start the job.

     EXEC dbo.sp_start_job N’ Daily SampleDB Backup’ ;
     GO
    

Create a job with SSMS

You can also create and manage jobs remotely using SQL Server Management Studio (SSMS) on Windows.

  1. Start SSMS on Windows and connect to your Linux SQL Server instance. For more information, see Manage SQL Server on Linux with SSMS.

  2. Create a new database named SampleDB.

    Create a SampleDB database
  3. Verify that SQL Agent was installed and configured correctly. Look for the plus sign next to SQL Server Agent in the Object Explorer. If SQL Server Agent is not installed, see Install SQL Server Agent on Linux.

    Verify SQL Server Agent was installed

  4. Create a new job.

    Create a new job

  5. Give your job a name and create your job step.

    Create a job step

  6. Specify what subsystem you want to use and what the job step should do.

    Job subsystem

    Job step action

  7. Create a new job schedule.

    Job schedule

    Job schedule

  8. Start your job.

    Start the SQL Server Agent job

Next Steps

For more information about creating and managing SQL Server Agent jobs, see SQL Server Agent.