Simple code running in systemd exits to fast?

Codejoy asked:

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]
Type=fork
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]
Descriptions=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.

My answer:


For socket activated services, you don’t start the service file, you start the socket file.

systemctl enable --now kosmos.socket

Now systemd will listen on the specified port and pass incoming connections to the corresponding service, which it will also start itself.


View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.