My script/program consists of main.py; submethod1.py, submethod2.py in subdirectory; second subdirectory for temporary files; one txt file for logging I'm s struggling with running it as service for a week.
PyWin32 gives me Error 1053: The service did not respond to the start or control request in a timely fashion
.
NSSM just writes this Unexpected status SERVICE_STOPPED in response to START control
and nothing in stdout
How to understand what is wrong with service? I can't debug it properly.
It works standalone perfectly, I debugged it in IDE.
import configparser
from script.sql import *
def app():
# reading .ini file
print (os.getcwd())
default_file = os.getcwd()+ r"/config.ini"
config = configparser.ConfigParser()
config.read (default_file)
path = config.get ("XLS", "dir")
file_1 = path + "\\" + config.get ("XLS", "workbook1")
file_2 = path + "\\" + config.get ("XLS", "workbook2")
file_3 = path + "\\" + config.get ("XLS", "workbook3")
usr = config.get ("SQL", "user")
pwd = config.get ("SQL", "password")
hst = config.get ("SQL", "host")
db = config.get ("SQL", "database")
pt = config.get ("SQL", "port")
ssl_cert = config.get ("SQL", "ssl_cert")
ssl_ca = config.get ("SQL", "ssl_ca")
ssl_key = config.get ("SQL", "ssl_key")
enc = config.get ("SQL", "encoding")
sql_config = "mysql+pymysql://" + usr + ":" + pwd + "@" + hst + ":" + pt + "/" + db + "?" + ssl_cert + "&" + \
ssl_ca + "&" + ssl_key + "&" + enc
if (os.path.isfile (file_1) == True or os.path.isfile (file_2) == True or os.path.isfile (file_3) == True):
SQL.sqlconnect (sql_config)
if (os.path.isfile (file_1) == True):
XLS.xls_wrk (file_1)
df_xls_1 = XLS.table_xls (file_1)
df_sql_1 = SQL.table_sql (file_1, sql_config)
if (df_sql_1.equals (df_xls_1) == False):
try:
SQL.sql_import (file_1, sql_config)
except:
SQL.table_trunc (file_1, sql_config)
if (os.path.isfile (file_2) == True):
XLS.xls_wrk (file_2)
df_xls_2 = XLS.table_xls (file_2)
df_sql_2 = SQL.table_sql (file_2, sql_config)
if (df_sql_2.equals (df_xls_2) == False):
try:
SQL.sql_import (file_2, sql_config)
except:
SQL.table_trunc (file_2, sql_config)
if (os.path.isfile (file_3) == True):
XLS.xls_wrk (file_3)
df_xls_3 = XLS.table_xls (file_3)
df_sql_3 = SQL.table_sql (file_3, sql_config)
if (df_sql_3.equals (df_xls_3) == False):
try:
SQL.sql_import (file_3, sql_config)
except:
SQL.table_trunc (file_3, sql_config)
SQL.con_close (sql_config)
else:
with open (os.getcwd () + r"\PyXLSQL.log", "a+") as text_file:
print (f"{str(datetime.datetime.now()).split('.')[0]} - Excel workbooks not found in dir {path}",
file=text_file)
if __name__ == '__main__':
app()
I used working templates like all python windows service can not start{error 1053}
What can be wrong? There is a loop, or I can add one to check directory for any *xls files in RunSvc section. Anyway no useful debug info after trying running it as service.
UPDATE:
Debugging service PyTest - press Ctrl+C to stop.
Info 0x40001002 - The PyTest service has started.
Error 0xC0000003 - The instance's SvcRun() method failed
<Error getting traceback - traceback.print_exception() failed
(null): (null)
With another template for running script as service.
I had a similar issue with NSSM, in my case the problem was due to the fact that I didn't set up the working directory correctly.
When creating a service out of a python script with the command:
nssm install '<SERVICE_NAME>' '<PYTHON_INTERPRETER_PATH>' '<SCRIPT_PATH>'
the working directory is set to the one containing the python executable.To solve the issue I had to change the path to the one containing the script I wanted to run either with the command
nssm set <SERVICE_NAME> AppDirectory <SCRIPT_DIRECTORY>
or with the Edit-GUInssm edit <SERVICE_NAME>