bash 在控制台上显示 init.d 脚本回显

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35397638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:14:46  来源:igfitidea点击:

showing init.d script echo on console

bashecho

提问by E_P

I have a bash script within /etc/init.d in a kali2016.1 box that executes some actions. My issue is the following: when executing the script as ./script{start|stop|restart|}, the output of echo shows on the console. When I run it as service script{start|stop|restart|*}, however, it logs the echo output in /var/log/daemon.log instead. How can I change this behavior so that, when executing it as service, it continues to log but also shows the echo output on console? Covering the basics, it goes as follows:

我在执行某些操作的 kali2016.1 框中的 /etc/init.d 中有一个 bash 脚本。我的问题如下:当以./script{start|stop|restart|}执行脚本时,echo 的输出显示在控制台上。但是,当我以service script{start|stop|restart|*}运行它时,它会在 /var/log/daemon.log 中记录 echo 输出。如何更改此行为,以便在将其作为服务执行时,它会继续记录并在控制台上显示 echo 输出?涵盖基础知识,如下所示:

    #!/bin/bash
    case "" in
      start)
        echo "start"
      ;;
      stop)
        echo "stop"
      ;;
      restart)
        echo "restart"
      ;;
      *)
        echo "usage {start|stop|restart}"
        exit 1
      ;;
    esac
    exit 0

回答by dancancode

You can echo "foo" > /dev/console.

你可以echo "foo" > /dev/console

When stdoutis connected to your terminal you see the output. Based on your description, the servicecommand is redirecting/capturing stdoutand sending it to syslog.

stdout连接到您的终端时,您会看到输出。根据您的描述,该service命令正在重定向/捕获stdout并将其发送到系统日志。

Have a look at service- it's probably a shell script, so you can see what's happening.

看看service- 它可能是一个 shell 脚本,所以你可以看到发生了什么。

FWIW, on most distributions, including CentOS7 and AWS Linux, the servicescript does not change stdout.

FWIW,在大多数发行版上,包括 CentOS7 和 AWS Linux,service脚本不会改变stdout