如何仅显示作为服务运行的Shell脚本的输出?
我编写了一个shell脚本并将其安装在SystemCtl上以在启动上运行,一切正常,但是,如果我运行“ SystemCtl状态MyService”,它将显示我的所有脚本源代码,而仅我的脚本的输出,我的脚本看起来与此相似(如一个示例):
while true
do
echo "pinging..."
ping -c 10 google.com
.... blah blah ....
.... blah blah ....
done
如果我执行SystemCtl状态myService,则显示我的代码“ ping ...”和ping输出,我该如何执行SystemCtl仅显示输出而不是代码?
这是我的SystemCTL状态和SystemCTL CAT输出:
$ SystemCtl状态检查器:
● checkupserver.service - Check Servers Online Service
Loaded: loaded (/etc/systemd/system/checkupserver.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2022-05-09 20:54:21 UTC;
Main PID: 471544 (bash)
Tasks: 2 (limit: 9054)
Memory: 1.2M
CGroup: /system.slice/checkupserver.service
├─ 571033 /bin/checkservers.sh >
echo "Starting Service..."
while true
do
echo "pinging server..."
ping -c 10 serverip
... blah blah ...
done
/bin/checkservers.sh
└─4785411 /bin/checkservers.sh >
echo "Starting Service..."
while true
do
echo "pinging server..."
ping -c 10 serverip
... blah blah ...
done
/bin/checkservers.sh
$ SystemCtl Cat CheckupServer:
# /etc/systemd/system/checkupserver.service
[Unit]
Description=Check Servers Online Service
[Service]
User=checker
ExecStart=/bin/checkservers.sh
Restart=always
RestartSec=10
TimeoutStopSec=90
KillMode=process
SyslogIdentifier=CheckServers
[Install]
WantedBy=multi-user.target
I write a shell script and installed it on systemctl to run on startup, everything works fine, however if i run "systemctl status myservice" it shows all my script source code instead only the outputs of my script my script looks similar to this (as a example):
while true
do
echo "pinging..."
ping -c 10 google.com
.... blah blah ....
.... blah blah ....
done
if i do systemctl status myservice it show my code instead "pinging..." and the ping output, how can i do that systemctl only shows the output instead the code?
Here is my systemctl status and systemctl cat outputs:
$systemctl status checkupserver:
● checkupserver.service - Check Servers Online Service
Loaded: loaded (/etc/systemd/system/checkupserver.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2022-05-09 20:54:21 UTC;
Main PID: 471544 (bash)
Tasks: 2 (limit: 9054)
Memory: 1.2M
CGroup: /system.slice/checkupserver.service
├─ 571033 /bin/checkservers.sh >
echo "Starting Service..."
while true
do
echo "pinging server..."
ping -c 10 serverip
... blah blah ...
done
/bin/checkservers.sh
└─4785411 /bin/checkservers.sh >
echo "Starting Service..."
while true
do
echo "pinging server..."
ping -c 10 serverip
... blah blah ...
done
/bin/checkservers.sh
$systemctl cat checkupserver:
# /etc/systemd/system/checkupserver.service
[Unit]
Description=Check Servers Online Service
[Service]
User=checker
ExecStart=/bin/checkservers.sh
Restart=always
RestartSec=10
TimeoutStopSec=90
KillMode=process
SyslogIdentifier=CheckServers
[Install]
WantedBy=multi-user.target
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只是收到脚本的打印输出,因为您还没有告诉它如何从外壳中运行。
因此,您在这里有两个选择。
。告诉您的服务文件如何执行
两个选项中的任何一个都会为您提供以下输出:
You're just receiving the printout of the script because you haven't told it how it can be run from your shell.
So you have two options here.
or 2. Tell your service file how to execute the script
Either of the two options will give you the below output: