To help you better we need some information from you.
*** Please fill in the fields below. If you leave fields empty or specify 'latest' rather than the actual version your answer will be delayed as we will be forced to ask you for this information. ***
Product, version and build: PowerShell Studio 2018 v5.5.154
32 or 64 bit version of product: 64
Operating system: Windows Server 2012 R2
32 or 64 bit OS: 64
*** Please add details and screenshots as needed below. ***
I've created a service from the Service Project Template that generates several background jobs that will continuously run. When I issue the stop command to the service the Stop-command never seems to reach the service. I've set up commands that update a log file when the Stop-MyService command is issued and nothing makes its way into that function.
I've verified the Invoke-MyService and everything else seems to run well, but I cannot cleanly stop my service. Any help would be appreciated.
DO NOT POST SUBSCRIPTIONS, KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM
Service Project Not Receiving Stop-MyService command
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Re: Service Project Not Receiving Stop-MyService command
If a service takes too long to stop, it will timeout and an error will be returned. Make sure all of your jobs are aware that the service is stopping and are able to stop within seconds after attempting to stop the service.
Brittney
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.
Re: Service Project Not Receiving Stop-MyService command
- function Stop-MyService
- {
- "ACTIVITY,Stopping Service" >> $global:ConfigurationImport.confLogFilePath
- $global:bRunService = $false # Signal main loop to exit
- $CountDown = 30 # Maximum wait for loop to exit
- while($global:bServiceRunning -and $Countdown -gt 0)
- {
- Start-Sleep -Seconds 1 # wait for your main loop to exit
- $Countdown = $Countdown - 1
- }
- Try
- {
- Get-Job $jobname | Stop-Job
- Get-EventSubscriber | Where {$_.SourceIdentifier -eq $jobname} | Unregister-Event
- Get-Job $jobname | Remove-Job
- }
- catch
- {
- $_.Exception.Message >> $global:ConfigurationImport.confLogFilePath
- }
- }
Am I missing something?
Re: Service Project Not Receiving Stop-MyService command
Just for my clarification, it never writes to your log file? Is there any information that is returned in the event logs when you try to stop the service? Also, how are you packaging your service project?
The Stop-MyService function is called when your service is asked to stop by the operating system or when using Stop-Service. This is also the function that you would terminate running jobs and secondary runspaces; otherwise the service process may hang and not exit properly. As some modules create their own threads, jobs, runspaces, etc. it is also a good idea to use remove-module for any modules you load implicitly or explicitly.
The Stop-MyService function is called when your service is asked to stop by the operating system or when using Stop-Service. This is also the function that you would terminate running jobs and secondary runspaces; otherwise the service process may hang and not exit properly. As some modules create their own threads, jobs, runspaces, etc. it is also a good idea to use remove-module for any modules you load implicitly or explicitly.
Brittney
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.
- mxtrinidad
- Posts: 399
- Last visit: Tue May 16, 2023 6:52 am
Re: Service Project Not Receiving Stop-MyService command
It may be possible that due to the scope of variable in the function, when it goes from the Invoke-Service to the Stop-Service, the Get-Job might be null.
We need to see what you have in the Invoke-Service. Can you please share the code?
Thanks
We need to see what you have in the Invoke-Service. Can you please share the code?
Thanks
- mxtrinidad
- Posts: 399
- Last visit: Tue May 16, 2023 6:52 am
Re: Service Project Not Receiving Stop-MyService command
Using some basic assumption, I created a service job that output to a text file, found the Stop-MyService is not working keeping the service running when it should stop.
I'll have to talk to the developer which is on vacation until next week.
Thanks for notifying the issue!
I'll have to talk to the developer which is on vacation until next week.
Thanks for notifying the issue!
Re: Service Project Not Receiving Stop-MyService command
Good to know! I'll continue to use it as is until a fix can be implemented.
For those curious a basic version of my script is as follows:
For those curious a basic version of my script is as follows:
- # Warning: Do not rename Start-MyService, Invoke-MyService and Stop-MyService functions
- function Start-MyService
- {
- # Place one time startup code here.
- # Initialize global variables and open connections if needed
- "Starting Service" >> log.txt
- Start-Job -Name JobName -ScriptBlock { Get-Content -Tail 0 -Wait | where $_ -Like "*WARNING*"}
- $global:bRunService = $true
- $global:bServiceRunning = $false
- $global:bServicePaused = $false;
- }
- function Invoke-MyService
- {
- $global:bServiceRunning = $true
- while($global:bRunService) {
- try
- {
- if($global:bServicePaused -eq $false) #Only act if service is not paused
- {
- Receive-Job -Name 'JobName' >> C:\Logs\log.txt
- #Place code for your service here
- #e.g. $ProcessList = Get-Process solitaire -ErrorAction SilentlyContinue
- # Use Write-Host or any other PowerShell output function to write to the System's application log
- }
- }
- catch
- {
- # Log exception in application log
- Write-Host $_.Exception.Message
- }
- # Adjust sleep timing to determine how often your service becomes active.
- if($global:bServicePaused -eq $true)
- {
- Start-Sleep -Seconds 20 # if the service is paused we sleep longer between checks.
- }
- else
- {
- Start-Sleep –Seconds 10 # a lower number will make your service active more often and use more CPU cycles
- }
- }
- $global:bServiceRunning = $false
- }
- function Stop-MyService
- {
- "Stopping Service" >> Log.txt
- $global:bRunService = $false # Signal main loop to exit
- $CountDown = 30 # Maximum wait for loop to exit
- while($global:bServiceRunning -and $Countdown -gt 0)
- {
- Start-Sleep -Seconds 1 # wait for your main loop to exit
- $Countdown = $Countdown - 1
- }
- Get-Job -Name 'JobName' | Stop-Job
- Get-Job -Name 'JobName' | Remove-Job
- # Place code to be executed on service stop here
- # Close files and connections, terminate jobs and
- # use remove-module to unload blocking modules
- }
- function Pause-MyService
- {
- # Service is being paused
- # Save state
- $global:bServicePaused = $true
- # Note that the thread your PowerShell script is running on is not suspended on 'pause'.
- # It is your responsibility in the service loop to pause processing until a 'continue' command is issued.
- # It is recommended to sleep for longer periods between loop iterations when the service is paused
- # in order to prevent excessive CPU usage by simply waiting and looping.
- }
- function Continue-MyService
- {
- # Service is being continued from a paused state
- # Restore any saved states if needed
- $global:bServicePaused = $false
- }
Re: Service Project Not Receiving Stop-MyService command
I used your script to create Windows Service and tested it on both Windows 10 and Windows Server 2012 R2 OSes. I was able to stop and start service without any issues.
If you can, please zip and load up the .msi file, so that we can attempt to recreate the issue:
https://www.sapien.com/support/upload
If you can, please zip and load up the .msi file, so that we can attempt to recreate the issue:
https://www.sapien.com/support/upload