Start/Stop Service when User logs in/out

3

I have a system user that is dedicated to my work. At work I need postgres, however I don't need it anywhere else. Is there a way to start the postgres service when the user logs in and stop it when i log out?

Note that the user has no admin priveleges and i currently cannot start the service with this user account. If I have to change it it's ok, but suboptimal.

edit

During work I switch the user from work user to my normal user account. When I do this postgres should continue to run.

Mene

Posted 2012-01-19T12:21:22.937

Reputation: 200

Answers

4

You could do this using Scheduled Tasks.

Use the On connection to user session trigger to run a batch script containing NET START [Service Name]

Use the On disconnect from user session trigger to run a batch script containing NET STOP [Service Name]

More info on Triggers here: http://technet.microsoft.com/en-us/library/cc748841(WS.10).aspx

You should be able to set the tasks to run under an account which has permissions to start/stop services so you won't need to elevate your user's privileges.

EDIT FOLLOWING COMMENTS AND UPDATE TO QUESTION:

Use the At log on trigger to run NET with START [Service Name] as the parameter

Use the On an event trigger (Custom settings, Event ID 4647, User initiated logoff, for the user) to run NET with STOP [Service Name] as the parameter

Shevek

Posted 2012-01-19T12:21:22.937

Reputation: 15 408

1Just as a sidenote: You don't need to create a batch script, just use NET as the program and START [Service Name] as parameters. – Mene – 2012-01-19T13:51:42.320

Also I used the "At log on" trigger. There's no "At log out" so I used the proposed disconnect trigger; didn't test the connect trigger. I guess both are working, but the connect/disconnect sounds more "symmetric". Wonder why there's no log off counterpart for the log in trigger – Mene – 2012-01-19T13:57:11.563

Just noticed that this is not working for me. I'd need an "At log off" trigger since I'm switching around during work and postgres should continue to work as long as I don't log off the work-user. – Mene – 2012-01-19T14:10:34.583

If you use User Switching then you will need to use the connect trigger as the disconnect is triggered when you switch away and the logon will not trigger when switching back. – Shevek – 2012-01-19T14:13:49.820

Yes, but the service will be stopped when I switch away, but it needs to be running. I usually switch when i run integration tests and I need the database for this to work. – Mene – 2012-01-19T14:19:44.583

It's a bit long winded but maybe use the On an event trigger to look for the log off event? but this will fire if either user logs off... – Shevek – 2012-01-19T14:28:29.373

That should work under my circumstances, thanks! – Mene – 2012-01-19T16:08:41.630

For some reason the "user" filter isn't working for this; however since I only switch between two users and the "mainuser" stays logged on it's fine for me. I did use event id 7002 though - might be the reason why the user filter is not working, not sure, I found that Id on some forum before you edited your answer. When I find time I'll try 4647. – Mene – 2012-02-23T12:43:06.347

1

Another alternative is to use Group Policies.

Logged in as the postgres user:

gpedit.msc -> User Configuration -> Windows Settings -> Scripts (Logon/Logoff)

This will require the user to have permission to start/stop services

Shevek

Posted 2012-01-19T12:21:22.937

Reputation: 15 408