如何将当前正在运行的 linux 进程置于后台? [英] How can I put the current running linux process in background?

查看:48
本文介绍了如何将当前正在运行的 linux 进程置于后台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a command that uploads files using git to a remote server from the Linux shell and it will take many hours to finish.

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

How can I put that running program in background? So that I can still work on shell and that process also gets completed?

推荐答案

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

Suspend the process with CTRL+Z then use the command bg to resume it in background. For example:

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 用法的更多信息:

More about job control and bg usage in bash manual page:

工作控制
在进程运行时键入 suspend 字符(通常是 ^Z、Control-Z)会导致该进程停止并将控制权返回给 bash.[...] 然后,用户可以操纵此作业的状态,使用 bg 命令在后台继续它,[...].^Z 立即生效,并具有导致挂起的输出和预输入被丢弃的额外副作用.

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 ...]
在后台恢复每个暂停的作业 jobspec,就好像它是用 & 启动的一样.如果 jobspec 不存在,则使用 shell 的 当前作业 概念.

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.

编辑

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

To start a process where you can even kill the terminal and it still carries on running

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

例如

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

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

To just ignore the output (not very wise) change the filename to /dev/null

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

To get the error message set to a different file change the &1 to a filename.

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

In addition: You can use the jobs command to see an indexed list of those backgrounded processes. And you can kill a backgrounded process by running kill %1 or kill %2 with the number being the index of the process.

这篇关于如何将当前正在运行的 linux 进程置于后台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆