我可以通过编辑文件在 Ubuntu 中执行以下操作:
/etc/rc.local
Run Code Online (Sandbox Code Playgroud)
并添加:
IP=$(/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')
echo "IP: $IP" > /etc/issue
Run Code Online (Sandbox Code Playgroud)
在 Arch 中,这个文件不存在“/etc/rc.local”,经过一番搜索,我发现我必须创建这个文件:
/etc/systemd/system/rc-local.service
Run Code Online (Sandbox Code Playgroud)
内容:
[Unit]
Description=/etc/rc.local compatibility
[Service]
Type=oneshot
ExecStart=/etc/rc.local
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
然后创建“/etc/rc.local”。
内容:
IP=$(/sbin/ip route get 1 | awk '{print $NF;exit}')
echo "IP: $IP" > /etc/issue
exit 0
Run Code Online (Sandbox Code Playgroud)
然后使其可执行:
sudo chmod +x /etc/rc.local
Run Code Online (Sandbox Code Playgroud)
最后开始/测试:
sudo systemctl start rc-local.service
Run Code Online (Sandbox Code Playgroud)
得到错误:
Job for rc-local.service failed because the control process exited with …Run Code Online (Sandbox Code Playgroud) 我在~/.config/systemd/user工作中登录时创建了以下服务文件来备份我的工作数据。
[Unit]
Description=Backup Work Related Files
[Service]
Type=oneshot
ExecStartPre=/usr/bin/ping -c 1 -W 1 xxx
ExecStart=/usr/bin/rsync -avzuhH --progress --delete --exclude=.Trash-1000 /some/folder/ xxx@xxx:Documents/backup
ExecStart=/usr/bin/rsync -avzuhH --progress file xxx@xxx:Documents/backup/file
Run Code Online (Sandbox Code Playgroud)
我可以用 找到服务,systemctl --user list-unit-files我也可以用 开始服务systemctl --user start backup_work.service。出于某种原因,虽然我无法使用journalctl --user -u backup_work.service. 我在日志中正常查看时会看到 rsync 和 ping 的日志,但无法过滤到我的服务文件。
我需要在服务文件中更改某些内容吗?
我正在尝试在 CentOS 7.0 服务器(未安装 X11)上运行 systemd 用户模式。例如
systemctl --user start hw.service
Run Code Online (Sandbox Code Playgroud)
它的反应是:
Failed to get D-Bus connection: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11
Run Code Online (Sandbox Code Playgroud)
如果我添加
export DISPLAY=:0
Run Code Online (Sandbox Code Playgroud)
再次运行,它报告:
Failed to get D-Bus connection: /bin/dbus-launch terminated abnormally without any error message
Run Code Online (Sandbox Code Playgroud)
任何人都可以阐明这一点吗?顺便说一句, systemctl --version 报告:
systemd 208
+PAM +LIBWRAP +AUDIT +SELINUX +IMA +SYSVINIT +LIBCRYPTSETUP +GCRYPT +ACL +XZ
Run Code Online (Sandbox Code Playgroud)
谢谢!
我的服务器上运行着一个备份守护进程,它每隔几天就会崩溃一次。我不知道为什么。从长远来看,我想找出原因并修复它,但同时我希望 systemd 在崩溃时重新启动它。
它有一个老式的 SysV 初始化脚本,它被 systemd-sysv-generator 接收。显然,当它崩溃时,它会使用零(“成功”)退出代码。为了尝试在这些崩溃后重新启动它,我输入了一个override.conf:
~$ cat /etc/systemd/system/crashplan.service.d/override.conf
[Service]
Restart=always
Run Code Online (Sandbox Code Playgroud)
systemd 似乎正在解决这个问题:
roberts:~$ sudo systemctl show crashplan.service | grep Restart
Restart=always
RestartUSec=100ms
Run Code Online (Sandbox Code Playgroud)
尽管如此,当我在几天后检查它时,我发现:
roberts:~$ sudo systemctl status crashplan.service
? crashplan.service - LSB: CrashPlan Engine
Loaded: loaded (/etc/init.d/crashplan; bad; vendor preset: enabled)
Drop-In: /etc/systemd/system/crashplan.service.d
??override.conf
Active: active (exited) since Thu 2017-01-05 00:33:50 PST; 5 days ago
Docs: man:systemd-sysv-generator(8)
Jan 05 00:33:50 roberts systemd[1]: Stopped LSB: CrashPlan Engine.
Jan 05 00:33:50 roberts systemd[1]: Starting LSB: …Run Code Online (Sandbox Code Playgroud) 我没有找到这样的问题。我有一个启动systemd-nspawn会话的 bash 脚本(在 Debian 中运行)。之后,我想在生成的机器中继续我的脚本:
#!/bin/bash
systemd-nspawn -q --bind /usr/bin/qemu-arm-static -D /mnt/project /bin/bash
apt-get update
apt-get -y upgrade
Run Code Online (Sandbox Code Playgroud)
执行systemd-nspawn命令后,脚本被挂起,控制台的提示在生成的系统内部。当我退出脚本恢复但在本地机器中。
根据本指南,我在 Raspberry Pi 上设置了pulseaudio 以在系统模式下运行。这是我的 systemd 服务文件的样子:
[Unit]
Description=PulseAudio Daemon
After=sound.target
Requires=sound.target
[Install]
WantedBy=default.target
[Service]
Type=notify
ExecStart=/usr/bin/pulseaudio --disallow-exit --no-cpu-limit --daemonize=no --system --realtime --disallow-module-loading --log-target=journal -vvvv
Run Code Online (Sandbox Code Playgroud)
但是,我没有收到任何声音 - 根据日志,pulseaudio 启动时,udev-detect 报告“找到 0 张卡”。我只得到空接收器。运行systemctl --system restart pulseaudio.service修复了这个问题,我的音频设备被正确检测到。
有没有办法告诉pulseaudio等到检测到设备?
我安装了 Linux Mint 18.3,但似乎没有启用休眠:
$ sudo systemctl hibernate
Failed to hibernate system via logind: Sleep verb not supported
Run Code Online (Sandbox Code Playgroud)
这是因为我有点晦涩的硬件没有 Linux 知道如何与之通信的资源,还是有一种方法可以配置事物,以便最终我可以按下电源按钮并使计算机休眠?
这是我花了一整天的奇怪问题。如果有人能对此有所了解,那就太好了。
该问题表现为名称解析问题,但我不确定这是根本原因:
# host www.google.com
;; connection timed out; no servers could be reached
Run Code Online (Sandbox Code Playgroud)
到目前为止很无聊,但是等等!:
#systemd-resolve www.google.com
www.google.com: 209.85.202.103
209.85.202.106
209.85.202.105
209.85.202.104
209.85.202.147
209.85.202.99
Run Code Online (Sandbox Code Playgroud)
很简单,是resolv.conf的问题吧?
# This file is managed by man:systemd-resolved(8). Do not edit.
#
# 127.0.0.53 is the systemd-resolved stub resolver.
# run "systemd-resolve --status" to see details about the actual nameservers.
nameserver 127.0.0.53
search xxx.uk xyz
Run Code Online (Sandbox Code Playgroud)
那么系统使用的是systemd解析器吗?
#dig @127.0.0.53 www.google.com
; <<>> DiG 9.10.3-P4-Ubuntu <<>> @127.0.0.53 www.google.com
; (1 server found)
;; global options: +cmd
;; connection …Run Code Online (Sandbox Code Playgroud) 我通过 systemd 单元处理由 docker 容器实现的许多服务(数据库、Web 服务器...)。它可以工作,但是系统需要很长时间才能关闭,显然是在等待 docker 服务关闭。
这是我在 Debian Stretch 下使用 Docker CE 在虚拟机中制作的 systemd 单元的示例:
/lib/systemd/system/mariadb.service
[Unit]
Description=MariaDB
After=docker.service
Requires=docker.service
[Service]
TimeoutStartSec=0
Restart=always
ExecStart=/usr/bin/docker run --rm \
--name=mariadb \
-p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=root \
-e MYSQL_DATABASE=test \
-e MYSQL_USER=user \
-e MYSQL_PASSWORD=user \
-v /var/lib/mysql:/var/lib/mysql \
mariadb:latest
ExecStop=-/usr/bin/docker stop mariadb # See UPDATEs 1 & 2
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
当我使用 systemctl 命令时,服务会在几秒钟内正确启动和停止。但是当我关闭系统时,它显示:
持续时间超过3分钟。如果有许多容器通过 systemd 运行,则可能会更长。
该问题并非特定于 MariaDB。
有没有更好的方法来通过 systemd 处理 docker 容器,避免额外的关机时间?
更新 1:正如 Bennett Hardwick 在评论中建议的那样,我尝试从服务的定义中删除 ExecStop …
我正在使用 Debian 9.8
我有一个运行我的 dotnet 程序的服务。我想用 monit 监视它,但在所有示例中,您都需要在 /var/run 中引用 .pid 文件,但我的 dotnet 程序在 /var/run 中没有 .pid 文件。
所以我将 PIDFile=/var/run/testservice.pid 添加到我的服务的 .service 文件中,但是当我启动它时它没有创建文件。
这就是我所在的地方
这是我的 .service 文件
[Unit]
Description=Test Service
Wants=network-online.target influxdb.service
After=network-online.target influxdb.service
[Service]
User=testservice
Group=mainapp
SyslogIdentifier=testservice
PIDFile=/var/run/testservice.pid
Restart=on-failure
TimeoutStopSec=5
RestartSec=10
ExecStart=/usr/bin/dotnet /mainapp/app/TestSystemdService.dll
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud) systemd ×10
linux ×4
bash ×2
arch-linux ×1
audio ×1
centos ×1
command-line ×1
debian ×1
dns ×1
docker ×1
hibernate ×1
linux-mint ×1
monit ×1
pid ×1
pulse-audio ×1
resolv.conf ×1
ubuntu ×1