I had a service I was trying to run this way but it was slightly a large python program. I took a step back and built a dead simple python program to see if I can get it to run. It fails when I try to connect via telnet to this socket running. Below are the .socket, .service and .py files....
testPy.socket
[Unit]
Description=Socket to TESTPY for connection
PartOf=testPy.service
[Socket]
ListenStream=30001
[Install]
WantedBy=sockets.target
testPy.service
[Unit]
Description=TEST PY
After=network.target testPy.socket
Requires=testPy.socket
[Service]
ExecStart=/home/workers/miniconda2/bin/python /home/workers/testPy.py
StandardInput=socket
[Install]
WantedBy=default.target
testPy.py
import sys
END_OF_LINE = '\r\n'
while(1):
input = sys.stdin.readline()
buffer = input.strip()
if not buffer:
sys.stdout.write("OKAY DUDE")
sys.stdout.flush()
continue
if buffer in ['quit', 'QUIT']:
break
sys.stdout.write('\n' + buffer + END_OF_LINE)
sys.stdout.flush()
now if I run this in a command line, it runs fine. I can type quit and it exits out of the loop,echos anything back..
If I say:
systemctl start testPy.socket
and then type:
telnet localhost 30001
it connects a bit then drops it. Then various statuses are (to me ) non descriptive:
systemctl status testPy.socket
● testPy.socket - Socket to TESTPY for connection
Loaded: loaded (/etc/systemd/system/testPy.socket; disabled; vendor preset: disabled)
Active: failed (Result: service-failed-permanent) since Thu 2021-03-11 13:59:54 EST; 11min ago
Listen: [::]:30001 (Stream)
Mar 11 13:59:42 dhcp-093.apo.nmsu.edu systemd[1]: Listening on Socket to TESTPY for connection.
Mar 11 13:59:54 dhcp-093.apo.nmsu.edu systemd[1]: Unit testPy.socket entered failed state.
systemctl status testPy.service
● testPy.service - TEST PY
Loaded: loaded (/etc/systemd/system/testPy.service; disabled; vendor preset: disabled)
Active: failed (Result: start-limit) since Thu 2021-03-11 13:59:54 EST; 12min ago
Process: 2087 ExecStart=/home/workers/miniconda2/bin/python /home/workers/testPy.py (code=exited, status=1/FAILURE)
Main PID: 2087 (code=exited, status=1/FAILURE)
Mar 11 13:59:54 dhcp-093.apo.nmsu.edu systemd[1]: Started TEST PY.
Mar 11 13:59:54 dhcp-093.apo.nmsu.edu systemd[1]: testPy.service: main process exited, code=exited, status=1/FAILURE
Mar 11 13:59:54 dhcp-093.apo.nmsu.edu systemd[1]: Unit testPy.service entered failed state.
Mar 11 13:59:54 dhcp-093.apo.nmsu.edu systemd[1]: testPy.service failed.
Mar 11 13:59:54 dhcp-093.apo.nmsu.edu systemd[1]: start request repeated too quickly for testPy.service
Mar 11 13:59:54 dhcp-093.apo.nmsu.edu systemd[1]: Failed to start TEST PY.
Mar 11 13:59:54 dhcp-093.apo.nmsu.edu systemd[1]: testPy.service failed.
I believe if I can get this simple test to work, I can get the larger .py file I need to run as it works essentially the same. I have a service and socket built for that, with generally the same errors. Though the systemctl status kosmos.service
gives a failed still but says the main PID status=0/success so that is odd.
It says a start limit is the fail, but if the service as simple as the one here has to start and start and start that means something else is wrong, guessing a config in my socket or service file but not sure what. I was hoping I could have my python not change at all from listening sys.stdin.readline etc, and just the lines it read were from a connection made on that port (30001) from another machine. I thought that is what all this socket stuff does just this (all this came about because it used to run on an older machine with xinetd)