linux – Systemd退出bash脚本,执行导致失败的命令,而不是继续
发布时间:2020-12-14 02:41:29 所属栏目:Linux 来源:网络整理
导读:我正在尝试获取一个脚本,将我在CoreOS上的系统日志推送到logentries.为了弥补实例在AWS上运行时没有立即连接互联网的事实,我将命令停留在while循环中. 从命令行运行脚本while循环工作正常.但是当systemd运行它时,当netcat超时时它会立即退出,所以它再也没有
我正在尝试获取一个脚本,将我在CoreOS上的系统日志推送到logentries.为了弥补实例在AWS上运行时没有立即连接互联网的事实,我将命令停留在while循环中.
从命令行运行脚本while循环工作正常.但是当systemd运行它时,当netcat超时时它会立即退出,所以它再也没有机会再试一次. 有没有办法让systemd在退出脚本时不那么积极? systemd输出,永远不会到“睡觉netcat” Jul 23 22:26:21 core-01 systemd[1]: Starting Push journal logs to logentries.com... Jul 23 22:26:21 core-01 systemd[1]: Started Push journal logs to logentries.com. Jul 23 22:26:21 core-01 bash[880]: trying netcat Jul 23 22:26:31 core-01 bash[880]: Ncat: Connection timed out. journal2logentries.sh #!/usr/bin/env bash token=logentriestoken while true do echo 'trying netcat' journalctl -o short -f | awk -v token=$token '{ print token,$0; fflush(); }' | ncat --ssl --ssl-verify data.logentries.com 20000 echo 'sleeping netcat' sleep 30s done logentries.service [Unit] Description=Push journal logs to logentries.com After=systemd-journald.service After=systemd-networkd.service [Service] Restart=always ExecStart=/bin/bash /home/core/journal2logentries.sh [Install] WantedBy=multi-user.target 更新: 似乎真正的问题是,当netcat死掉系统时,/ bin / sh进程仍在运行.注意:url故意不正确测试 logentries.service - Push journal logs to logentries.com Loaded: loaded (/etc/systemd/system/logentries.service; disabled) Active: active (running) since Mon 2014-07-28 17:12:04 UTC; 1min 48s ago Main PID: 16305 (sh) CGroup: /system.slice/logentries.service ├─16305 /bin/sh -c journalctl -o short -f | awk -v token=token_here '{ print token,$0; fflush(); }' | ncat --ssl --ssl-verify -vv ogentries.com 20000 ├─16306 journalctl -o short -f └─16307 awk -v token=80b4b3b6-1315-4b76-ac69-f530c1dec47f { print token,$0; fflush(); } Jul 28 17:12:04 ip-172-31-19-155.us-west-2.compute.internal systemd[1]: logentries.service holdoff time over,scheduling restart. Jul 28 17:12:04 ip-172-31-19-155.us-west-2.compute.internal systemd[1]: Stopping Push journal logs to logentries.com... Jul 28 17:12:04 ip-172-31-19-155.us-west-2.compute.internal systemd[1]: Starting Push journal logs to logentries.com... Jul 28 17:12:04 ip-172-31-19-155.us-west-2.compute.internal systemd[1]: Started Push journal logs to logentries.com. Jul 28 17:12:04 ip-172-31-19-155.us-west-2.compute.internal sh[16305]: Ncat: Version 6.40 ( http://nmap.org/ncat ) Jul 28 17:12:04 ip-172-31-19-155.us-west-2.compute.internal sh[16305]: Ncat: Could not resolve hostname "ogentries.com": Name or service not known. QUITTING. 解决方法
从管道切换到处理替换.
http://paraf.in/abs-guide/process-sub.html https://stackoverflow.com/a/18360260/136408 这是我提出的单元文件: logentries.service [Unit] Description=Push journal logs to logentries.com After=systemd-journald.service After=systemd-networkd.service [Service] Restart=always RestartSec=30s ExecStart=/bin/bash -c "ncat --ssl --ssl-verify data.logentries.com 20000 < <(awk -v token=token_here '{ print token,$0; fflush(); }' < <(journalctl -o short -f))" [Install] WantedBy=multi-user.target (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- embedded – Initrd,Ramdisk,Initramfs,uclinux
- linux – 多管理员root访问权限的最佳实践
- linux – 启用kerberos时在本地锁定用户的帐户
- linux – Serial Over Lan不重定向OS终端
- linux – SSH服务器零日攻击 – 保护自己的建议
- gpg:no valid openpgp data found
- 在Linux中将软件限制设置为RAM
- ____cacheline_aligned_in_smp用于linux中的结构
- linux – 缺少磁盘空间
- active-directory – Linux AD集成,使用Windows Server 201