I had this old code base running in xinet d. It was basically a simple python script that waited for standard in to come in on a socket, it would do something with it and respond on standard out. So I heard xinetd is no longer used, so I tried to convert this to systemd. I can get all this setup and do a systemctl start kosmos (the service name)but it exits instantly. Was curious what if anything I might be missing in the setup to cause this. I am unsure as the code it is pretty simple forever loop that listens to that standard in so not sure why it exits on systemd.
kosmos.service
[Unit]
Description=Kosmos Guide Camera ICC
After=network.target kosmos.socket
Requires=kosmos.socket
[Service]
ExecStart=/home/workers/miniconda2/bin/python /home/workers/kosmosICC/kcamera/kcamera.py
StandardInput=socket
[Install]
WantedBy=default.target
Kosmos.socket
[Unit]
Description=Socket to KOSMOS for connection
PartOf=kosmos.service
[Socket]
ListenStream=127.0.0.1:30001
[Install]
WantedBy=sockets.target
So when I do a systemctl start kosmos
I get no error and yet a systemctl status kosmos shows:
● kosmos.service - Kosmos Guide Camera ICC
Loaded: loaded (/etc/systemd/system/kosmos.service; disabled; vendor preset: disabled)
Active: inactive (dead) since Thu 2021-03-04 17:58:43 EST; 14s ago
Process: 5754 ExecStart=/home/workers/miniconda2/bin/python /home/workers/kosmosICC/kcamera/kcamera.py (code=exited, status=0/SUCCESS)
Main PID: 5754 (code=exited, status=0/SUCCESS)
Mar 04 17:58:42 dhcp-093.apo.nmsu.edu systemd[1]: Started Kosmos Guide Camera ICC.
I didn't see where systemd might put logs in /var/log but didn't see anything that looked like it would give a clue as to what is going on (there was no kosmos.service.log or anything). I am probably trying to find the logs to see if systemd sees the code bomb out (running by hand the code runs fine)
This is the main loop snippet of the code trying to be ran:
def run():
'''
run for ever listening for standard in
all commands are echoed, whatever reply output, and then
an OK.
'''
while 1:
input = sys.stdin.readline()
try:
run()
except:
print format_exc()
(of course more methods etc) but this is the main one. I suspect the except might be getting called but not sure how to see print format_exec() when running from that systemd service.
EDIT:
Thanks to a comment suggestion I did run the socket and then it ran a bit, I could telnet to the socket but then sending any data made it died, though it doesn't say why:
journalctl -u kosmos.service -b
Mar 08 15:53:19 dhcp-093.apo.nmsu.edu systemd[1]: Started Kosmos Guide Camera ICC.
Mar 08 15:53:20 dhcp-093.apo.nmsu.edu systemd[1]: Started Kosmos Guide Camera ICC.
Mar 08 15:53:20 dhcp-093.apo.nmsu.edu systemd[1]: Started Kosmos Guide Camera ICC.
Mar 08 15:53:21 dhcp-093.apo.nmsu.edu systemd[1]: Started Kosmos Guide Camera ICC.
Mar 08 15:53:21 dhcp-093.apo.nmsu.edu systemd[1]: Started Kosmos Guide Camera ICC.
Mar 08 15:53:22 dhcp-093.apo.nmsu.edu systemd[1]: start request repeated too quickly for kosmos.service
Mar 08 15:53:22 dhcp-093.apo.nmsu.edu systemd[1]: Failed to start Kosmos Guide Camera ICC.
Mar 08 15:53:22 dhcp-093.apo.nmsu.edu systemd[1]: Unit kosmos.service entered failed state.
Mar 08 15:53:22 dhcp-093.apo.nmsu.edu systemd[1]: kosmos.service failed.
Does my Service need a Type? Or my socket?
Anywhere else to see in a log make sure it isn't my python code bombing out (Even though again it runs fine as a python program)
For socket activated services, you don't start the service file, you start the socket file.
Now systemd will listen on the specified port and pass incoming connections to the corresponding service, which it will also start itself.