Tuesday, January 18, 2011

To restart a service (e.g httpd), should I use /etc/init.d httpd restart or /sbin/service httpd restart?

Whenever I need to restart Apache on my RHEL VPS, I type sudo /sbin/service httpd restart. However I notice a lot of articles suggest using sudo /etc/init.d httpd restart.

Is there any difference? If there is, when I should use each one?

  • Using /sbin/service is good because it gives the daemon a fresh environment to work in, without any potentially-annoying environment variables getting in the way and causing havoc. At least, that's the rationale for requiring it at work; I've always used the init scripts directly on my other-distro machines and it hasn't caused a visible problem, but I'm assured that there was a real problem that /sbin/service is working around. Perhaps RHEL systems just have more crap laying around in the default environment.

    From womble
  • From the man page:

    service runs a System V init script in as predictable environment as possible, removing most environment variables and with current working directory set to /.

    It then calls the init.d script. So they both accomplish the same thing, except calling the script via /sbin/service just ensures that some environment variables in your shell don't screw up the init.d script.

    From Insyte
  • My advice would be to use whatever you want whenever you're actually logged in to the machine, and use /etc/(init.d|rc.d)/daemon-name if you are scripting, the reason being the latter is generally more portable. IIRC, the only distros that come with /sbin/service in the base packages install are RH-flavored, i.e. RHEL, CentOS, Fedora. My Debian systems for example do not have this script, however this may not matter for your environment.

    Zoredache : Debian-based systems have 'invoke-rc.d' which fills the same roll as service.
    serverninja : There really should be a distribution-independent way of doing this.

0 comments:

Post a Comment