小技巧:让linux程序在后台运行
有些时候,我们需要在终端启动一个程序,并使之运行——但是如果关闭终端,那么这个程序也就随着关闭了。那么有没有什么方法在关闭终端后,让已经从这个终端启动的程序继续运行呢?
前置知识:
xterm,console,tty,pts,pty的区别
- shell是直接和内核进行通信的东西
- xterm是一个软件概念,可以通过这个程序连接到console从而控制主机,可以理解为cli形式的终端模拟器,而gnome-terminal,konsole就是gui形式的终端模拟器
- console是主机的控制台,是一个物理概念。
- tty、pty、pts都是终端,是硬件或者设备概念。
- tty是所有终端设备的总称
- pty是其中一类,伪终端,或者叫虚拟终端
在Unix/Linux下如果想让程序独立终端运行,一般都是使用 & 在命令结尾来让程序自动运行。(命令后可以不追加空格)
打开gnome-terminal,执行如下命令:
delectate@delectate:~$ totem &[1] 8510delectate@delectate:~$
delectate@delectate:~$ ps -e | grep totem? ? //程序已被以totem & 形式启动,当前附在pts0上8819 pts/0??? 00:00:00 totemdelectate@delectate:~$ ps -e | grep totem? ?//pts0的模拟终端被exit命令关闭,totem自动附在tty8819 ???????? 00:00:00 totemdelectate@delectate:~$
delectate@delectate:~$ vlc &[1] 8850delectate@delectate:~$ VLC media player 1.0.6 Goldeneye[0x8b998b0] main libvlc: Running vlc with the default interface. Use 'cvlc' to use vlc without interface.? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//enter presseddelectate@delectate:~$ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //show a clean terminal now** (:8850): CRITICAL **: giop_thread_request_push: assertion `tdata != NULL' failed? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? //仍然在输出数据……? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//关闭程序[1]+ Done vlcdelectate@delectate:~$
nohup描述:Run COMMAND, ignoring hangup signals.(忽略任何中断/挂起信号,使命令继续执行)
但是当你尝试使用命令:
nohup aria2c -i downloadlist -m 0 -j 1 &从jsharer极享下载了不少动漫。
http://topic.csdn.net/u/20100201/17/a34370cc-8a61-4315-a4d0-84242362064d.html
http://www.linuxsir.org/bbs/thread362001.html
http://www.williamlong.info/archives/482.html
http://dev.firnow.com/course/6_system/linux/Linuxjs/2008716/133186.html
http://www.cnblogs.com/hnrainll/archive/2011/07/04/2097408.html
转自:https://www.deleak.com/blog/2010/05/19/run-software-on-background/