pythonubuntuconfigparsersystem-services

configparser - system service failed when reading config file


I have a working python script. It works as a system service unless I read from a config file via configparser() -> (code=exited, status=1/FAILURE)

config = configparser.ConfigParser()    
config.read(configfile)
csvfile = config.get('Oled-config', 'csvpath2')

When I run this script including above code on the cmd line it works as intended but running as service gives an error.


Solution

  • Probably the problem is about "configfile". When you run a service, it run in "C:\Windows\system32", so if you have not an absolute path, you have to store your configuration file in there. If you use dinamically path, instead, the user path is "C:\Windows\System32\config\systemprofile" and not "C:\Users\YourUser". If you are not sure about the current working path, you can add this lines:

    import os
    import logging
    logging.basicConfig(filename=r"Path\to\your\Log.log", level=logging.INFO)
    logging.info(os.getcwd())
    

    This will save a log file in a choiced path storing the current work directory.

    If you are sure that the issue is related to configparser, I also suggest you to add a "try...except" to let start your service and to add a log write in the except, so that you can catch your error