0

I'm trying to run a xp_cmdshell to execute a program and thought I'd test it by opening notepad. However when I execute the command it just keeps on executing without returning results or opening notepad. Then, when I try to cancel the execution, the cancel just keeps running and won't cancel the execution.

I know how to stop the cancel , but how do I avoid this in the first place? What am I doing wrong in the execution?

DECLARE @command varchar(8000)
SET @command = 'C:\Windows\System32\notepad.exe' 
EXEC master..xp_cmdshell @command
ptownbro
  • 1,240
  • 3
  • 26
  • 44
  • `xp_cmdshell` continues only when whatever it is executing completes. In your case, either you quit or kill notepad – Squirrel Oct 11 '19 at 05:13
  • Thanks Squirrel. I understand how to kill the process. I'm trying to understand why this is happening. xm_cmdshell is said to start processes. I'm trying to figure out how to do it properly. – ptownbro Oct 11 '19 at 21:02
  • whatever you are doing with `xp_cmdshell` make sure it ends and exit – Squirrel Oct 12 '19 at 11:22
  • It opens notepad.exe using the Windows account under which SQL Server is running, so you won't see Notepad running - unless you happen to also be logged in to that server's console with the same Windows account. It never returns because there's no way for notepad.exe to terminate. You would have the same issue if you ran `xp_cmdshell 'cmd.exe /k echo "Hello"'` because cmd.exe would never exit due to the `/k` switch. – AlwaysLearning Oct 12 '19 at 11:57

0 Answers0