The article you linked is an example where systemd passes the listen socket to the service. The service would still need to accept connections and fork off children to handle them. Are you sure that systemd offers a mode where it accepts and launches a new service each time a connections is received?

If your main goal is to let long-lived processes keep running on an old version while a new version is deployed and new connections use the new code, all you really need to do is execute a new perl process instead of forking. Or in other words, fork like you do above, then exec($^X, $script_name, @args); and put sub handle_connection in that script file. You can replace the file at any time and new connections get the new implementation without interrupting current ones.


So... I was curious and asked ChatGPT. It thinks your solution could be this simple:

If you're looking for a setup similar to inetd, where a new process is spawned for each incoming connection, systemd has a feature called socket-activated services with per-connection instances. This is achieved by using the Accept=yes option in the socket unit file. Here's a simplified example: Create a socket unit file, let's say example-socket.socket:
[Unit] Description=Example Socket [Socket] ListenStream=12345 Accept=yes [Install] WantedBy=sockets.target
This sets up a socket to listen on port 12345 and spawns a new service for each incoming connection. Create a service unit file, for example example-service@.service:
[Unit] Description=Example Service [Service] ExecStart=/path/to/your/executable StandardInput=socket
Note the %i in the service file, which represents the instance identifier. systemd will replace this with a unique identifier for each connection. Enable and start the socket:
systemctl enable example-socket.socket systemctl start example-socket.socket
Now, systemd will spawn a new service instance for each incoming connection on the specified socket.
If that is correct (which clearly it forgot to use a '%i' in the service file... and I don't see any linkage between the socket unit and the service unit...) then the top-level of your script could be reduced to just
handle_conenction(\*STDIN);

In reply to Re: Systemd socket activated service in perl by NERDVANA
in thread Systemd socket activated service in perl by chrestomanci

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.