linux - 如何将当前正在运行的 linux 进程置于后台?

我有一个命令,它使用 git 从 Linux shell 将文件上传到远程服务器,这需要几个小时才能完成。

如何将正在运行的程序置于后台?这样我仍然可以在 shell 上工作并且该过程也完成了吗?

最佳答案

使用 CTRL+Z 暂停进程,然后使用命令 bg 在后台恢复它。例如:

sleep 60
^Z  #Suspend character shown after hitting CTRL+Z
[1]+  Stopped  sleep 60  #Message showing stopped process info
bg  #Resume current job (last job stopped)

bash 手册页中有关作业控制和 bg 用法的更多信息:

JOB CONTROL
Typing the suspend character (typically ^Z, Control-Z) while a process is running causes that process to be stopped and returns control to bash. [...] The user may then manipulate the state of this job, using the bg command to continue it in the background, [...]. A ^Z takes effect immediately, and has the additional side effect of causing pending output and typeahead to be discarded.

bg [jobspec ...]
Resume each suspended job jobspec in the background, as if it had been started with &. If jobspec is not present, the shell's notion of the current job is used.

编辑

启动一个进程,你甚至可以杀死终端,它仍然继续运行

nohup [command] [-args] > [filename] 2>&1 &

例如

nohup /home/edheal/myprog -arg1 -arg2 > /home/edheal/output.txt 2>&1 &

忽略输出(不是很明智)将文件名更改为 /dev/null

要将错误消息设置为不同的文件,请将 &1 更改为文件名。

此外:您可以使用 jobs 命令查看这些后台进程的索引列表。您可以通过运行 kill %1kill %2 来终止后台进程,其中数字是进程的索引。

https://stackoverflow.com/questions/13676457/

相关文章:

python - 将列表列表获取到 pandas DataFrame

python - 子进程命令的实时输出

windows - 为什么在 Windows 上创建新进程比在 Linux 上更昂贵?

c - 将输出重定向到文件时 printf() 和 system() 的结果顺序错误

python - 如何在 Python 中创建 PDF 文件

linux - 64 位 Linux 机器上的 Android SDK

python - Django 动态模型字段

linux - Bash 脚本在空行上打印 "Command Not Found"

linux - 将 awk 与列值条件一起使用

python - Windows 上的错误 "Import Error: No module nam