I have below simple python script that stops a service running by administrator. When I run this script manually on windows core device as administrator it works perfectly.
**import win32serviceutil
service_name = "eMAROffline" # replace with the name of the service you want to stop
try:
win32serviceutil.StopService(service_name)
print(f"Service {service_name} stopped successfully.")
except Exception as e:
print(f"Error stopping service {service_name}: {e}")
**
My requirement to stop service using AWS IoT Greengrass component deployment. When I deploy this script as Greengrass component am getting the below error.
com.mx.stopService: stdout. Error stopping service eMAROffline: (5, 'OpenSCManager', 'Access is denied.')
I googled this error and it seems the issue is related to permissions. Greengrass component user ggc_user wouldn't have privileges' to stop services running under administrator.
Any suggestions/thoughts how can make this work?
I added ggc_user in Administrators group but no luck.
Full log:
2023-04-18T15:02:50.077Z [INFO] (pool-2-thread-58) com.mx.stopService: shell-runner-start. {scriptName=services.com.mx.stopService.lifecycle.Run, serviceName=com.mx.stopService, currentState=STARTING, command=["python C:\greengrass\v2\packages\artifacts-unarchived\com.mx.stopService\1.0.0..."]}
2023-04-18T15:02:51.120Z [INFO] (Copier) com.mx.stopService: stdout. Error stopping service eMAROffline: (5, 'OpenSCManager', 'Access is denied.'). {scriptName=services.com.mx.stopService.lifecycle.Run, serviceName=com.mx.stopService, currentState=RUNNING}
2023-04-18T15:02:51.152Z [INFO] (Copier) com.mx.stopService: Run script exited. {exitCode=0, serviceName=com.mx.stopService, currentState=RUNNING}
You may also set "RequiresPrivilege": "true" to run as SYSTEM which will certainly have permissions.
https://docs.aws.amazon.com/greengrass/v2/developerguide/component-recipe-reference.html#recipe-format:~:text=Lifecycle.Setenv.-,Run,-(Optional)%20An%20object