nohup
命令可以让进程忽略挂起(SIGHUP)信号,从而在关闭终端或注销时继续运行
使用 nohup
命令启动进程:
nohup your_command &
这将在后台运行 your_command
,并将输出重定向到名为 nohup.out
的文件。
使用 disown
命令:
如果进程已经在运行,你可以使用 disown
命令将其从当前 shell 的作业列表中移除,从而防止接收到 SIGHUP 信号:
disown %job_number
其中 job_number
是作业编号,可以通过 jobs
命令查看。
使用 screen
或 tmux
:
screen
和 tmux
是终端复用器,可以让你在一个终端窗口中运行多个会话。当你使用它们运行进程时,即使关闭终端或注销,进程也会继续运行。
例如,使用 screen
:
screen -S your_session_name
your_command
然后按 Ctrl-A
再按 D
键将 screen
会话分离。要重新连接到会话,请使用:
screen -r your_session_name
对于 tmux
,过程类似:
tmux new-session -s your_session_name
your_command
然后按 Ctrl-B
再按 D
键将 tmux
会话分离。要重新连接到会话,请使用:
tmux attach-session -t your_session_name
通过这些方法,你可以确保在使用 nohup
命令时,进程不会被系统杀死。