简体   繁体   English

如何在后台运行此命令行?

[英]How can I run this command line in the background?

I have this 我有这个

script -q -c "continuously_running_command" /dev/null > out

When I have the above command line running I can stop it by doing CTRL+C 当我运行上述命令行时,可以通过执行CTRL+C来停止它

However I'd like to run the above commandline in back ground so that I can stop it by doing kill -9 %1 但是我想在后台运行上述命令行,以便可以通过kill -9 %1来停止它

But when I try to run this 但是当我尝试运行这个

script -q -c "continuously_running_command" /dev/null > out &

I get 我懂了

[2]+  Stopped (tty output)       script -q -c "continuously_running_command" /dev/null 1>out

Question: 题:

How can I run the above commandline in back ground? 如何在后台运行以上命令行?

In order to background a process with redirection to a file, you must also redirect stderr. 为了使具有重定向到文件的进程进入后台,您还必须重定向stderr。 With stdout and stderr redirected, you can then background the process: 重定向了stdout和stderr之后,您就可以后台处理了:

script -q -c "continuously_running_command" /dev/null > out 2>&1 &

Fully working example: 完全正常的示例:

#!/bin/bash

i=$((0+0))

while test "$i" -lt 100; do

    ((i+=1))
    echo "$i"
    sleep 1

done

Running the script and tail of output file while backgrounded: 在后台运行脚本和输出文件的尾部:

alchemy:~/scr/tmp/stack> ./back.sh > outfile 2>&1 &
[1] 31779
alchemy:~/scr/tmp/stack> tailf outfile
10
11
12
13
14
...
100
[1]+  Done                    ./back.sh > outfile 2>&1

In the case of: 如果是:

 script -q -c "continuously_running_command" /dev/null

The problem in in this case is the fact that script itself causes redirection of all dialog with script to FILE, in this case to /dev/null . 在这种情况下,问题在于script本身会导致所有带有脚本的对话框重定向到FILE,在这种情况下,重定向到/dev/null So you need to simply issue the command without redirecting to /dev/null or just redirect stderr to out: 因此,您只需要发出命令而无需重定向到/ dev / null或仅将stderr重定向到out:

script -q -c "continuously_running_command" out 2>&1 &

or 要么

script -q -c "continuously_running_command" /dev/null/ 2>out &

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果前一行命令将运行服务器,我该如何继续执行脚本 - how can i go on with script if previous line command will run a server 如何从命令行中的脚本运行 function? - How can I run a function from a script in command line? 如何将命令行参数传递给应该在后台运行的脚本? - how to pass command line argument to a script which should be run in the background? 如何在后台运行进程并在同一命令行中更改目录 - How to run a process in background and change directory in the same command-line 如何在 bash 命令行上运行脚本,使其配置与 cron 运行时的配置相同? - how can I run a script at the bash command line such that its configuration is identical to its configuration when it is run by cron? 如何在Windows上借助cygwin从命令行运行Shell脚本? - How can I run a shell script from command line with the help of cygwin on Windows? 如何使用命令行参数从bash运行perl脚本? - How can I run a perl script from bash using command line arguments? 我怎么能告诉Java代码运行脚本,因为它是从普通用户的命令行运行的? - How can i tell Java code run the script as it was running from command line as normal user? JMeter 命令行: 我可以将 output 的运行日志输出到 STDOUT 吗? - JMeter command line: Can I output the run log to STDOUT? 如何在 bash 中在一行中运行多个后台命令? - How do I run multiple background commands in bash in a single line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM