Run python as a daemon process
2016-05-27 01:12
766 查看
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? shell ssh terminal background-process processes
| ||||||||
|
5 Answers 5
active oldest votesup 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. Disassociate the backgrounded program from your shell. Use the disowncommand after backgrounding your process. This will make the shell forget about it. 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. Use logoutinstead 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).
| ||||||||||||||||
|
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>&1before the &to redirect both the outputs to /dev/null- adding disownalso 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
| |||
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 &
| |||
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.
| ||
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" |
相关文章推荐
- 用PYTHON批量將PDF保存為圖片
- python装饰器前世今生
- python统计文件行数效率对比
- 『Python』 爬取 WooYun 论坛所有漏洞条目的相关信息
- python-文件操作
- Python为什么没有switch/case语句?
- Python程序中的观察者模式结构编写示例
- 详解Python编程中对Monkey Patch猴子补丁开发方式的运用
- python3.2.5 + PyMySQL3 0.5 基本使用方法
- python类中super()和__init__()的区别
- 1、python文件读写练习
- python学习之路-3 初始python数据类型以及文件操作
- python学习之路-2 初识python数据类型
- python学习之路-1 python基础操作
- python学习之路-1 python简介及安装方法
- leetcode(11),Power of Three(python)
- python二进制解析strut.upack的错误unpack requires a string argument of length 4
- Python脚本中写日志的问题
- python学习3
- 关于Python的主(main)函数问题--转