Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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

share|improve this answer
1  
Just 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 Jan 19 '12 at 13:51
    
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 Jan 19 '12 at 13:57
    
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 Jan 19 '12 at 14:10
    
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 Jan 19 '12 at 14:13
    
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 Jan 19 '12 at 14:19

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.