我有這樣的python腳本來檢查,如MySQL和Apache服務,如果它們中的一些下來,然後再次開始:我的python腳本和cron作業有什麼問題?
:import subprocess import smtplib _process = ['mysql', 'apache2'] for _proc in _process: p = subprocess.Popen(["service", _proc, "status"], stdout=subprocess.PIPE) output, err = p.communicate() print "*** check service "+_proc+" ***\n" if 'Active: active (running)' in output: print _proc+" is working!" else: print "Ouchh!! "+_proc+" again is down." output = subprocess.Popen(["service", _proc, "start"]) print "*** The service "+_proc+" was restarted ***\n", output print "*** sending email ***\n" server = smtplib.SMTP('smtp.gmail.com', 587) server.ehlo() server.starttls() _from = "[email protected]" _to = "[email protected]" _cc = ["[email protected]"] _pass = "xxxxxx" server.login(_from, _pass) msg = "\r\n".join([ "From: "+_from, "To: "+_to, "CC: "+",".join(_cc), "Subject: Ouchh!! "+_proc+" again is down.", "", "Again " + _proc + " is down. The service was restarted!!" ]) server.sendmail(_from, _to, msg) server.quit() print "*** email send ***\n"
它當我執行下面的命令工作在我的控制檯罰款
python /home/check-services.py
一切都看起來沒問題。我嘗試在webmin服務器上設置cron作業,它應該每分鐘執行一次,但不會執行。這是cron的日誌:
(根)CMD(python /home/check-services.py)每一分鐘,就像我們已經配置好的一樣。
我沒有看到任何異常,但它仍然沒有工作。我感謝你的幫助。
Python版本2.7。
編輯: 我剛纔看到在/ var /郵件/根的電子郵件,它表明這一點:
Traceback (most recent call last):
File "/home/check-services.py", line 77, in <module>
p = subprocess.Popen(["service", _proc, "status"], stdout=subprocess.PIPE)
File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
在這個職位的語法着色只是去,看起來你已經有了''之後再加一個額外的引號。「' – Kevin
你試過'/ usr/bin/python/home/check-services.py'。 cron工作的大部分時間,你需要給絕對路徑。 –
你在腳本的第一行缺少#!/ usr/bin/python嗎?在crontab中,你是用python預修補程序還是僅調用/opt/script/myscript.py? – AK47