想要改进这个问题? Update the question所以它是on-topic堆栈溢出。
关闭 9 年前。
我有一个命令,它使用 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 %1
或 kill %2
来终止后台进程,其中数字是进程的索引。
https://stackoverflow.com/questions/13676457/