如何仅显示作为服务运行的Shell脚本的输出?

发布于 2025-01-29 00:42:44 字数 1646 浏览 3 评论 0原文

我编写了一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

场罚期间 2025-02-05 00:42:44

您只是收到脚本的打印输出,因为您还没有告诉它如何从外壳中运行。

因此,您在这里有两个选择。

  1. 更改您的脚本,使其在开始时具有适当的Shebang
#!/bin/bash

while true
do
   echo "pinging..."
   ping -c 10 google.com
   .... blah blah ....
   .... blah blah ....
done

。告诉您的服务文件如何执行

# /etc/systemd/system/checkupserver.service
[Unit]
Description=Check Servers Online Service

[Service]
User=checker
ExecStart=/bin/bash -c /bin/checkservers.sh
Restart=always
RestartSec=10
TimeoutStopSec=90
KillMode=process
SyslogIdentifier=CheckServers

[Install]
WantedBy=multi-user.target

两个选项中的任何一个都会为您提供以下输出:

root@var-som-mx6:/tmp# systemctl status checkupserver.service 
● checkupserver.service - Check Servers Online Service
   Loaded: loaded (/tmp/checkupserver.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 1970-01-05 20:11:38 UTC; 4s ago
 Main PID: 3645 (bash)
   CGroup: /system.slice/checkupserver.service
           ├─3645 /bin/bash -c /tmp/checkservers.sh
           └─3646 ping -c 10 10.7.0.50

Jan 05 20:11:38 var-som-mx6 systemd[1]: Started Check Servers Online Service.
Jan 05 20:11:39 var-som-mx6 CheckServers[3645]: pinging...
Jan 05 20:11:39 var-som-mx6 CheckServers[3645]: PING 10.7.0.50 (10.7.0.50): 56 data bytes
Jan 05 20:11:39 var-som-mx6 CheckServers[3645]: 64 bytes from 10.7.0.50: seq=0 ttl=64 time=0.406 ms
Jan 05 20:11:40 var-som-mx6 CheckServers[3645]: 64 bytes from 10.7.0.50: seq=1 ttl=64 time=0.463 ms
Jan 05 20:11:41 var-som-mx6 CheckServers[3645]: 64 bytes from 10.7.0.50: seq=2 ttl=64 time=0.369 ms
Jan 05 20:11:42 var-som-mx6 CheckServers[3645]: 64 bytes from 10.7.0.50: seq=3 ttl=64 time=0.399 ms
Jan 05 20:11:43 var-som-mx6 CheckServers[3645]: 64 bytes from 10.7.0.50: seq=4 ttl=64 time=0.453 ms

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.

  1. Change your script so it has the appropriate shebang at the start
#!/bin/bash

while true
do
   echo "pinging..."
   ping -c 10 google.com
   .... blah blah ....
   .... blah blah ....
done

or 2. Tell your service file how to execute the script

# /etc/systemd/system/checkupserver.service
[Unit]
Description=Check Servers Online Service

[Service]
User=checker
ExecStart=/bin/bash -c /bin/checkservers.sh
Restart=always
RestartSec=10
TimeoutStopSec=90
KillMode=process
SyslogIdentifier=CheckServers

[Install]
WantedBy=multi-user.target

Either of the two options will give you the below output:

root@var-som-mx6:/tmp# systemctl status checkupserver.service 
● checkupserver.service - Check Servers Online Service
   Loaded: loaded (/tmp/checkupserver.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 1970-01-05 20:11:38 UTC; 4s ago
 Main PID: 3645 (bash)
   CGroup: /system.slice/checkupserver.service
           ├─3645 /bin/bash -c /tmp/checkservers.sh
           └─3646 ping -c 10 10.7.0.50

Jan 05 20:11:38 var-som-mx6 systemd[1]: Started Check Servers Online Service.
Jan 05 20:11:39 var-som-mx6 CheckServers[3645]: pinging...
Jan 05 20:11:39 var-som-mx6 CheckServers[3645]: PING 10.7.0.50 (10.7.0.50): 56 data bytes
Jan 05 20:11:39 var-som-mx6 CheckServers[3645]: 64 bytes from 10.7.0.50: seq=0 ttl=64 time=0.406 ms
Jan 05 20:11:40 var-som-mx6 CheckServers[3645]: 64 bytes from 10.7.0.50: seq=1 ttl=64 time=0.463 ms
Jan 05 20:11:41 var-som-mx6 CheckServers[3645]: 64 bytes from 10.7.0.50: seq=2 ttl=64 time=0.369 ms
Jan 05 20:11:42 var-som-mx6 CheckServers[3645]: 64 bytes from 10.7.0.50: seq=3 ttl=64 time=0.399 ms
Jan 05 20:11:43 var-som-mx6 CheckServers[3645]: 64 bytes from 10.7.0.50: seq=4 ttl=64 time=0.453 ms
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文