Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server 2008 Running Procs: How to get the xp_cmdshell items?

I'm using this query to find some queries that have been running for a long time:

SELECT sqltext.TEXT,
req.session_id,
req.status,
req.command,
req.cpu_time,
req.total_elapsed_time
FROM sys.dm_exec_requests req
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS sqltext
order by req.total_elapsed_time des 

Currently, we have some xp_cmdshell things that seem to be stuck (we use it to call bcp.exe for bulk exporting in jobs). However, the output of sys.dm_exec_sql_text() only outputs "xp_cmdshell" and not the parameters -- I really would like to see exactly what commands xp_cmdshell is running so I can track down the issues.

Is there any way to do that in SQL server?

EDIT: The active sessions are calling xp_cmdshell from a stored procedure. E.g.:

EXEC usp_xxx -> calls EXEC usp_yyy -> calls xp_cmdshell.

So, the output of DBCC InputBuffer is the call to usp_xxx which is not what I want.

like image 917
Jen A Avatar asked Jan 27 '26 20:01

Jen A


1 Answers

I'm not sure if the details are available in any of the dynamic management views, but you could take your session_id and use it with DBCC INPUTBUFFER to get the details you seek.

DBCC INPUTBUFFER(83)

would return something like this as an example

EventType           Parameters      EventInfo
--------------      ----------      ------------------------
Language Event      0               EXEC xp_cmdshell 'sc /h'
like image 175
Joe Stefanelli Avatar answered Jan 30 '26 09:01

Joe Stefanelli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!