Koozali.org: home of the SME Server

How to run multiple processes from one [daemon tools] service? - SORTED

Offline judgej

  • *
  • 375
  • +0/-0
Following on from my monotone thread, I now know how to set up a service in SME Server and run a process in the foreground. The process I am running is monotone, the distributed change control system, as a server.

Now, I wish to run several of these services, each listening on a different port and connected to a different monotone repository. So far as I can see, a single service can run just one process. If that process happens to spawn several children, and provides supervision for those children (e.g. httpd) then that is fine, but if I want to run more than one forground process, then it seems that it must be done from several different services.

So, the question is: am I understanding this correctly? Do I really need to set up a separate service for each of my monotone repository servers? If so, and assuming the list of repositories and ports is located in a configuration file, how would I automate creating as many or as few services as I need? Would I have one startup service that creates additional services on-the-fly when it starts up, and starts them all as a group?

Or, is there some other way of doing this?

The service processes I would want to start up are basically of this form:

exec useruidgid mtn /usr/local/bin/mtn --db=foo.db --bind=example.com:12000
exec useruidgid mtn /usr/local/bin/mtn --db=bar.db --bind=example.com:12001
etc.

The databases and port bindings will be configured through a file or admin screen.

-- Jason
« Last Edit: September 01, 2008, 11:15:16 AM by judgej »
-- Jason

Offline pfloor

  • *****
  • 889
  • +1/-0
Re: Advice on how to run multiple processes from one service?
« Reply #1 on: August 30, 2008, 09:37:44 PM »
Look at the way that httpd-e-smith and httpd-admin are setup, it might be what you have to do.  httpd-e-smith and httpd-admin use different config files and run 2 different processes under 2 different users.

For example, I had 3 httpd services running 3 different config files under 3 different users on one server a while back.  FreePBX needs httpd to run as user "asterisk" for it to work properly so I added a third instance of httpd called httpd-asterisk.  It started a new httpd service running as asterisk with a 3rd config file.

Doesn't look like you need to run your services with different users but you do need them to run with different config files so you may want to look at this option.

This may be a totally wrong approach but it did work for me.

-Paul
In life, you must either "Push, Pull or Get out of the way!"

Offline judgej

  • *
  • 375
  • +0/-0
Re: Advice on how to run multiple processes from one service?
« Reply #2 on: August 30, 2008, 09:45:19 PM »
Yes, I noticed they are run this way. The difference in this case is that there only every will be (and always must be) those two httpd services (apart from additional customisations, as you state). This means the services are fairly static.

With the monotone services, there could be any number of them, and they could change from time-to-time. I suspect I will need to create these services through an admin screen and enable and disable them there too. Each time I want a new server, a new config file will be written, a new service created (e.g. mtn.1, mtn.2, mtn.3 etc.). When I no longer need that server, the service can be removed.

It just feels wrong, as though the services should be more static, and the multiple servers run some other way. But perhaps that is the way it is?

-- Jason
-- Jason

Offline pfloor

  • *****
  • 889
  • +1/-0
Re: Advice on how to run multiple processes from one service?
« Reply #3 on: August 30, 2008, 10:23:48 PM »
Some more food for thought...

Use the config db to help you configure the service (including proper firewall settings) eg:

Code: [Select]
config set monotone1 service TCPPort xxx1 UDPPort xxx1 access public status enabled
config set monotone2 service TCPPort xxx2 UDPPort xxx2 access private status disabled

service and status settings control starting/stoping the service.
TCPPort, UDPPort and access settings will open the correct ports.

You can expand on this with many db entries (or you own custom database), templates that write custom config files and /var/service files on the fly and you can add some events to expand the right templates then stop, start and/or reload the appropriate services.

You can get VERY creative with the config db's and template engine, look at the /etc/httpd/conf/httpd.conf templates.

Some careful planning and you could control the entire setup with db entries, templates, events and a custom panel.

-Paul
« Last Edit: August 30, 2008, 10:25:33 PM by pfloor »
In life, you must either "Push, Pull or Get out of the way!"

Offline judgej

  • *
  • 375
  • +0/-0
Re: Advice on how to run multiple processes from one service?
« Reply #4 on: August 30, 2008, 10:30:56 PM »
Thanks. I was certainly going to do something like that, but that's a big help to get me started.

-- Jason
-- Jason

Offline judgej

  • *
  • 375
  • +0/-0
Re: Advice on how to run multiple processes from one service?
« Reply #5 on: August 31, 2008, 03:06:31 AM »
Thinking about it, adding a new service is actually not that difficult. Once I have my run script under /var/service, adding a new service is just a case of linking it in under a new name:

ln -s /var/service/monotone/ /service/monotone.1
ln -s /var/service/monotone/ /service/monotone.2
etc.

The run script can then look at the directory it is run from (monotone.x) and look up its configuration data in the database based on that name.

Removing one of those services would involve stopping the service, then removing the symbolic link.

It took a relax in the bath to come up with that one :-)

-- Jason


Edit: Now I've written all that, I realise it probably would not work, as the files under the /service/monotone folder only support one procress at a time. Back to the bathtub.
« Last Edit: August 31, 2008, 03:08:19 AM by judgej »
-- Jason

Offline judgej

  • *
  • 375
  • +0/-0
Re: Advice on how to run multiple processes from one service?
« Reply #6 on: August 31, 2008, 03:29:50 PM »
I think I have found another way to handle this: run multiple processes from my service shell script as background processes. The trick is to make sure they all get killed when the service is stopped, and that is done with a trap. So my 'run' script executes 'run.all' which looks something like this:

Code: [Select]
#! /bin/sh
trap 'kill -s 15 0; exit' 15
/usr/local/bin/mtn --db=~mtn/home/test1.mtn --bind=example.com:4691 serve &
/usr/local/bin/mtn --db=~mtn/home/test2.mtn --bind=example.com:4692 serve &
# etc
wait

The 'wait' will keep the script hanging around until all its children are terminated. The trap will ensure the 'term' signal is sent to its children when the service is stopped.

The next step would be to start these child processes from a config script and/or the SME database. I could also trap CHLD to restart individual child processes if they get killed. In addition, logging etc. would be useful. But essentially, I think this would do the job - an arbitrary number of monotone servers derived from configuration data, and without having to create a new service for each one.

Hope this is of help to someone.

-- Jason
« Last Edit: August 31, 2008, 03:31:30 PM by judgej »
-- Jason