大网数据广告活动
服务器运维问答

How to silent a systemd service?

· 52CE 我爱测

I've created the following systemd service: [Unit] Description=ISPConfig DC Sync After=network.target After=mysql.service After=nginx.service [Service] User=root Group=root Type=simple ExecStart=/usr/local/ispconfig/server/server…

问题原文

原帖链接 · Diogo Braga · CC BY-SA 3.0

I've created the following systemd service:

[Unit]
Description=ISPConfig DC Sync
After=network.target
After=mysql.service
After=nginx.service

[Service]
User=root
Group=root
Type=simple
ExecStart=/usr/local/ispconfig/server/server.sh

[Install]
WantedBy=multi-user.target

The (.sh) command has a loop that returns a message stating whether it had an error or was successful. The problem is that this result is printed in the messages (/var/log/messages) log. There's a way to prevent this? (Preferably in the specifications of the own service)

社区高分解答

原帖链接 · Michael Hampton · CC BY-SA 4.0

The documentation states that you can set StandardOutput= and StandardError= to whatever you want. They default to the journal, but you can redirect them to any of several places.

Controls where file descriptor 1 (STDOUT) of the executed processes is connected to. Takes one of inherit, null, tty, journal, syslog, kmsg, journal+console, syslog+console, kmsg+console, file:path, append:path, truncate:path, socket or fd.

...

null connects standard output to /dev/null, i.e. everything written to it will be lost.

StandardError= is similar, with an important exception to how it inherits (which is also the default):

Controls where file descriptor 2 (STDERR) of the executed processes is connected to. The available options are identical to those of StandardOutput=, with some exceptions: if set to inherit the file descriptor used for standard output is duplicated for standard error, while fd operates on the error stream and will look by default for a descriptor named "stderr".

So, you can do this:

[Service]
StandardOutput=null
StandardError=journal

But keep in mind that you will lose the output from the script; more importantly, if there is a problem and the script outputs an error on standard output instead of standard error (as many shell scripts do), you will lose that as well. When something goes wrong, this will cause you a whole lot of unnecessary frustration.

So, you really should do nothing to the systemd unit. Rather, if you don't want the script to output anything on success, fix the script.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-03-16)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。