小编Her*_*ezy的帖子

从控制台输出中获取最后n行

我想制作一个shell脚本,它将有效地从输出到控制台的sterr和stin中获取最后n行.我有一个屏幕会话运行一个进程,如果它通过hacky无限循环崩溃将重新启动它:

#!/bin/bash
#This script will be started in a screen session
counter=0
while [ $counter -lt 10 ]; do
    ./run_some_process;
     last_output=#GRAB PREVIOUS OUTPUT FROM CONSOLE HERE AND LOG TO FILE
     echo -e "$last_output" >> mylog.txt;
    sleep 5; #sleep for a few seconds before restarting
done
Run Code Online (Sandbox Code Playgroud)

我需要的是第7行代码从stderr和stdin中获取最后10行左右并将它们附加到日志文件中

bash shell logging gnu-screen

21
推荐指数
1
解决办法
3万
查看次数

python - 内存没有被回馈给内核

我有一个非常简单的脚本,它分配内存,dels唯一引用一个相当大的对象,一直打印heapypidstat报告.在运行脚本之后,heapy告诉我,当pidstat告诉我相反时,不应该使用太多内存:

from guppy import hpy
import time
import sys
import os

'''
1) print heapy and pidstat report after starting and before actually doing any work
2) allocate some memory in a simple 2d array
3) print heapy and pidstat report
4) del the d2 array (attempt at garbage collection)
5) print heapy and pidstat report
6) sleep so pidstat can continue to be run to check on memory
'''

def pidstat(msg):
    print '===============================' …
Run Code Online (Sandbox Code Playgroud)

python memory-leaks memory-management python-2.7

6
推荐指数
1
解决办法
2177
查看次数

python SimpleHTTPRequestHandler服务器退出后使套接字处于TIME_WAIT状态

我编写了一个简单的服务器来扩展SimpleHTTPRequestHandler

如果我在没有向服务器发出任何请求的情况下启动和停止它,我可以开始备份同一个端口,没有任何问题.

启动时,netstat看起来像这样:

sam@hersheezy:server$ sudo netstat -na --program | grep 8001
tcp        0      0 0.0.0.0:8001            0.0.0.0:*               LISTEN      23392/python

发出请求后,netstat看起来像这样(即使请求完成后):

sam@hersheezy:server$ sudo netstat -na --program | grep 8001
tcp        0      0 0.0.0.0:8001            0.0.0.0:*               LISTEN      23392/python    
tcp        0      0 127.0.0.1:8001          127.0.0.1:48659         TIME_WAIT   -

然后,我使用Cc杀死服务器,netstat看起来像这样(此时我无法重启服务器,因为端口已经在使用):

 sudo netstat -na --program | grep 8001
tcp        0      0 127.0.0.1:8001          127.0.0.1:48674         TIME_WAIT   - 

我显然没有正确地关闭一些东西.我发送回复的代码如下所示:

"""
reply is an object that can be json encoded that is written with a response code 200
"""
def send_provider_reply(self, replyobj): …
Run Code Online (Sandbox Code Playgroud)

python http time-wait python-2.7

5
推荐指数
1
解决办法
2007
查看次数

64位ubuntu上的adb库依赖项出错

当试图在Ubuntu 12.1机器上创建我的测试项目时:

adb install bin/opine_test-debug.apk
adb: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
Run Code Online (Sandbox Code Playgroud)

所以我去安装lib64ncurses(我的确运行64位操作系统):

sudo apt-get install lib64ncurses5
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information …
Run Code Online (Sandbox Code Playgroud)

ubuntu android adb

3
推荐指数
1
解决办法
2148
查看次数