Bash - 将 stdout 和 stderr 重定向到具有后台进程的文件

mic*_*zer 3 python linux bash init

我为我正在构建的应用程序创建了一个简单的初始化脚本。脚本的开始部分如下所示:

user="ec2-user"

name=`basename $0`
pid_file="/var/run/python_worker.pid"
stdout_log="/var/log/worker/worker.log"
stderr_log="/var/log/worker/worker.err"

get_pid() {
    cat "$pid_file"
}

is_running() {
    [ -f "$pid_file" ] && ps `get_pid` > /dev/null 2>&1
}

case "$1" in
    start)
    if is_running; then
        echo "Already started"
    else
        echo "Starting $name"
        cd /var/lib/worker
        . venv/bin/activate
        . /etc/profile.d/worker.sh
        python run.py  >> "$stdout_log" 2>> "$stderr_log" &
        echo $! > "$pid_file" 
        if ! is_running; then
            echo "Unable to start, see $stdout_log and $stderr_log"
            exit 1
        fi
        echo "$name running"
    fi
Run Code Online (Sandbox Code Playgroud)

我在这条线上遇到了问题:

python run.py  >> "$stdout_log" 2>> "$stderr_log" &
Run Code Online (Sandbox Code Playgroud)

我想使用此代码启动我的应用程序并将输出重定向到上面指定的文件。但是,当我包含&以使其在后台运行时,两个日志文件中没有任何内容。但是,当我&从这一行中删除 时,日志文件会获取数据。为什么会这样?

显然,我需要运行命令使其作为后台进程运行,以停止 shell 等待。

我也确信当我使用 & 时进程正在运行。我可以找到它ps -aux

root     11357  7.0  3.1 474832 31828 pts/1    Sl   21:22   0:00 python run.py
Run Code Online (Sandbox Code Playgroud)

有谁知道我做错了什么?:)

Vor*_*Vor 5

有谁知道我做错了什么?:)

简答:

是的。添加-u到 python 命令,它应该可以工作。

python -u run.py  >> "$stdout_log" 2>> "$stderr_log" &
Run Code Online (Sandbox Code Playgroud)

长答案:

这是一个缓冲问题(来自man python):

   -u     Force stdin, stdout and stderr to be totally unbuffered.  On systems where it matters, also put stdin, stdout
          and  stderr  in  binary  mode.   Note that there is internal buffering in xreadlines(), readlines() and file-
          object iterators ("for line in sys.stdin") which is not influenced by this option.  To work around this,  you
          will want to use "sys.stdin.readline()" inside a "while 1:" loop.
Run Code Online (Sandbox Code Playgroud)