0

When starting, restarting an apache server is there a difference between the following commands?

sudo service apache2 restart

sudo service apache2 stop sudo service apache2 start

AND

sudo systemctl restart apache2

sudo systemctl stop apache2 sudo systemctl start apache2

I'm just learning a little bit about Linux servers by playing around with the LAMP stack, but I want to know why certain guides use one syntax and other guides the other.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • 1
    I swapped your stop and start directives around, since otherwise it wouldn't make sense. Please revert if you really mean start then stop – Chris Davies Dec 13 '21 at 13:23
  • 1
    the Fedora page slm links there says "Note that all /sbin/service and /sbin/chkconfig lines listed above continue to work on systemd, and will be translated to native equivalents as necessary. The only exception is chkconfig --list.". I'm not sure what the other answer means with "there may be a few services left in /etc/init.d/ which you could control with the service command" (as if those are the only ones). I'm pretty sure the service script just maps to systemctl, and that you probably shouldn't run the init scripts manually under systemd. – ilkkachu Dec 13 '21 at 13:33
  • Is the answer for Fedora applicable on Ubuntu-based Pop_OS? – Neil Meyer Dec 13 '21 at 13:38
  • @NeilMeyer, well, I checked, service on Ubuntu is a shell script, and the comments within say "When this machine is running systemd, standard service calls are turned into systemctl calls." It runs stuff like if [ -n "$is_systemd" ]; then ... exec systemctl $sctl_args ${ACTION} ${UNIT}. I'm not sure if there's any other sensible way to do that on systemd, other than call systemctl... (or maybe there's some equivalent dbus call) – ilkkachu Dec 13 '21 at 13:47

1 Answers1

2

I'm just learning a little bit about Linux servers by playing around with the LAMP stack, but I want to know why certain guides use one syntax and other guides the other.

using a sample file out of my /etc/init.d which is for an rlm license manager, the relevant code in that file related to start, stop, restart, and status :

status() {
    pid=`_getpid ${RLM_ROOT}/rlm`
    if [ -z "$pid" ]; then
        echo "rlm is stopped"
        return 3
    fi
    echo "rlm (pid $pid) is running..."
    return 0
}

restart() { stop start }

case "$1" in start) start ;; stop) stop ;; status) status ;; restart) restart ;; *) echo $"Usage: $0 {start|stop|status|restart}" exit 1 esac

As you can see, restart is just a function call that calls stop then start. So that's all restart is and is nothing more than calling stop then start.

Can a init.d or systemctl file actually define restart to be different and more involved than my example? yes, and you would have to look at the code first hand to really know. But it is highly likely they are all very similar to what I posted above, including your apache file, with regards to start | stop | restart | status.

In the context of your question, if you want to stop AND start your apache2 service, then the least amount of typing to accomplish that would be service apache2 restart as opposed to doing two separate commands manually to first stop and then start. But either way you would accomplish the same thing. Also recognize if you do restart then the service will immediately try to be started after it is stopped, regardless if the stop was successful; this may be good or bad depending on what you are doing or debugging. Sometimes its more useful to do a service whatever stop and then have it stopped for however long while you do other things before manually issuing the subsequent service whatever start.

init.d is the old linux way that worked around the service syntax structure and is now replaced by systemd linux and the systemctl syntax structure. But the service whatever <start|stop|restart|status is still supported and will alias to systemctl <start|stop|restart|status whatever. So there is no difference in using service vs systemctl on modern systemd linux systems. If you try to use systemctl on an old init.d type linux system, redhat5 for example, it'll simply be command not found [hopefully] for obvious reasons. You can read web articles explaining initd vs systemd.

It is still perfectly valid, and correct, to use the service whatever <start|stop|status|restart> syntax in documentation as well as typing it on the command line. For documentation & guides it gets the point across in the least amount of characters, and since it aliases pretty much directly to systemctl (which are services) it's just more human readable in my opinion to continue to use that legacy initd service syntax in any documentation.

ron
  • 6,575