What is needed for a linux service to be supported by chkconfig?

1,754

Solution 1

The script must have 2 lines:

# chkconfig: <levels> <start> <stop>
# description: <some description>

for example:

# chkconfig: 345 99 01
# description: some startup script

345 - levels to configure
99 - startup order
01 - stop order

After you add the above headers you can run chkconfig --add <service>.

Solution 2

While katriel has already answered this with the bare minimum needed to create an init script, I think you'd also be well served with looking at /etc/init.d/skeleton and using that as a template on which to base your init script. You'll end up with a much more consistent and readable script.

Solution 3

It sounds like Geo's specific problem has already been solved, but I ran into a similar message while trying to set up a Rails app with sidekiq as a managed service. I'll explain my solution here in case it helps any other newbies like me.

I'm working on a CentOS install, and chkconfig is already set up with several other services like httpd, mysql, and redis. Note that most services need only be enabled on runlevels 3 through 5.

chkconfig --list
> httpd             0:off   1:off   2:on    3:on    4:on    5:on    6:off
> mysqld            0:off   1:off   2:on    3:on    4:on    5:on    6:off
> redis-server      0:off   1:off   2:on    3:on    4:on    5:on    6:off
> (etc...)

I needed to add a new script for the sidekiq service, so I grabbed the script at https://gist.github.com/CD1212/5326706, modified it to fit my app's parameters, and saved it at /etc/rc.d/init.d/sidekiq (owned by root like all the other scripts there).

However when I tried to register this new service, I got the chkconfig error:

sudo chkconfig --add sidekiq
> service sidekiq does not support chkconfig

After some extra reading I discovered that the priority numbers defined at the top of each chkconfig script must be unique. A clearer error message would have been nice! Another script had shutdown priority level 75, so I changed mine to 76 and tried again. Here's the head of my init script:

#!/bin/bash
#
# sidekiq    Init script for Sidekiq
#
# chkconfig: 345 99 76
# processname: sidekiq
# pidfile: /var/www/visual_testing_tool/sidekiq.pid
# description: Starts and Stops Sidekiq message processor for the Rails app.
#

This time, sudo chkconfig --add sidekiq gave no complaint. Then when I ran sudo chkconfig --list sidekiq, the sidekiq service was shown as on for the appropriate runlevels.

Share:
1,754
Mike Moore
Author by

Mike Moore

Updated on September 17, 2022

Comments

  • Mike Moore
    Mike Moore almost 2 years

    Which would you recommend?

    1. Return an error code, such as E_USER_ERROR from a function, and determine proper message higher up:

      function currentScriptFilename()
      {
        if(!isset($_SERVER['SCRIPT_FILENAME']))
        {
          //This?
          return E_USER_ERROR;
        }
        else
        {
          $url = $_SERVER['SCRIPT_FILENAME'];
          $exploded = explode('/', $url);
          return end($exploded);
        }
      }
      
    2. Execute trigger_error() from the function, with a specific error message:

      function currentScriptFilename()
      {
        if(!isset($_SERVER['SCRIPT_FILENAME']))
        {
          //Or this?
          trigger_error('$_SERVER[\'SCRIPT_FILENAME\'] is not set.', E_USER_ERROR);
        }
        else
        {
          $url = $_SERVER['SCRIPT_FILENAME'];
          $exploded = explode('/', $url);
          return end($exploded);
        }
      }
      

    I am not sure if I will regret having put a bunch of error messages in my functions further down the line, since I would like to use them for other projects.

    Or, would you recommend something totally different?

    • Jeff Finn
      Jeff Finn about 15 years
      is your service in init.d/
  • katriel
    katriel about 15 years
    The extra space on the second line was added by markdown, it is not needed
  • Mike Moore
    Mike Moore about 14 years
    Would you mind explaining why you would use exceptions in this case?
  • Ignacio Vazquez-Abrams
    Ignacio Vazquez-Abrams about 14 years
    They provide an unambiguous indication that you've hit an exceptional situation. They can't be implicitly ignored, only handled somehow.
  • Mike Moore
    Mike Moore about 14 years
    Are you saying that I should use exceptions in this specific example? Or across the board?
  • alex
    alex about 14 years
    Well if you are using PHP5 then I would go with exceptions.
  • Your Common Sense
    Your Common Sense about 14 years
    @letseatfood across the board but not in this simple case. Exceptions mostly used to handle errors, to control the program flow, rather than for the the error notification.
  • Your Common Sense
    Your Common Sense about 14 years
    well the question remains the same - where to throw an exception - inside or outside :)
  • Andrew Moore
    Andrew Moore about 14 years
    @Col. Shrapnel: Inside the function.
  • Mike Moore
    Mike Moore about 14 years
    Thanks for pointing out basename! Your example of throwing exceptions is very helpful. I have been spending the last week reading articles and trying out handling errors and exceptions in my projects.
  • Christian
    Christian over 13 years
    Adding to your comment; you usually trigger/raise/throw errors, not return them. However, you may return success states (true/false, file/null, resource/null, object/null ...).
  • user239558
    user239558 about 5 years
    The above doesn't seem to add the proper kill scripts though. I need chkconfig <service> --level 06 off to explicitly get the kill scripts.