运行一个外壳脚本并立即背景它,但是保持检查其输出的能力

发布于 2025-01-24 15:47:15 字数 209 浏览 0 评论 0 原文

我如何运行一个shell脚本并立即 background ,但是可以通过尾部/tmp/output.txt 来保持任何时间检查其输出的能力。

如果我可以过时的过程,那就太好了。


ps,

如果您还可以向我展示如何将背景过程“发送”到可能初始化或可能未被初始化的GNU屏幕中“发送”背景过程,那将是很酷的。

How can I run a shell script and immediately background it, however keep the ability to inspect its output any time by tailing /tmp/output.txt.

It would be nice if I can foreground the process too later.


P.S.

It would be really cool if you can also show me how to "send" the backgrounded process in to a GNU screen that may or may not have been initialized.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜光 2025-01-31 15:47:15

在启动过程中,“背景”一个过程

只需在命令之后添加ampersand(& )即可。

如果该程序编写标准化,它仍将写入您的控制台 /终端。


为了预言过程,

只需使用 fg 命令。您可以在背景中看到 jobs

例如:

sh -c'Sleep 3&&回声我刚醒了'&作业


如果您已经在 foreground 中启动了该过程

,则可以将其移至 background ,您可以执行以下操作:

  1. ctrl + z 将当前过程入睡并返回您的外壳。此过程将被暂停,直到您发送另一个信号为止。
  2. 运行 bg 该过程,但要在背景而不是前景中运行。

To 'background' a process when you start it

Simply add an ampersand (&) after the command.

If the program writes to standard out, it will still write to your console / terminal.


To foreground the process

Simply use the fg command. You can see a list of jobs in the background with jobs.

For example:

sh -c 'sleep 3 && echo I just woke up' & jobs


To background a currently running process

If you have already started the process in the foreground, but you want to move it to the background, you can do the following:

  1. Press Ctrl+z to put the current process to sleep and return to your shell. This process will be paused until you send it another signal.
  2. Run the bg command to resume the process, but have it run in the background instead of the foreground.
鹿童谣 2025-01-31 15:47:15

另一种方法是将 nohup 命令与& 在行结尾处使用。

这样的东西

nohup whatevercommandyouwant whateverparameters &

将在后台运行并将其输出发送到nohup.log文件。

Another way is using the nohup command with & at the end of the line.

Something like this

nohup whatevercommandyouwant whateverparameters &

This will run it in the background and send its output to a nohup.log file.

情栀口红 2025-01-31 15:47:15

一种易于使用的方法,它允许管理多个流程,并且具有一个不错的终端UI是不幸的实用程序。

使用 PIP安装不可能(或 python3 -m pip install dobs nabless ),只需

$ hap run my-command  # e.g. hap run python my_long_running_script.py
$ hap status  # check all the launched processes
$ hap logs 4  # output logs for you 4th background process
$ hap logs -f 2  # continuously stream logs for the 2nd process

参见 docs 有关更多信息。

One easy to use approach that allows managing multiple processes and has a nice terminal UI is hapless utility.

Install with pip install hapless (or python3 -m pip install hapless) and just run

$ hap run my-command  # e.g. hap run python my_long_running_script.py
$ hap status  # check all the launched processes
$ hap logs 4  # output logs for you 4th background process
$ hap logs -f 2  # continuously stream logs for the 2nd process

See docs for more info.

ui

冷弦 2025-01-31 15:47:15

我尝试了 nohup ,但是一两天后我的过程被停止了,所以我尝试了这个简单的bash脚本来重新运行我的

命令

while true; do
[ -e stopme ] && break
python3 main.py # your command here
done

nohup bash run.sh &

I tried nohup but my process was getting stopped after a day or two , so i tried this simple bash script to rerun my command after it stops

run.sh

while true; do
[ -e stopme ] && break
python3 main.py # your command here
done

then do

nohup bash run.sh &
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文