luv*_*eet 243 command-line services systemd
systemd
为我们提供了systemctl
命令套件,该套件主要用于使服务在启动时启动。我们还可以在 的帮助下启动、停止、重新加载、重新启动和检查服务的状态systemctl
。
例如,我们可以这样做,sudo systemctl enable service_name
并且service_name
会在启动时自动启动。我们还可以禁用在启动时不启动的服务。
service
和systemctl
命令之间的唯一区别是systemctl
可用于在运行时启动服务吗?我们可以systemctl
在任何服务上使用吗?还有哪些其他显着差异?
mur*_*uru 236
The service
command is a wrapper script that allows system administrators to start, stop, and check the status of services without worrying too much about the actual init system being used. Prior to systemd's introduction, it was a wrapper for /etc/init.d
scripts and Upstart's initctl
command, and now it is a wrapper for these two and systemctl
as well.
It checks for Upstart:
# Operate against system upstart, not session
unset UPSTART_SESSION
if [ -r "/etc/init/${SERVICE}.conf" ] && which initctl >/dev/null \
&& initctl version 2>/dev/null | grep -q upstart \
&& initctl status ${SERVICE} 2>/dev/null 1>/dev/null
then
# Upstart configuration exists for this job and we're running on upstart
Run Code Online (Sandbox Code Playgroud)
If that doesn't work, it looks for systemd:
if [ -d /run/systemd/system ]; then
is_systemd=1
fi
...
# When this machine is running systemd, standard service calls are turned into
# systemctl calls.
if [ -n "$is_systemd" ]
then
Run Code Online (Sandbox Code Playgroud)
And if that fails as well, it falls back to System V /etc/init.d
scripts:
run_via_sysvinit() {
# Otherwise, use the traditional sysvinit
if [ -x "${SERVICEDIR}/${SERVICE}" ]; then
exec env -i LANG="$LANG" LANGUAGE="$LANGUAGE" LC_CTYPE="$LC_CTYPE" LC_NUMERIC="$LC_NUMERIC" LC_TIME="$LC_TIME" LC_COLLATE="$LC_COLLATE" LC_MONETARY="$LC_MONETARY" LC_MESSAGES="$LC_MESSAGES" LC_PAPER="$LC_PAPER" LC_NAME="$LC_NAME" LC_ADDRESS="$LC_ADDRESS" LC_TELEPHONE="$LC_TELEPHONE" LC_MEASUREMENT="$LC_MEASUREMENT" LC_IDENTIFICATION="$LC_IDENTIFICATION" LC_ALL="$LC_ALL" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" ${ACTION} ${OPTIONS}
else
echo "${SERVICE}: unrecognized service" >&2
exit 1
fi
}
...
run_via_sysvinit
Run Code Online (Sandbox Code Playgroud)
Since the service
command is a fairly simple wrapper, it only supports a limited subset of actions compared to what the actual init system might provide.
For portability over various versions of Ubuntu, users can reliably use the service
command to start, stop, restart or examine the status of a service. For more complex tasks, however, the actual command being used, be that initctl
or systemctl
or the /etc/init.d
script might have to be used directly.
Further, being a wrapper, the service
script in some cases also does more than the direct equivalent command might do. For example:
/etc/init.d
scripts in a clean environment. (Note the long env
command invocation in the run_via_sysvinit
function above.)restart
on Upstart systems to a combination of stop
/start
, since a plain initctl restart
will error out if the service isn't running already.它在停止具有关联套接字的 systemd 服务时停止套接字:
case "${ACTION}" in
restart|status)
exec systemctl $sctl_args ${ACTION} ${UNIT}
;;
start|stop)
# Follow the principle of least surprise for SysV people:
# When running "service foo stop" and foo happens to be a service that
# has one or more .socket files, we also stop the .socket units.
# Users who need more control will use systemctl directly.
Run Code Online (Sandbox Code Playgroud)Upstart 服务直接在服务配置文件中启用(或通过覆盖禁用),并且 System V 脚本使用update-rc.d
命令启用或禁用(管理/etc/rc*
目录中的符号链接),因此该service
命令从未涉及在启动时启用或禁用服务.
Rav*_*ina 45
有很多比你提到systemctl
的能力。
systemd
与单元一起工作,有不同类型的单元:目标、服务、套接字等。目标与运行级别的概念相同,它们是一组单元。
您可以使用systemctl
来设置或获取默认系统目标。
systemctl get-default
Run Code Online (Sandbox Code Playgroud)
您可以进入其他目标:
systemctl isolate multiuser.target
Run Code Online (Sandbox Code Playgroud)
其他目标是:多用户、图形、recue、紧急情况、重启、关机。
正如您所说,您可以systemctl
用来管理服务,我所知道的与服务管理相关的其他一些命令是:
# Restarts a service only if it is running.
systemctl try-restart name.service
# Reloads configuration if it's possible.
systemctl reload name.service
# try to reload but if it's not possible restarts the service
systemctl reload-or-restart name.service
Run Code Online (Sandbox Code Playgroud)
您可以使用它来了解服务状态:
systemctl status name.service
systemctl is-active name.service # running
systemctl is-enabled name.service # will be activated when booting
systemctl is-failed name.service # failed to load
Run Code Online (Sandbox Code Playgroud)
您可以屏蔽或取消屏蔽服务:
systemctl mask name.service
systemctl unmask name.service
Run Code Online (Sandbox Code Playgroud)
你屏蔽了一个服务,它会链接到/dev/null
,所以手动或自动其他服务不能激活/启用它。(你应该先揭开它的面具)。
systemctl 的另一个用法是列出单位:
systemctl list-units
Run Code Online (Sandbox Code Playgroud)
其中列出了所有类型的单位,已加载和活动。
列出服务单位:
systemctl list-units --type=service
Run Code Online (Sandbox Code Playgroud)
或者列出所有可用的单位,而不仅仅是加载和激活的单位:
systemctl list-unit-files
Run Code Online (Sandbox Code Playgroud)
您可以创建别名甚至控制远程机器
systemctl --host ravexina@192.168.56.4 list-units
Run Code Online (Sandbox Code Playgroud)
另一方面service
做它必须做的事情,管理服务并且与其他人的业务无关;)
归档时间: |
|
查看次数: |
160589 次 |
最近记录: |