Created
July 30, 2022 06:44
-
-
Save ericwoud/017d46c2ca0f19d0fc789641199d032c to your computer and use it in GitHub Desktop.
Oscam helper script to restart reader on error
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [Unit] | |
| Description=A helper service to execute and monitor oscam written in Python | |
| [Service] | |
| Environment=PYTHONUNBUFFERED=true CONFIGPATH=/var/lib/oscam | |
| ExecStart= | |
| ExecStart=/usr/bin/python3 /usr/local/bin/oscam.py | |
| SyslogIdentifier=oscam-py | |
| Type=simple | |
| TimeoutStopSec=1 | |
| Restart=on-failure | |
| RestartSec=3 | |
| StartLimitInterval=0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python3 | |
| # oscam.conf: | |
| # logfile = syslog;stdout | |
| # reload_readers = 1 | |
| from subprocess import Popen, PIPE | |
| from signal import signal, Signals | |
| from os import getenv | |
| from sys import exit | |
| restartreaders = ["THIS WAS A FAILED START ATTEMPT", | |
| "Error activating card", | |
| "card system not supported", | |
| "card initializing error", | |
| "Cannot open device", | |
| "SR: USB bulk read failed with error", | |
| "Error activating card", | |
| "rejected group (0 ms) (no MATCHED reader)", | |
| ] | |
| restartoscam = ["(login failure)", | |
| "AU disabled for user", | |
| ] | |
| restartscript = [ | |
| ] | |
| stoposcam = [ | |
| ] | |
| def contain(string_, substrings): | |
| return any(s in string_ for s in substrings) | |
| def handler(signum, frame): | |
| global restart ; restart = False | |
| print("Received ", Signals(signum).name, ", should not restart oscam and script.") | |
| restart = True | |
| rc = 0 | |
| signal(Signals.SIGINT, handler) | |
| while restart: | |
| print("Starting oscam") | |
| proc = Popen(["oscam", "-c", getenv('CONFIGPATH'), "-r", "0"], | |
| stdout=PIPE, bufsize=1, universal_newlines=True) | |
| for line in iter(proc.stdout.readline, ''): | |
| # print("OSCAM:" + line.rstrip()) | |
| if contain(line, restartreaders): | |
| print("MATCHED: ", line , "Sending SIGHUP, Oscam should restart the reader", sep='') | |
| proc.send_signal(Signals.SIGHUP) | |
| elif contain(line, restartoscam): | |
| print("MATCHED: ", line , "Sending SIGTERM, Script should restart oscam", sep='') | |
| proc.send_signal(Signals.SIGTERM) | |
| elif contain(line, restartscript): | |
| restart = False | |
| rc = 1 | |
| print("MATCHED: ", line , "Sending SIGINT, Service should restart script", sep='') | |
| proc.terminate() | |
| elif contain(line, stoposcam): | |
| restart = False | |
| print("MATCHED: ", line , "Sending SIGINT, Service should stop", sep='') | |
| proc.terminate() | |
| proc.wait(2) | |
| if proc.returncode != 99: | |
| print("Oscam not requesting to restart (", proc.returncode, ")", sep='' ) | |
| restart = False | |
| else: | |
| print("Oscam requesting to restart (", proc.returncode, ")", sep='' ) | |
| proc.kill() | |
| print("Finished Oscam and script, return", rc, "to systemd") | |
| exit(rc) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment