I have got the below script, this works fine when i run this from command line however when i put the same script to execute through cron it throws me the error.
My code
import subprocess
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from datetime import date as dt
sites = ('dc1', 'dc2', 'dc3')
ping_cmd = ['fping', '-C', '1']
for site in sites:
# get_info is environment specific script which pulls certain details for the servers.
cmd1 = ['get_info', '-c', '-H', "install%", '-s', site]
server_list = subprocess.Popen(cmd1, stdout=subprocess.PIPE).communicate()[0].strip().split(',')
not_alive = ''
if len(server_list):
for i in server_list:
server_status = subprocess.Popen(['fping', '-C', '1', '-q', i], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = server_status.communicate()
if server_status.returncode != 0:
not_alive += i + '\n'
current_time = dt.today().strftime("%F")
from_add = 'noreply@test.com'
to_add = 'user@test.com'
msg = MIMEMultipart()
msg['From'] = from_add
msg['To'] = to_add
msg['Cc'] = cc_add
msg['Subject'] = 'Unreachable Install Boxes on ' + current_time
# Attach the email body to MIMEMultipart object
msg.attach(MIMEText(not_alive, 'plain'))
body = msg.as_string()
server = smtplib.SMTP('localhost')
server.sendmail(from_add, [to_add, cc_add], body)
server.quit()
Above works just fine when run like this /usr/bin/python2 pingtest.py
However when i schedule the same script like this 08 * * * * /usr/bin/python2 /apps/rbhanot/pingtest.py
in cron i get the below error
Traceback (most recent call last):
File "/apps/nttech/rbhanot/Documents/PythonPrograms/tower-scripts/pingtest.py", line 28, in <module>
server_list = subprocess.Popen(cmd1, stdout=subprocess.PIPE).communicate()[0].strip()
File "/usr/lib/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1343, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Any ideas ?