Avatar of Trevor Mifsud
Trevor Mifsud
 asked on

How to check if a service is running and stop a shutdown via a script

Hi Experts,

Im having a bit of an issue with both my servers. One is running SBS 2003 and the other is running server 2008 standard.

Both are running StorageCraft ShadowProtect backup software.  We are also backing up our servers off-site into a backup data centre.

The problem i am having, is when there is a windows update, or a power outage the server reboots or shuts down.

With ShadowProtect, if the backup job is not completed successfully or aborted from the console. It causes the system to do its next backup via VDIFF. This is my problem, when this happens the back goes from taking seconds or minutes, to days.

What i am looking for is a script that will allow me to check and see if the state of the shadowprotect backup is in the process of running and if so, execute an abort so once it is aborted the server can continue to shutdown as it normally would.

Like this once the server comes back online, it wont know any difference and just send the next increment over to the data centre.

I know this solution may be made of a few different solutions, but everyone who helps, thank you in advance.

Windows Server 2003Windows Server 2008Active Directory

Avatar of undefined
Last Comment
Trevor Mifsud

8/22/2022 - Mon

in a  simple batch script....

Depending on how your abort sequence is, or if you just need to kill the process....
tasklist | fin /i "yourservice.exe" && taskkill /f /im yourservice.exe

tasklist | fin /i "yourservice.exe" && start "" abortsequence.exe /switches

Open in new window

Trevor Mifsud

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.

Thats why I was hoping for more info, on the secondary command. If the service executable has the proper abort switches, that would be the safe way to go.  

And this would be better for a service ...

And the "fin" above should be "find"
tasklist | find /i "yourservice.exe" && net stop "yourservice"

Open in new window


You can use the following vb script.
On Error Resume Next
Dim strServiceName
Dim strMachineList
strServiceName = "REPLACE_WITH_SERVICE_NAME" 'Service name should be in quotes
strComputer = "." 'use . for local computer else provide a computer name

Dim objWMI : Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMI.ExecQuery("Select * from Win32_Service where Name = '" & strServiceName & "'")

For Each objService In colServiceList
	If objService.State <> "Stopped" Then
		WScript.Sleep 5000	
	End If	

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck

As was mentioned, i do not think that shutting down the service is going to help. What you probably need is an API call to shadowprotect backup program to check status and abort gracefully, or alternatively to set the state condition to what you want ( Do an incremental, not a VDIFF on next backup )
You may need to check their site to get info on their API or get an SDK if they have one.

I hope this helps !
Trevor Mifsud

great work.
Trevor Mifsud

Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Trevor Mifsud