如何在服务器上连续运行应用程序

我正在使用Ubuntu 12.04服务器版本。 我有通过SSH访问服务器。

我的问题是我怎样才能通过SSH运行程序,即使我断开SSH连接也不会被closures。 它必须作为服务在服务器上运行。

你能告诉我怎么做?

nohup调用命令

nohup信息:

'nohup'运行给定的COMMAND时挂起信号被忽略,这样该命令可以在注销后继续在后台运行。 概要:

  nohup COMMAND [ARG]... 

作为我build议使用的服务运行程序

 start-stop-daemon --start --quiet --background --make-pidfile --pidfile $PIDFILE --exec $DAEMON --chuid $USER --chdir $DIR -- $DAEMON_ARGS 

看一下/etc/init.d/skeleton并根据这个例子编写init脚本。

新贵 ! 正如你使用Ubuntu 12.04 ..

你在/etc/init/

你的脚本包含指令,控制进程,如何运行,运行的地方,环境,如果失败,重新生成等。

 # my upstart script # optional stuff description "start and stop the THING" version "1.0" author "Tom" # configuration variables. # You'll want to change these as needed env A_THING_TO_SET=blaaah # tell upstart we're creating a daemon # upstart manages PID creation for you. expect fork # of course, if it doesn't fork, take out the above line script # My startup script, plain old shell scripting here. chdir /opt/thingy/bin exec /opt/thingy/bin/thingy ARGUMENTS # create a custom event in case we want to chain later emit thingy_running end script 

采取和改编: http : //geeknme.wordpress.com/2009/10/15/getting-started-with-upstart-in-ubuntu/

我也build议你阅读Upstart食谱

如果你不使用Ubuntu,那么其他选项包括主pipe(d) , 监控 , 守护 进程 ,所有这些都比在屏幕上运行的东西要less得多,而且没有。

nohup不会自动把它在后台运行的命令,你应该用一个&符号结束命令行。

nohup命令&

除了nohup的build议,考虑使用GNU屏幕 ,它允许你有多个terminal窗口打开的能力,恢复连接,如果你的SSH会话因任何原因死亡。

屏幕是有用的。

运行屏幕,然后运行你的命令,按ctrl + A ,然后d 。 您可以返回到您的程序。 运行screen -lsfind分离的屏幕,使用screen -r 20673.pts-0.srv连接到它。