7

I have a program that under normal activation listens on some port.

I don't want the program running continuously.

Is there a "quick and dirty" way to wrap the application in a shell script or similar that will monitor the appropriate port, and start the service on demand?

The simplest approach would likely lead to the connection failing since the wrapper would have to let go of the port, and then start up the application. If the client simply connects again a short time later though, it could all work.

But it would of course be even nicer if this was all completely transparent to the client.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
user50849
  • 5,082
  • 5
  • 25
  • 30
  • As no one has responded in 2 hours, I will give what I can know. The answer is yes. from what I can remember, there is a program that runs as part of the OS. It listens on multiple ports. If something connects to a port, it launches the registered program and passes of the connection to it. – ctrl-alt-delor Oct 27 '14 at 21:51
  • I did some research and discovered that the thing I remember is `inetd`, however it has been replaced. See http://en.wikipedia.org/wiki/Inetd Of all the replacements I have `systemd` installed on my debian machine. – ctrl-alt-delor Oct 27 '14 at 22:08
  • If your distro is a redhat-base use xinetd, and your distro is a debian-base use inetd. – PersianGulf Oct 27 '14 at 22:32
  • @richard I'm aware of systemd, that's actually where I got the idea from to begin with, I'd prefer not needing to rip out the interals of my distro though. ;) I'll take a look at inetd. – user50849 Oct 28 '14 at 07:19
  • My hope is that it can do what inetd does, and you just edit the config file, but I have no idea. I last touched this pre-1995, when inetd was still in use. – ctrl-alt-delor Oct 28 '14 at 19:46
  • @richard Inetd hasn't been replaced. There are several implementations of inetd that do much more than the basic job, but you can still use a plain inetd if that's what you need. – Gilles 'SO- stop being evil' Oct 29 '14 at 00:32
  • Why is your daemon taking so many resources while just being idle? If it is listening on a socket, it should be sleeping and using no CPU. If the program has problems WRT memory, then fix those because they will be an issue either way. – CameronNemo Oct 29 '14 at 00:42
  • 1
    @CameronNemo If it was my application, or if I had access to the source, that would be an option. Currently however, it is not. :) – user50849 Oct 29 '14 at 06:53

2 Answers2

7

As richard already mentioned, what you're looking for has existed for a long time. It's called inetd.

There are several implementations of inetd around. Some are simple and do just that socket activation thing (they're usually just the inetd program from some bigger software package containing other basic networking tools, such as GNU inetutils or BusyBox or BSD tools which have been ported to Linux), some offer more features (xinetd is pretty popular, rlinetd describes itself as “gruesomely over-featured”), and there's an implementation of inetd in systemd (what doesn't systemd implement?).

Almost every unix system comes with an inetd implementation. Traditionally, “little” services such as echo, finger, ftp, etc. have used inetd, while “big” services such as httpd and NFS and helpers as well as ssh haven't. Use your distribution's default inetd implementation unless you need more than the basic service.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
3

It is also possible to ask systemd to open a socket (whether TCP, UDP or unix AF) and then start a service when a connection is received. Since not all services are happy being passed existing sockets and prefer to create their own, systemd also includes a helper named systemd-socket-proxyd which buffers content on the systemd-opened socket until the correct listening socket has been bought online by the requested service.

Here's how.

DMCoding
  • 201
  • 1
  • 5