[英]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.