Computers & Electronics

Automatic conditional start/stop Windows service?

  • Last Updated:
  • Sep 24th, 2015 12:21 pm
Tags:
None
Deal Guru
User avatar
Oct 24, 2012
11641 posts
2620 upvotes
Montreal

Automatic conditional start/stop Windows service?

My computer runs a CCTV monitoring software, when it is monitoring all 5 cameras, it uses around 30% CPU.

When I play CPU intensive games, that 30% makes the difference between smooth and not so smooth.
I tried making a schedule in the CCTV monitoring program to cut down to 2 cameras during the hours that I am home, but it still uses too much processing power.

Right now, I use "net stop servicename" in CMD before and after playing a game. I don't mind the service being off for the odd hour or so that I play. I might miss someone coming to my door or whatever, but nothing major.

However, I still want it to run when I am not playing games, so I have to remember to restart the service, which has already happened that I forgot.

I can write a batch script that would run in a loop 24/7, but I wonder if there isn't a more graceful way to automate the process.

Note - I tried setting the service in low priority mode, but it still took away enough processing power to make a difference.



///////////////////////////////////////////////////////
UPDATE --------- SOLUTION FOUND
//////////////////////////////////////////////////////
Ok, so for the sake of future people googling for answers, here is my solution.

I used Eventghost (google it) to detect the start/stop of my game (The log will show "create" and "terminated" events with the .exe name)
Without any special configuration, Eventghost already logs the activation and deactivation of any program.
You can tell Eventghost to do something when it detects a specific log event.

Now, in most situations, you can just tell Eventghost to run a command line, however, in my case, I needed Eventghost to run the command with administrative priviledges.


So I followed this guide

http://www.sevenforums.com/tutorials/11 ... reate.html

In my case, I wanted to stop the service named "blueiris" when the game started, and restart the service when I quit the game.

Step 3: I named the event as "gamestart"

Step 8: I replaced their example [ /c start "CCleaner" "C:\Program Files\CCleaner\CCleaner64.exe" ] with: /c start "gamestart" "%windir%\system32\net.exe stop blueiris]


I repeated step 1-12 to create a second event named "gamestop" and used the command line: /c start "gamestart" "%windir%\system32\net.exe start blueiris


I stopped the guide at step 12 because I didn't need to create a desktop shortcut.
Instead, I went in eventghost and told it to run a command based on whether it dedected the game lauching, or the game stopping.

So, when eventghost detects the game launching, it will run the command: schtasks /run /tn "gamestart"

When the game starts, it will run the command: schtasks /run /tn "gamestop"


It's complicated, but now that I learned to do this, I can see many other applications to this technique.
Anyway, I was already using Eventghost for years to send commands from my IR USB remote directly to KODI.
15 replies
Deal Fanatic
User avatar
Mar 20, 2009
8862 posts
2693 upvotes
Vancouver
Most programs will auto-start their associated services if you leave the service in Manual mode, but I assume you already tried that with this program.

Presumably you shut down the monitoring program when you run a game and restart it after. Why don't you just replace the program launch icon with a batch file icon that does "net start servicename" and then runs the program?
Deal Guru
User avatar
Oct 24, 2012
11641 posts
2620 upvotes
Montreal
JamesA1 wrote: Most programs will auto-start their associated services if you leave the service in Manual mode, but I assume you already tried that with this program.
The service does auto-start, but only when the computer boots up. Once it's running, it runs 24/7.... until I stop it to play a game, at which point I have to remember to re-start the service once I finish playing a game.
JamesA1 wrote: Presumably you shut down the monitoring program when you run a game and restart it after. Why don't you just replace the program launch icon with a batch file icon that does "net start servicename" and then runs the program?
Restarting the service is pretty simple already, my issue is remember to do so. I frequently stop playing a game because I am interrupted (wife wants something, kids want something), I don't always think of restarting the service before leaving the desk, especially since the game takes a good 10-15 seconds to shut down.
Deal Addict
Sep 30, 2004
1650 posts
196 upvotes
Ottawa
Why not make a Task Scheduler event using the On Idle trigger condition?
Deal Addict
User avatar
Dec 3, 2003
2366 posts
398 upvotes
Edmonton
I think JamesA1's solution might work in a simple batch file.

i.e.:
net stop servicename
game.exe
net start servicename

So when you quit your game the service gets started back up.
Definition: Than
A conjunction used to introduce the second element of an unequal comparison: She had better grammar than I.
Deal Fanatic
User avatar
Mar 20, 2009
8862 posts
2693 upvotes
Vancouver
alkizmo wrote: The service does auto-start, but only when the computer boots up. Once it's running, it runs 24/7.... until I stop it to play a game, at which point I have to remember to re-start the service once I finish playing a game.
But there's an associated program that uses the service, isn't there? If you set the service to Manual start, it won't autostart with Windows, but a program that uses it would probably start it.
Deal Guru
User avatar
Oct 24, 2012
11641 posts
2620 upvotes
Montreal
JamesA1 wrote: But there's an associated program that uses the service, isn't there? If you set the service to Manual start, it won't autostart with Windows, but a program that uses it would probably start it.
No, the service runs by itself.

There is a management program, which would indeed launch the service if said program was running. However, I only run the program when I want to view my cameras or view recordings. Even if the program would run in the background, I'd have to shut it down for the same reason that I stop the service to play games.

Vacillator wrote: net stop servicename
game.exe
net start servicename

It wouldn't work for the type of game I play.

Battlefield 4, online, requires to be launched from the Battelog webpage.
enko wrote: Why not make a Task Scheduler event using the On Idle trigger condition?
I'd have to try that, or see what other conditions exist in Task Scheduler. I'd have to see what is "idle" as it might not ever happen on my computer, or at least, not every time I leave the game.

However, I might find something suitful in Task Scheduler. I forgot about that little tool.
Deal Addict
Sep 30, 2004
1650 posts
196 upvotes
Ottawa
You could also just make a Powershell cmdlet to see if the BF4 process is running. If not, start your service. Just schedule that to run every 30 minutes or whatever suits your needs.
Deal Guru
User avatar
Oct 24, 2012
11641 posts
2620 upvotes
Montreal
enko wrote: You could also just make a Powershell cmdlet to see if the BF4 process is running. If not, start your service. Just schedule that to run every 30 minutes or whatever suits your needs.
Yeah I already had the idea of running a batch (powershell, whatever, same sort of thing :razz :) , but I didn't like to let it run in a loop.

However, making it run every few minutes through task scheduler sounds like a winning combo (If task scheduler can't figure out the condition by itself).

I can't test all these suggestions as I am at right ATM. Normally, we'd have probably ruled out the suggestions, or found the solution by now :razz:
Deal Expert
Mar 23, 2004
35606 posts
18999 upvotes
Is there a process associated with the service? There should be.... What happens if you set the process priority to "Low" and try just running the game with it still running? Does that help any?
Deal Guru
User avatar
Oct 24, 2012
11641 posts
2620 upvotes
Montreal
ES_Revenge wrote: Is there a process associated with the service?
Yes
ES_Revenge wrote: What happens if you set the process priority to "Low" and try just running the game with it still running? Does that help any?
I tried that, it didn't help much. I think it's because of the time sensitive nature of the process, though I might try to set the game's process to high, but that might make the rest of my computer act funky.
Deal Expert
Mar 23, 2004
35606 posts
18999 upvotes
alkizmo wrote: I tried that, it didn't help much. I think it's because of the time sensitive nature of the process, though I might try to set the game's process to high, but that might make the rest of my computer act funky.
Nah only realtime will cause issues with other stuff, but if setting the CCTV process to low didn't do anything for you, I'm guessing that route is not going to get you anywhere anyhow.

Probably time to get an i7 59x0K/X ;) Either that or live with having to end/re-start the service all the time. I'm guessing you've already looked for programs and utilities that would do what you want?
Deal Guru
User avatar
Oct 24, 2012
11641 posts
2620 upvotes
Montreal
ES_Revenge wrote: Either that or live with having to end/re-start the service all the time. I'm guessing you've already looked for programs and utilities that would do what you want?

I googled a bit and found nothing.

I was bored at work so I made this thread, I will probably explore Task Scheduler tonight.

Worst case scenario, I make that batch script I talked about in the first post and make Task Scheduler run it every 15 minutes in the background instead of looping the script.

I'm not going to buy a new CPU (and motherboard) just for an inconvenience :razz:
Deal Fanatic
User avatar
Mar 20, 2009
8862 posts
2693 upvotes
Vancouver
alkizmo wrote: I was bored at work so I made this thread
Careful, your employer might try a task scheduler that kick starts idle workers.
Deal Guru
User avatar
Oct 24, 2012
11641 posts
2620 upvotes
Montreal
JamesA1 wrote: Careful, your employer might try a task scheduler that kick starts idle workers.
Yes, it's called variable workload.

Some days I will work my ass off while others, like today, I get to relax and clean up my inbox and shop online.

My employer has no issue with it.
Deal Guru
User avatar
Oct 24, 2012
11641 posts
2620 upvotes
Montreal
Solution posted in the original post.

Bump for win

Top

Thread Information

There is currently 1 user viewing this thread. (0 members and 1 guest)