2016-11-01 6 views
-1

こんにちは私は同じPiのOSMCとWebサーバーを使ってRaspberry Piを実行しています。ファイルへのCrontabとpythonスクリプトの書き込み

IveはCPU temp、使用量、メモリなどを取得するPythonスクリプトを作成しました スクリプトを実行すると、sudo python state.pyが動作し、値が取得されてtxtファイルに書き込まれます。

私はそう私はcrontabファイルを作ったスクリプトが起動時に実行したい:

@reboot sudo python /home/osmc/python/state.py & 

これは動作しますが、しかし、それは唯一のメモリとディスク統計、ファイルへのCPUの統計を書くdosent。

私のPythonスクリプトは次のようになります。

#!/usr/bin/python 
import os 
import sys 
import commands 
import time 

# Return CPU temperature as a character string          
def getCPUtemperature(): 
    res = os.popen('vcgencmd measure_temp').readline() 
    return(res.replace("temp=","").replace("'C\n","")) 

# Return RAM information (unit=kb) in a list           
# Index 0: total RAM                 
# Index 1: used RAM                 
# Index 2: free RAM                 
def getRAMinfo(): 
    p = os.popen('free') 
    i = 0 
    while 1: 
     i = i + 1 
     line = p.readline() 
     if i==2: 
      return(line.split()[1:4]) 

# Return % of CPU used by user as a character string         
def getCPUuse(): 
    return(str(os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip(\ 
))) 

# Return information about disk space as a list (unit included)      
# Index 0: total disk space               
# Index 1: used disk space               
# Index 2: remaining disk space              
# Index 3: percentage of disk used             
def getDiskSpace(): 
    p = os.popen("df -h /") 
    i = 0 
    while 1: 
     i = i +1 
     line = p.readline() 
     if i==2: 
      return(line.split()[1:5]) 
while True: 
     # CPU informatiom 
     CPU_temp = getCPUtemperature() 
     CPU_usage = getCPUuse() 

     # RAM information 
     # Output is in kb, here I convert it in Mb for readability 
     RAM_stats = getRAMinfo() 
     RAM_total = round(int(RAM_stats[0])/1000,1) 
     RAM_used = round(int(RAM_stats[1])/1000,1) 
     RAM_free = round(int(RAM_stats[2])/1000,1) 

     # Disk information 
     DISK_stats = getDiskSpace() 
     DISK_total = DISK_stats[0] 
     DISK_free = DISK_stats[1] 
     DISK_perc = DISK_stats[3] 

     starttime = time.time() 

     file = open("/var/www/html/rspi_state.txt", "w") 


     file.write(CPU_temp + "\n") 
     file.write(CPU_usage + "\n") 
     file.write(DISK_stats[1] + "\n") 
     file.write(DISK_stats[0] + "\n") 
     file.write(DISK_stats[3] + "\n") 
     file.write(str(RAM_total) + "\n") 
     file.write(str(RAM_free) + "\n") 
     file.close() 
     print "CPU TEMP: " + CPU_temp 
     time.sleep(9.0 - ((time.time() - starttime) % 9.0)) 

なぜそれが唯一のテキストファイルへのデータの一部を書きいかなる理由がありますか? ご迷惑をおかけして申し訳ありません。

+1

'OS、popen' thows離れ' strerr'、あなたは、コールが失敗した場合に対処していないので、コードを返します。 'subprocess'の呼び出しのうちの1つを使用して、失敗した場合に例外を発生させるか、stderrを読み込んでリターンコードを自分自身でチェックします。 – tdelaney

+0

コメントありがとうございます。私はsshからスクリプトを実行するだけで動作します。 cronを使用しているためにエラーが発生しましたか? –

+1

'cron'の中で' sudo'しません。 'sudo crontab -e'を使って' root'のcrontabにスクリプトを追加するだけです。また、あなたは既にスクリプトにインタプリタを持っているので、 '@reboot /home/osmc/python/state.py 'をルートのcrontabに追加するとうまくいくはずです。 – Munir

答えて

0

問題に焦点を当てたスクリプトを作成することで、問題を診断できます。これは、システムコールから戻ってくるすべての情報を記録します。私はmy_popenのエラーをチェックする修正されたバージョン...またはsubprocessのエラーチェックの呼び出しの1つが良い考えだと思います。私は自分自身で多くのシステムコールを行い、Popenの複雑なラッパーを持っています。ログにはあらゆる種類の魅力的なものがあります。あなたの通常の一人として、このスクリプトを同じ方法で実行し、何が起こるかを参照してください。

import subprocess as subp 

def my_popen(cmd, file): 
    file.write('cmd: {}\n'.format(cmd)) 
    proc = subp.Popen(cmd, shell=True, stdout=subp.PIPE, stderr=subp.PIPE) 
    out, err = proc.communicate() 
    print('return code: {}\n'.format(proc.returncode)) 
    print('out: {}\n'.format(out)) 
    print('err: {}\n'.format(err)) 
    print('------\n') 
    return out 

with file = open("/var/www/html/test_state.txt", "w") as file: 
    my_popen('vcgencmd measure_temp', file) 
    my_popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'", file) 
関連する問題