1

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 ?

Rohit
  • 3,659
  • 3
  • 35
  • 57
  • Where is `get_info`? Make sure it's in a location included in the PATH variable your crontab is run with (you can put environment variable definitions at the top of your crontab, before the job specifications, in `key=value` form). This is given in the 2rd (unaccepted) answer to the linked duplicate. – Charles Duffy Feb 19 '18 at 13:42

1 Answers1

1

Try mentioning the full path to the environment script.

Replace

cmd1 = ['get_info', '-c', '-H', "install%", '-s', site]

to

cmd1 = ['FULL_PATH/get_info', '-c', '-H', "install%", '-s', site]
Rakesh
  • 81,458
  • 17
  • 76
  • 113
  • Or just set a reasonable `PATH` in your crontab file. Set `PATH=/bin:/usr/bin:/usr/local/bin` (if the `get_info` command is in `/usr/local/bin`), and there you are, without needing to modify any of the scripts that cron invokes (and thereby reduce their portability across environments -- as soon as you make your script run `/usr/local/bin/get_info`, it won't work somewhere `get_info` is in `/opt/local/bin`; but let the PATH be locally configured and the script can work anywhere). – Charles Duffy Feb 19 '18 at 13:40