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.
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
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.
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
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
-- 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
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
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
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
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.
Start SSMS on Windows and connect to your Linux SQL Server instance. For more information, see Manage SQL Server on Linux with SSMS.
Create a new database named SampleDB.
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.
Create a new job.
Give your job a name and create your job step.
Specify what subsystem you want to use and what the job step should do.
Create a new job schedule.
Start your job.
For more information about creating and managing SQL Server Agent jobs, see SQL Server Agent.