• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 909
  • Last Modified:

How do I restart a 'service' after a user has completely logged off.

How do I restart a 'service' after a user has completely logged off?  I have an printer/plotter application that needs to be running all the time for users to be able to print to the device.  Problem is that way the program was written a user (with admin rights) has to be logged in all the time.  While the user only has to have local admin rights this is still a security risk for our network and not acceptable.  

What I have been able to do is using instrv.exe and srvany.exe create an "app as service" instance called plotbase.  The application when launched actually runs three other programs that cannot be launched manually.  The have to be called by the main program.  I have the service set to run automatically and when it the computer reboots the app runs as desired.  However when a user needs to login to monitor the application, if there is a struck job or something, a normal user (with only domain user rights) cannot launch the application.  They get the message a user with admin rights can only launch the program.  (if the user does have admin rights then they get an error that the program is already running)  

So, what I have been able to do is create a local user with admin rights (that no one know about except me), created a script (see below) that when a user logs in, it kills the current running process (using the local user account) and then restart the plotbase service.  The script is on the local machine in the startup folder. This works perfectly.  

However we don't need or want the user to stay logged in when done.  So when the user logs off the application shuts down killing all the associated processes and is not longer running in the background, but I still need it to stay running.  The main application defined as a service actually stays running, just the other three programs shut down. I tried to define all the programs as individual services and set the to start automatically and manually but that didn't work.  I have tried to create a script the restarts the service at log off but this happens before the log off process is complete and then the log off process kills my application processes. So, without rebooting, how can i get the service to start running again.  I am new to scripting so my script may not be the cleanest.  

BTW, the computer is part of a domain and using GPO I gave users the ability to start, stop and restart the service.  This is why the login script works because the service is running with the system account.

'Start script
on error resume next

Dim shell,spass,prog
set shell= createobject("wscript.shell")
prog="""c:\program files\rw-240\kp.bat"""
spass = "mypassword"&VBCRLF



shell.popup "Starting PlotBase.  This will take a minute, please wait.",5,"",64
Wscript.Sleep 10000


'kill remainants of plotbase program - call the kp.bat file
shell.run "runas /noprofile /user:cad-plotter\rw240 "&prog
Wscript.Sleep 100 'need to give time for window to open.
shell.sendkeys sPass



'Edit registry to turn Auto Plot and SSL readers
const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set StdOut = WScript.StdOut
 
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")
 
strKeyPath = "SOFTWARE\RATIO\PLOTBASE\3"
strValueName = "SslOn"
dwValue = "1"
oReg.SetDWORDValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,dwValue
 
strValueName = "AutoPlot"
dwValue = "1"
oReg.SetDWORDValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,dwValue

'Stop the PlotBase service
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery("Associators of " _
   & "{Win32_Service.Name='plotbase'} Where " _
        & "AssocClass=Win32_DependentService " & "Role=Antecedent" )
For each objService in colServiceList
    objService.StopService()
Next
Wscript.Sleep 20000
Set colServiceList = objWMIService.ExecQuery _
        ("Select * from Win32_Service where Name='plotbase'")
For each objService in colServiceList
    errReturn = objService.StopService()
Next

script.wait 20000
'Start the Plotbase service
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery _
    ("Select * from Win32_Service where Name='plotbase'")
For each objService in colServiceList
    errReturn = objService.StartService()
Next
Wscript.Sleep 20000
Set colServiceList = objWMIService.ExecQuery("Associators of " _
   & "{Win32_Service.Name='plotbase'} Where " _
        & "AssocClass=Win32_DependentService " & "Role=Dependent" )
For each objService in colServiceList
    objService.StartService()
Next
'End script

The script above calls this batch file (kp.bat) that is run as the local user with admin rights to kill the process:
Rem begin script
taskkill /F /IM pbru.exe /T
taskkill /F /IM pbdbru.exe /T
taskkill /F /IM rescfgru.exe /T
taskkill /F /IM resslru.exe /T
Rem end script


I was thinking that maybe I could create another script and run as another service that checks to see if the application and associated services are running and if not then stop the main application and/or associated running services and then restart the previously defined plotbase service.  But I am not sure how to create a vbscript or other script the will run or loop in the background to accomplish this.  

I have tried to define this script as a service but I get the error in services.msc that there was nothing to do when I restart it.
0
PlazaProp
Asked:
PlazaProp
  • 4
  • 2
1 Solution
 
kdtreshCommented:
You could try using scheduled tasks to run your checker at a specified interval, like every hour. You should be able to specify that the user doesn't have to be logged on for the task to run.
0
 
MarcjevCommented:
if you can program (like C#) it's simple to create another service which monitors the services and processes. If the first service is running, but the 3 apps are not, it could restart the service. So in fact you would create a kind of monitoring service...
0
 
PlazaPropAuthor Commented:
OK, here is what I came up with.  I created a service and assigned the below script to the service that starts automatically. But it is not working and my guess is because in the other script I am calling a batch file to kill the processes instead of doing it from withing the same script.  So, I believe that if I figure out how to kill the processes within the other script it will work, hopefully.  (then I will try to combine the two so that there is only one script)

Dim objWMIService, objProcess, colProcess
Dim strComputer, strList
Dim wshShell, i, restartplotbase, t

restartplotbase = """c:\program files\rw-240\restartplotbase.vbs"""
strComputer = "."

i = 0
Do While i = 0
Wscript.Sleep 10000
Set wshShell = CreateObject("Wscript.Shell")
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

'start sub routines of checking if process are running
a

loop

sub a
Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where name = 'pbru.exe'")
If colProcess.Count <> 0 then
b
Else
        wshShell.run restartplotbase, 1, true
end if
end sub

sub b      
Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where name = 'pbdbru.exe'")
If colProcess.Count <> 0 then      
c
Else
        wshShell.run restartplotbase, 1, true
end if
end sub

sub c
Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where name = 'recfgru.exe'")
If colProcess.Count <> 0 then
d
Else
        wshShell.run restartplotbase, 1, true
end if
end sub

sub d
Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where name = 'resslru.exe'")
If colProcess.Count <> 0 then
t=1
Else
        wshShell.run restartplotbase, 1, true
end if
end sub
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
PlazaPropAuthor Commented:
I have tired both of these snips of code but neither seem to work.  I think it has something to do with ACL but not sure and not sure how to get around this. If the script runs as SYSTEM, shouldn't it be able to kill processes owned by SYSTEM?

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery("Associators of " _
   & "{Win32_Service.Name='pbru'} Where " _
        & "AssocClass=Win32_DependentService " & "Role=Antecedent" )
For each objService in colServiceList
    objService.StopService()
Next




Set colServiceList = objWMIService.ExecQuery _
        ("Select * from Win32_Service where Name='pbru'")
For each objService in colServiceList
    errReturn = objService.StopService()
Next
0
 
kdtreshCommented:
Can you start/stop the service with "net stop serviceName" or "net start serviceName" at the command prompt? If so, you can use a batch file to stop/start it.
0
 
PlazaPropAuthor Commented:
Yes, as an administrator I can start and stop the service and I have made a batch file to do this but the user cannot use the net stop/start because only one of the 3 programs is actually defined as a service.  The other 3 programs are launched by the first program.  The issue is making sure all four programs are running even if there is not a user logged on.  All 4 programs start up fine on a reboot, and when a use logs in my script stops the services and then restarts them so that the logged on user can use the GUI for the program.  The problem is when the user logs off  the program(s) close and don't restart.  I need make sure the program restarts after the user logs off, without having to restart the computer.  My script is a vb scirpt and it seems that even though it is calling a batch file it is not working.  I think it is not working because the batch file requires a password to be sent to it and it it not sending the password while running in the background. The other 3 programs do not show up in the "services" list, they show up in the task list. My most recent post of code relates to only services (I think) and therefore will not work.  I need a "taskkill" type command that I can execute from within the vbscript without having to "pass" a password to an external script.  If I have to hard code the password that is ok cause I have created a special local user account that is used only for this purpose.    
0
 
PlazaPropAuthor Commented:
OK, solved my own issue.

Previously the machine had to have a user continually logged in for networked users to be able to print to the plotter/printer. This presents security risk because the user had to have local administrative rights.  With a couple of windows tools (instrsv.exe and srvany.exe ) and some creative thinking I have been able to configure that machine so that the program runs upon startup without having to have a user log in.  If a user logs in they can use the application just as before.  A script monitors to make sure that application is running correctly and if not it will automatically restart the program.  When the user logs out the main program closes therefore it will some time before the script restarts the application and print jobs start back up, usually within 1 to 2 minutes.  Even during this process users can send print jobs, they just wont print until the application restarts.

Since application and its dependents are executed by the script that has admin rights, they cannot simple close the program by normal means.  So that users can easily restart teh program, a Restart shortcut (to a script) exists on the desktop.  There is a Stop shortcut on the desktop as well. There should be no need for a user to stay logged in anymore and if the computer is restarted, the application will start properly configured to automatically spool and print documents sent to the plotter/printer.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now