question

liton avatar image
liton asked

Batch file in sql server agent

Is it possible to run a batch file from sql server agent without using `xp_cmdshell`? I have tried using cmd.exe /c "c:\filename.bat" but it doesn't work.
sql-agent
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Fatherjack avatar image Fatherjack ♦♦ commented ·
>> it doesnt work Can you explain the problem please? Do you get an error?
0 Likes 0 ·
KenJ avatar image
KenJ answered
In your SQL Agent job step, make sure the step type is set to "Operating System (CmdExec)" Then enter your batch file name in the command window `C:\filename.bat` - you shouldn't need to include the cmd.exe /c part, but it shouldn't stop it from working, either.
4 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

liton avatar image liton commented ·
I tried that but it never ends the job. Start job status is success but the execute job status is "in progress" for a while. I ended up closing the job.
0 Likes 0 ·
ThomasRushton avatar image ThomasRushton ♦♦ commented ·
That could be done to the way your job script is written. How long does it take to run from a DOS box?
0 Likes 0 ·
liton avatar image liton commented ·
Only a minute. I'm just sending an email. I do receive the email but the job never ends. In other word, the job accomplishes the task but job hangs in "execute job". My orginal batch file will take a lot longer but I'm just testing it out with a batch file that sends email.
0 Likes 0 ·
KenJ avatar image KenJ commented ·
can you test it out with a batch that just writes a file, maybe to the sql server log directory? There might be something in the email batch file that the sql service account isn't able to do (permissions/mapped drives, etc) that works fine when you run the batch yourself.
0 Likes 0 ·
Grant Fritchey avatar image
Grant Fritchey answered
If you're running 2008 or better, you could try making the call through PowerShell.
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.