I am using `&`: why isn't the process running in the background?

 
 
No problem. We won't show you that ad again. Why didn't you like it?

  • Uninteresting
  • Misleading
  • Offensive
  • Repetitive
  • Other

Oops! I didn't mean to do this.

         up vote9down votefavorite

6

I know that I can append & to a command to run the process in the background.

I'm SSH'ing into an Ubuntu 12.04 box and running a python program with $python program.py & -- but when I go to close the terminal window I get a message saying that closing the terminal will kill the running process.

Why is this? I am using the ampersand to run the process in the background. How can I get it to run regardless of if I am SSH'ed in?

Gilles

342k586121049        
        asked Jun 6 '14 at 21:30    
bernie2436

1,20252435        
 
                                                                                                                    
Duplicate of unix.stackexchange.com/q/4004/69080                     – Joshua Huber                 Jun 6 '14 at 23:50                                                                            
                                                                                                                    
apt-get install screen, man screen                     – captcha                 Mar 23 at 1:56                                                                                                 
add a comment |                      

5 Answers                                 5

active         oldest         votes
         up vote25down voteaccepted

When you close a terminal window, the terminal emulator sends a SIGHUP to the process it is running, your shell. Your shell then forwards that SIGHUP to everything it's running. On your local system, this is the ssh. The ssh then forwards the SIGHUP to what it's running, the remote shell. So your remote shell then sends a SIGHUP to all its processes, your backgrounded program.

There are 2 ways around this.

  1. Disassociate the backgrounded program from your shell.
    1. Use the disown command after backgrounding your process. This will make the shell forget about it.
    2. Prefix your command with nohup (nohup $python program.py &). This accomplishes the same thing, but by using an intermediate process. Basically it ignores the SIGHUP signal, and then forks & executes your program which inherits the setting, and then exits. Because it forked, the program being launched is not a child of the shell, and the shell doesn't know about it. And unless it installs a signal handler for SIGHUP, it keeps the ignore action anyway.
  2. Use logout instead of closing the terminal window. When you use logout, this isn't a SIGHUP, and so the shell won't send a SIGHUP to any of its children.

Additionally you must make sure that your program doesn't write to the terminal through STDOUT or STDERR, as both of those will no longer exist once the terminal exits. If you don't redirect them to something like /dev/null, the program will still run, but if it tries to write to them, it'll get a SIGPIPE, and the default action of SIGPIPE is to kill the process).

        answered Jun 6 '14 at 21:58    
Patrick

31k674124        
 
4                                                                                  
Technically, ssh dying causes the connection to drop, the connection to drop causes sshd on the other end to die. That sshd controlling the master side of the pseudo-terminal that the remote shell runs, when it dies, that's a hang-up (it's like pulling the plug on a real terminal), so the system sends a SIGHUP to the remote shell.                     – Stéphane Chazelas                 Jun 6 '14 at 22:04                                                                            
                                                                                                                    
@StéphaneChazelas That's one thing I've never dug into. If it's the kernel doing it, how does it determine which process to SIGHUP? It obviously doesn't SIGHUP everything with an open file descriptor to that TTY, as programs will remain running as long as they don't try to use it. So does it pick the program that is currently reading from STDIN (since only one can read from STDIN at a time)?                     – Patrick                 Jun 6 '14 at 22:14                                                                                                 
4                                                                                  
Never mind, answered my own question. POSIX IEEE 1003.1 chap 11, If a modem disconnect is detected by the terminal interface for a controlling terminal ... the SIGHUP signal shall be sent to the controlling process.                     – Patrick                 Jun 6 '14 at 22:24                                                                                                 
1                                                                                  
Also note that nohup generates a nohup.out file with output of the program you start with it. May be annoying to delete that file every time you've used nohup to start an app this way (or you'd need to redirect its output to /dev/null).                     – Ruslan                 Jun 7 '14 at 14:42                                                                            
add a comment |                      
 
 
No problem. We won't show you that ad again. Why didn't you like it?

  • Uninteresting
  • Misleading
  • Offensive
  • Repetitive
  • Other

Oops! I didn't mean to do this.

         up vote3down vote

The process is running in the background in the terminal, but the output from stdout (and stderr) is still being sent to the terminal. To stop this, add > /dev/null 2>&1 before the & to redirect both the outputs to /dev/null - adding disown also makes sure the process is not killed after you close the terminal:

COMMAND > /dev/null 2>&1 & disown

In your case this would be:

python program.py > /dev/null 2>&1 & disown
        answered Jun 6 '14 at 21:49    
Wilf

1,319827        
 
   
add a comment |                      
         up vote1down vote

When you logout, background processes associated with the login session are normally killed as well. If you want them to be disconnected from the session, run them with nohup.

nohup python program.py &
Patrick

31k674124        
        answered Jun 6 '14 at 21:37    
Barmar

3,791514        
 
   
add a comment |                      
         up vote0down vote

The & operator separates commands to run in parallel, just as ; separates commands to run in series. Both kinds of commands will still run as a child of the shell process.

So, when you close the shell which started those children, the children will be closed also.

What you appear to want is a daemon process, which is significantly more tricky because it needs to dissociate entirely from the parent process. The shell usually doesn't have a simple way of doing that.

        answered Jun 7 '14 at 3:28    
bignose

1586        
 
   
add a comment |                      
         up vote0down vote

After the command ending with ampersand (&), at the command prompt run the command "bg"

bash> python program.py & bash> bg This will put the "&" command to the background bash> jobs This will list the jobs running in the background

bash> fg 1 This will bring Job #1 to the foreground

Another way, (to be able to log out)

bash> at now bash> /full/path/python /full/path/program.py bash> ^d   (# That is, Control-D, to run the command, Control-C to cancel) Multiple lines can be submitted to the "at" command, before the ^d (Control-D) see "man at"

Run python as a daemon process的更多相关文章

  1. A daemon process class in python

    In everbright task schedule project, we need some daemon process to do certain work, here is a examp ...

  2. python中的daemon守护进程实现方法

    原文参考:http://blog.csdn.net/tao_627/article/details/49532021 守护进程是生存期长的一种进程.它们独立于控制终端并且周期性的执行某种任务或等待处理 ...

  3. 越狱开发-创建真正的后台程序(Daemon Process)

    在网上搜索了一下如何在IOS上面实现Daemon Process,只有chrisalvares的博客中有过详细的描述,但是其博客中描述的较为复杂, 参考stackoverflow中的一个问答: htt ...

  4. ionic android - Unable to start the daemon process. Could not reserve enough space for 2097152KB object heap

    Unzipping C:\Users\app\.gradle\wrapper\dists\gradle-4.1-all\bzyivzo6n839fup2jbap0tjew\gradle-4.1-all ...

  5. Android Studio: Failed to sync Gradle project 'xxx' Error:Unable to start the daemon process: could not reserve enough space for object heap.

    创建项目的时候报错: Failed to sync Gradle project 'xxx' Error:Unable to start the daemon process: could not r ...

  6. Android Studio新建了一个项目提示Error:Unable to start the daemon process

    提示如下错误:

  7. python 守护进程 daemon

    python 守护进程 daemon # -*-coding:utf-8-*- import sys, os '''将当前进程fork为一个守护进程 注意:如果你的守护进程是由inetd启动的,不要这 ...

  8. 【error】Gradle sync failed: Unable to start the daemon process.【已解决】

    ---恢复内容开始--- 在克隆GIT项目后,Android Studio 报错: Gradle sync failed: Unable to start the daemon process. Th ...

  9. How to run Python code from Sublime

    How to run Python Code from Sublime,and How to run Python Code with input from sublime Using Sublime ...

随机推荐

  1. mvc 方法只允许ajax访问

    有时候我们写一些方法 只想在ajax使用  其他的不想暴露  就可以对方法进行限制 如下: [AttributeUsage(AttributeTargets.Method)] public class ...

  2. Tomcat抛出异常:ClientAbortException: java.net.SocketException: Connection

    在做一个小网站的时候,写了一个通过servlet实现文件下载功能的页面.当我点击超级练级,弹出下载对话框,点击保存正常下载,不会出现任何问题,当我我点击取消,服务器端就出现如下提示: ClientAb ...

  3. C#判断一个string是否为数字

    案一:Try...Catch(执行效率不高) private bool IsNumberic(string oText) { try { int var1=Convert.ToInt32 (oText ...

  4. Unity3D常用代码总结

    1 GUI汇总 function OnGUI() { GUI.Label(Rect(1,1,100,20),"I'm a Label"); //1 GUI.Box(Rect(1,2 ...

  5. Python中的模块与包

    标准库的安装路径 在import模块的时候,python是通过系统路径找到这些模块的,我们可以将这些路径打印出来: >>> pprint.pprint(sys.path) ['', ...

  6. SQL调优 - Hints指定索引 解决慢查询案例

    背景 每当交易高峰时期,可能会暴露一些平时无法发现的问题,机遇和挑战并存.下面聊聊最近解决的一个案例,因为执行计划走错导致慢查询,进而引发应用线程阻塞.线程池爆满,最后应用功能瘫痪.如何标本兼治的解决 ...

  7. shell 复习

    grep -v zip$  -v 逻辑否  $以zip结尾 (^开头) -n str不空,-z str 空

  8. C#读取物理网卡信息及其对应的IP地址

    using Microsoft.Win32; using System; using System.Collections; using System.Collections.Generic; usi ...

  9. shell 常用命令

    Terminal是Mac OS X系统中的字符控制界面,可以更灵活地控制苹果电脑以下看到 “>“ 就是打指令的地方,prompt,指令列>pwd列出路径>ls列出此档案夹里所有的东西 ...

  10. 用CentOS 7打造合适的科研环境 :zhuan

    这篇博文记录了我用CentOS 7搭建地震学科研环境的过程,供我个人在未来重装系统时参考.对于其他地震学科研人员,也许有借鉴意义. 阅读须知: 本文适用于个人电脑,不适用于服务器: 不推荐刚接触Lin ...