0

I have a Python script that restarts a GSM modem by opening its /dev/ttyUSBx serial port file, sending a shutdown AT-command, waiting for a response and then waiting for the modem to shutdown by monitoring for the disappearance of the serial port file, after which, the GPIO pins are used to physically turn the modem off and then back on again.

The script works fine when run standalone. I've then tried to use systemd to turn it into a oneshot service as follows:

[Unit]
Description=Modem Restart Service

[Service]
Type=oneshot
ExecStart=/usr/bin/env python3 modem-restart.py

[Install]
WantedBy=multi-user.target

However, when attempting to start the service, it always fails with the following error:

Apr 02 09:34:21 test-system systemd[1]: Starting Modem Restart Service...
-- Subject: Unit RestartModem.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit RestartModem.service has begun starting up. Apr 02 09:34:22 test-system InternetMonitor.exe[5601]: INFO:root:Sending shutdown command to modem... Apr 02 09:34:36 test-system systemd[1]: RestartModem.service: Main process exited, code=killed, status=1/HUP Apr 02 09:34:36 test-system systemd[1]: Failed to start Modem Restart Service.
-- Subject: Unit RestartModem.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit RestartModem.service has failed.
--
-- The result is failed. Apr 02 09:34:36 test-system systemd[1]: RestartModem.service: Unit entered failed state. Apr 02 09:34:36 test-system systemd[1]: RestartModem.service: Failed with result 'signal'.

Can anyone explain what the cause of the error is?

0

After further investigation, the problem seems to stem from the fact that the program has an open file handle to the serial port device file when the modem disconnects from the USB port1. My guess is that the kernel sends SIGHUP to let the program know of the disconnection. This is handled fine by Python without any errors and the program runs without any problem when executed normally. However, it seems that systemd intercepts the SIGHUP signal and treats it as a failure, killing the program. Modifying the program to work differently has bypassed the problem.


1: The reason for keeping the file handle open is that after sending the shutdown command, the program waits until the modem sends it a message indicating that it has fully shutdown. Unfortunately, the modem disconnects from the USB immediately after sending the message, which doesn't give enough time for the program to parse the response and then close the file handle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.