问题
I have a background process running, it can be seen in task Manager while its running. I need to check from my database, whether the process is running or not I have tried the following query
select * from sys.dm_os_threads
select * FROM sys.dm_os_tasks
But it doesnt show me all process running on the system
回答1:
I have a background process running, it can be seen in task Manager while its running. I need to check from my database, whether the process is running or not
I hope you are talking about background SQL Server process. Below queries only show information related to SQL Server
All processes running on SQL server can be seen by querying DMV
select * from sys.dm_exec_requests
select * from sys.dm_exec_sessions
select * from sys.dm_exec_connections
You can read books online about the documentation about these DMV's
回答2:
use this query
select
tblSysProcess.cmd
, tblSysProcess.physical_io IOUsage
, tblSysProcess.cpu as CPUusage
, tblSysProcess.waittype as waitType
, tblSysProcess.waittime as waitTime
, tblSysProcess.lastwaittype as lastWaitType
, tblSysProcess.waitResource as waitResource
, tblSysProcess.dbid as databaseID
, case
when (dbid > 0) then db_name(dbid)
else ''
end as dbName
, memusage as memUsage
, status
from master.dbo.sysprocesses tblSysProcess
where
(
--background process
(tblSysProcess.spid < 50)
)
回答3:
select db_name(dbid),cmd,spid,status
from
sys.sysprocesses
where
db_name(dbid) = 'dbname' and status = 'background'
回答4:
So it looks like you're not trying to see a SQL server process but another process on the machine for another application, none of the built in DMVs or functions will be able to show you this information natively. You have a couple of options, one is to write a CLR stored proc or function to check for the process, the other is to use xp_cmdshell to get the tasklist, you can do that like this:
EXEC sp_configure 'show advanced options', 1
RECONFIGURE
GO
EXEC sp_configure 'xp_cmdshell', 1
RECONFIGURE
GO
EXEC xp_cmdshell 'tasklist.exe'
GO
EXEC sp_configure 'xp_cmdshell', 0
RECONFIGURE
That will list out all the running processes on the machine, you could shove that data into a table if you like for further analysis if required, or you can play with the tasklist.exe parameters to just look for the process that you are interested in. Just remember to turn off xp_cmdshell whenever you don't need it as it can be a security risk.
回答5:
Try This;
EXEC sp_who1
EXEC sp_who2
SELECT *
FROM MASTER..sysprocesses
SELECT *
FROM sys.dm_exec_sessions
WHERE is_user_process = 1
来源:https://stackoverflow.com/questions/28493122/check-background-process-from-sql-server