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

Solution 1:

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.

Solution 2:

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.

Solution 3:

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.