3

I have created my own service by:

  1. copying my binary to /usr/bin/mcp
  2. Create a daemon copying the skeleton: /etc/init.d/skeleton
  3. Modify the skeleton bash script top:

    PATH=/sbin:/usr/sbin:/bin:/usr/bin
    DESC="Runs my Service"
    NAME=mcp
    DAEMON=/usr/bin/$NAME
    DAEMON_ARGS="-d f -c /etc/mcp"
    PIDFILE=/var/run/$NAME.pid
    SCRIPTNAME=/etc/init.d/$NAME
    

    Other than this the skeleton is exactly the same.

  4. Then I create the daemon:

    sudo update-rc.d -f mcp remove
    sudo update-rc.d mcp defaults 97 03
    

When I run the program manually: mcp -d f -c /etc/mcp, it works, but it never returns since its a service that is meant to always run. This is normal.

But now when I start it as a service: sudo service mcp start, I see the same behavior. I.e. as soon as I enter the start service command, the program runs, but now my terminal is stuck waiting for the program.

I thought if you run something as a service then it should be running in the background?

Also I can't stop this service unless I open a second terminal. I kind of want it so that when I start the service it starts somewhere in the background and then you return to the command line. Am I doing something wrong here?

  • 2
    Don't use an init.d service. Write an Upstart job instead. Here's an example: askubuntu.com/a/581869/158442. Upstart will take care of keeping the program in the background, logging, etc. – muru Aug 10 '15 at 15:48
  • @muru Thanks - this looks far more simple. How do I stop / re-start the service (lets say it hangs/crashes)? – code_fodder Aug 10 '15 at 16:05
  • service mcp restart, or restart mcp, if your Upstart configuration is in /etc/init/mcp.conf – muru Aug 10 '15 at 16:07
  • @muru I basically copied your code example (with my executable path) - do I need to add some "upstart" line in there too? – code_fodder Aug 10 '15 at 16:10
  • 1
    @code_fodder forgot sudo? – muru Aug 10 '15 at 16:32
2

Don't use an init.d service. Write an Upstart job instead. Here's an example. Upstart will take care of keeping the program in the background, logging, etc.

Your service would be in /etc/init/mcp.conf, containing, for example:

start on runlevel [2345]
stop  on runlevel [016]

exec /usr/bin/mcp -d f -c /etc/mcp

Then you can do:

sudo service mcp start
sudo service mcp stop

And so on.

|improve this answer|||||
  • 2
    lol, thanks very much - that must be a very delayed reaction from nearly a year ago! ... I would also up-vote you, but I spent my points on a bounty so I can upvote : ( – code_fodder Jun 5 '16 at 21:21
  • Eh, that's alright. I just answered it now to remove it from the unanswered list. :D – muru Jun 5 '16 at 21:25
  • 1
    Got my points now, so +1 : p – code_fodder Jun 6 '16 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.