nohup 执行 python 程序 , 以及 print 无法输出问题
2017-10-21 11:55
549 查看
这里的测试环境为 MacOS
一. python 文件
建立 test.py 文件,其代码如下 :
二. shell 脚本文件
建立 test.sh 文件,其代码如下 :
修改该 test.sh 文件使之具有执行权限 :
# chmod +x ./test.sh
三. 执行脚本启动 nohup ,以及 python 内部 print 无法输出问题
执行 shell 脚本 :
# ./test.sh
该目录下会多出一个文件 nohup.out , 此时目录下有三个文件 test.sh test.py nohup.out ,
但是发现 nohup.out 中显示不出来 python 程序中 print 的内容,
这是因为 python 输出有缓冲,使得 nohup.out 中不能马上有内容
此时,可以使用 python 的 -u 参数,使得 python 不启用缓冲,即 test.sh 脚本改为 :
再次执行该 shell 脚本,nohup.out 中就会有 python 中 print 输出的内容
四. 关闭 nohup 启动的 python 程序
查找 python 进程 :
# ps
显示如下 :
PID TTY
TIME CMD
2986 ttys001
0:00.47 -bash
8409 ttys001
0:00.02 python -u test.py
根据进程 ID , kill 掉该 python 进程
# kill -9 8409
一. python 文件
建立 test.py 文件,其代码如下 :
# coding:utf-8 import time while True: # 每隔 2 秒钟打印一次字符串 time.sleep(2) print('hello, python...')
二. shell 脚本文件
建立 test.sh 文件,其代码如下 :
#!/bin/bash echo "begin." nohup python test.py > nohup.out 2>&1 & echo "end."
修改该 test.sh 文件使之具有执行权限 :
# chmod +x ./test.sh
三. 执行脚本启动 nohup ,以及 python 内部 print 无法输出问题
执行 shell 脚本 :
# ./test.sh
该目录下会多出一个文件 nohup.out , 此时目录下有三个文件 test.sh test.py nohup.out ,
但是发现 nohup.out 中显示不出来 python 程序中 print 的内容,
这是因为 python 输出有缓冲,使得 nohup.out 中不能马上有内容
此时,可以使用 python 的 -u 参数,使得 python 不启用缓冲,即 test.sh 脚本改为 :
#!/bin/bash echo "begin." nohup python -u test.py > nohup.out 2>&1 & echo "end."
再次执行该 shell 脚本,nohup.out 中就会有 python 中 print 输出的内容
四. 关闭 nohup 启动的 python 程序
查找 python 进程 :
# ps
显示如下 :
PID TTY
TIME CMD
2986 ttys001
0:00.47 -bash
8409 ttys001
0:00.02 python -u test.py
根据进程 ID , kill 掉该 python 进程
# kill -9 8409
相关文章推荐
- 用nohup执行python程序时,print无法输出
- 用nohup执行python程序时,print无法输出
- 用nohup执行python程序时,print无法输出
- 用nohup执行python程序时,print无法输出
- 用nohup执行python程序时,print无法输出
- 用nohup执行python程序时,print无法输出
- 关于Python的第三方库requests关闭SSL验证后,依然警告致使程序无法执行问题
- Linux CRON定时执行JAR程序时,log4j日志无法输出到指定目录的问题
- python MySQLdb 问题 - 程序已的后台运行每90秒获取数据,却无法获取最新的值
- Python:通过执行100万次打印来比较C和python的性能,以及用C和python结合来解决性能问题的方法
- Python 2.7.3的文件编码问题,print在控制台下面中文乱码问题,以及推荐做法
- Python的print输出中文对齐问题
- Python:通过执行100万次打印来比较C和python的性能,以及用C和python结合来解决性能问题的方法
- 在C/python中执行linux命令并得到返回值以及输出
- CGI程序学习(2)-CGI执行Shell脚本后无法在浏览器正常显示问题
- Web程序中利用web.config解决无法输出excel页面的问题
- python程序中文输出问题怎么解决?
- document.write 方式引入外部 JS 文件导致脚本程序执行顺序不同以及 DOM 树更新延迟问题
- 在C/python中执行linux命令并得到返回值以及输出
- window.open()在IE8下报错,输出文件无法下载,以及打开多个窗口重复的问题