Mar-14-2026, 11:46 PM
Hello.
I'm working on a script that uses the "pymodbus" library to read data in synchronous mode using the ModBus RTU protocol, running as a daemon on a RaspianOS system based on Debian 11. The program writes the read data to files. The daemon mode is based on the HanzDaemon script, which uses a double branch. The daemon uses both output to the standard file descriptor and error logging to the standard file descriptor.
The problem is the lack of any control over the number of error messages written to the error file, which, when they occur, grows to a horrendous size.
For example, a missing serial port is recorded in the output file with messages every five minutes. During this time, the error file contains 100 "[Errno 2] could not open port /dev/ttyUSB0" messages.
Another example: a lack of response from a ModBus slave device is recorded in the output file with messages every 15 minutes. During this time, the error file contains "No response received after 3 retries, continue with next request" messages, equal to the number of registers queried.
How can I control the number of error messages written to the error file?
I'm working on a script that uses the "pymodbus" library to read data in synchronous mode using the ModBus RTU protocol, running as a daemon on a RaspianOS system based on Debian 11. The program writes the read data to files. The daemon mode is based on the HanzDaemon script, which uses a double branch. The daemon uses both output to the standard file descriptor and error logging to the standard file descriptor.
def _makeDaemon(self):
"""
tworzenie demona - podwójne rozgałęzienie
"""
try:
pid = os.fork()
if pid > 0:
# Exit first parent.
sys.exit(0)
except OSError as err:
print(f'Błąd rozwidlenia #1: {err}')
sys.exit(1)
# odłączenie od środowiska rodzica
os.chdir('/')
os.setsid()
os.umask(0)
# wykonanie drugiego odgałęzienia
try:
pid = os.fork()
if pid > 0:
# Exit from second parent.
sys.exit(0)
except OSError as err:
print(f'Błąd rozwidlenia #2: {err}')
sys.exit(1)
print(f'Proces demona uruchomiono w tle (PID:{os.getpid()}).')
# przekierowanie standardowych deskryptorów plików
sys.stdout.flush()
sys.stderr.flush()
si = open(self.stdin, 'r')
so = open(self.stdout, 'a+')
se = open(self.stderr, 'a+')
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())I use the "try - except" exception handler in my program to catch "IO Exception" and "Communication Exception" errors. Writing error messages to the output file is time-delayed. The program stores the first error occurrence and, after a defined period, checks whether the error still occurs, which is then recorded with an appropriate message in the output file. The script works correctly.The problem is the lack of any control over the number of error messages written to the error file, which, when they occur, grows to a horrendous size.
For example, a missing serial port is recorded in the output file with messages every five minutes. During this time, the error file contains 100 "[Errno 2] could not open port /dev/ttyUSB0" messages.
Another example: a lack of response from a ModBus slave device is recorded in the output file with messages every 15 minutes. During this time, the error file contains "No response received after 3 retries, continue with next request" messages, equal to the number of registers queried.
How can I control the number of error messages written to the error file?
