How can I have a service run a process under the current user's session?
Asked Answered
O

3

0

I have a service, written in C#, that monitors a set of files. It uses Growl to notify the end-user if the modified time of one of these files is over 4 hours old. I've seen situations where Growl.exe was not running under the current user's session, thus they did not receive updates. I'd like my service to check to make sure Growl is running (either Growl.exe or maybe I could look for the application ID somewhere?) and start it if it's not. How can I ensure that Growl is always running in the current user's session on any machine that this service runs on?

Olives answered 11/4, 2011 at 20:49 Comment(4)
What is "the current user"? From the service point of view, it's the user running the service (i.e. Local System, LocalService, etc). If you mean the current interactive user, which one? There might be several open interactive sessions (for instance if it's a terminal server)...Dorr
Session 0 would be a reasonable assumption. I'm willing to limit this to users on the physical console. I should mention, only Windows client machines are running this at present. It's very unlikely that it will ever run on a server.Olives
Remember, desktop versions of windows can have multiple simultaneous users as well, via fast user switching.Rebecarebecca
That's a very good point Mystere. I'll be sure to keep that in mind in the future.Olives
R
1

If the service needs to run as the current user, then why make it a service? The purpose of a service is to run at all times, even when a user is not logged in. As such, it's designed to run as a specific user at all times.

Why not convert this to a program that runs when a user logs in?

Rebecarebecca answered 11/4, 2011 at 21:11 Comment(5)
It's not the service that needs to run as the current user. It's Growl (www.growlforwindows.com). Having the monitoring application run as a service makes it a bit easier to manage from the back end. I believe it will also make it easier for me to implement a heartbeat between the different machines running it in the future, which is something that I'd like to do.Olives
Services should not interact with the desktop. There are various security vulnerabilities this causes, and recent versions of windows have taken steps to mitigate those. Those steps make services interacting with the desktop very annoying. You can certainly check to see if a certain app is running, and maybe even discover if it's running in the current interactive users desktop.. but starting an app on that desktop would just be a huge security flaw. You have to have some user-mode applicaiton running that can notify the user.Rebecarebecca
I suppose you're right. Starting an application under a user's session probably isn't good form. You mentioned having an application running that can notify the user. I'm already doing that using Growl. What I'm trying to avoid is a situation where either Growl crashes or the user terminates it. My idea was to have the service restart Growl if it crashed or was terminated.Olives
Well, you could have a small watchdog process that starts when the user logs in, this would start growl automatically, and monitor it. Be aware, this behavior is very malware-like and some users may not appreciate it.Rebecarebecca
As long as it doesn't trip anti-virus software, I'm okay with it. This monitoring application is critical to business continuity. I'll look into it. Thanks.Olives
C
0

It's an interesting question, but it would appear that you can't do this as this post suggests. It seems like you can either specify that the service runs as a local system service, or as a particular user:

Service Dialog

Cosentino answered 11/4, 2011 at 21:9 Comment(0)
S
0

If your service is being started as "automatic" then you may only pre-configure a single set of credentials for the service. However, if you can identify the .exe file that the service is invoking at startup then you can create a batch file that will kick off that service at logon, at which point the active users credentials will be used by default.

Cheers,

CEC

Sonny answered 11/4, 2011 at 21:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.