我想从我的SystemCtl命令中查看输出。例如:

问题描述 投票:0回答:4
systemctl status systemd-networkd.

我知道我可以编写一个脚本,该脚本总是依次将命令列入命令,但我希望有类似的东西
systemctl --verbose restart ....

没有进入男人页面。
    

对于我的知识,没有这样的事情。话虽如此,您可以继续“让自己拥有”:
我们将编辑BashRC文件以将其添加为一个别名命令

echo "startstat(){ systemctl start \$*; systemctl status \$* }" >> ~/.bashrc

systemctl
4个回答
5
投票
获得状态

startstat [arguments to pass to BOTH systemctl start AND systemctl status]

sample用法:

startstat systemd-networkd
如果您想等一点检查状态,可以随时添加睡眠:

just

nano ~/.bashrc

,滚动到底部(或者如果添加了任何东西,则在哪一行),然后在
sleep [seconds];

systemctl start \$*;

之间添加
systemctl status \$*;

如果您希望在开始结束后运行状态,则可以在

&

\$*
;
之间放置一个奇数
systemctl
标志,以将其分配到背景中。
    

不幸的是,不像大多数Unix命令一样提供“冗长”选项。

一种解决方案是将

SYSTEMD_LOG_LEVEL
提高到
debug
(这里是毫无用处的),然后像e.g.:
一样过滤输出。
info

您还可以添加一个前缀,例如例如

5
投票

$ SYSTEMD_LOG_LEVEL=debug systemctl restart $SERVICES 2>&1|egrep -i "Got result|Failed"|sed 's,^,restart: ,'
restart: Failed to restart xxxxx.service: Unit xxxxx.service not found.
restart: Got result done/Success for job cron.service
restart: Got result done/Success for job smartmontools.service
在所有系统上可能都不可用。

SYSTEMD_LOG_LEVEL
没有
systemctl
选项。如果您想查看您正在实时运行的服务的输出,您可以做的是打开另一个终端并运行:

verbose

journalctl文档:
https://www.freedesktop.org/software/software/systemd/man/journalctl.html


我承认这确实打扰了我,因为从用户体验的POV中,没有消息可以告诉“嘿,看日记!”!是不可接受的

4
投票
可找到调用shell的tty/pts where是允许这样做的代码:

sudo journalctl --unit=systemd-networkd.service -f
确定
systemctl

命令是从shell进程中调用的,并且先前的代码搜索所有

has_parent_process(){ local parent_to_search local ppid parent_to_search="${1:-}" if [ -z "${parent_to_search:-}" ] then echo "ERROR: need parent process pid as first arg" >&2 return 5 fi local pid pid="${2:-}" if [ -z "${pid:-}" ] then pid=$$ fi if [ $parent_to_search = $pid ] then echo ${parent_to_search} return 0 else if [ $pid > 1 ] then ppid=$(ps --pid ${pid} -o ppid= | xargs) if [ $ppid = $pid ] then echo "ERROR: pid=$pid is the same as ppid=$ppid" >&2 echo -1 else has_parent_process ${parent_to_search} ${ppid} fi else echo "NOT FOUND: ${parent_to_search}" >&2 echo 1 fi fi return 1 } find_pid_user_of(){ local used_file=$1 local regex="$2" lsof ${used_file} | awk 'NR>1 && $1 ~ /'${regex}'/ && !($2 in a){a[$2]++; print $2}' } find_systemctl_pids(){ ps -elf | grep 'systemctl' | grep -v grep | awk '{print $13}' | sort -u | while read term do if [ -z "${shell_pid:-}" ] then shell_pid=$(find_pid_user_of /dev/$term '.*sh$') fi if [ -z "${systemctl_pid:-}" ] then systemctl_pid=$(find_pid_user_of /dev/$term 'systemctl') fi echo ${shell_pid} ${systemctl_pid} done } process_and_parent=`find_systemctl_pids` if has_parent_process ${process_and_parent} then shell_process=$(echo ${process_and_parent} | awk '{print $1}') parent_term=`readlink /proc/${shell_process}/fd/2` fi # And here, if the magic is done if [ -c "${parent_term}" ] then # Redirects error output # to the shell's terminal exec 2>${parent_term} fi

进程及其相关的TTY。然后,它寻找一个具有壳父进程的
systemctl

(希望不会有几个人同时运行

systemctl命令,这在我的用例中是可以接受的)。在这种情况下,它检索了链接到壳过程的端子。 如何打印


0
投票
systemctl

命令对我来说不是很好。因此,要打印我的消息,我添加了使用

systemctl

echo

printf

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.