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.
stop
andstart
directives around, since otherwise it wouldn't make sense. Please revert if you really meanstart
thenstop
– Chris Davies Dec 13 '21 at 13:23service
script just maps tosystemctl
, and that you probably shouldn't run the init scripts manually under systemd. – ilkkachu Dec 13 '21 at 13:33service
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 likeif [ -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 callsystemctl
... (or maybe there's some equivalent dbus call) – ilkkachu Dec 13 '21 at 13:47