Zeinab
Zeinab

Reputation: 21

how to run python script with crontab with proxy

I am trying to run a python script from bash script to upload files to a website using crontab. The script runs from terminal but doesn't work from crontab and the error is

"Error: <urlopen error [Errno -2] Name or service not known>"

The python script is: #!/usr/bin/env python

 from ConfigParser import ConfigParser
 from mechanize import Browser, RobustFactory
 import base64
 from os.path import basename 
 import sys
 import os

 print(os.environ)

 if __name__ == "__main__":

 if len(sys.argv) != 2:
    print "Error: 1 argument needed"
    sys.exit(1)

  #DATA FROM CFG FILE
  url = "http:website"
  cfn = "absolute_path/upload.cfg"
  cfg = ConfigParser()

  try:
    cfg.read(cfn)
    usr = cfg.get('Auth', 'user')
    pwd = cfg.get('Auth', 'pass')
  except Exception, e:
    print "Error:", e
    sys.exit(1)

  if not usr or not pwd:
    print "Error: username or password not valid."
    sys.exit(1)
    filename = sys.argv[1]

try:
    br = Browser(factory=RobustFactory())
    br.addheaders.append(('Authorization', 'Basic %s' % base64.encodestring('%s:%s' % (usr, pwd))))

    br.open(url)
    br.select_form(nr=1)
    br.form.add_file(open(filename, 'rb'), 'text/plain', basename(filename))
    br.submit()

    print "File '%s' successfully uploaded" % filename
    sys.exit(0)

    except Exception, e:
    print "Error:", e
    sys.exit(1)

When I try to "print(os.environ)" in python script in the case of manual run and using crontab:

with crontab :

"{'MAILTO': '/var/mail/envclim', 'LANG': 'en_US.UTF-8', 'SHELL': '/bin/sh',    'XDG_RUNTIME_DIR': '/run/user/1000', 'SHLVL': '2', 'PYTHONPATH': '/usr/lib/python2.7/site-packages:/usr/lib/python2.7/site-packages/', 'OLDPWD': '/home/envclim/upload_sdswas', 'PWD': '/home/envclim/upload_sdswas/upload_scripts', 'LOGNAME': 'envclim', 'USER': 'envclim', 'HOME': '/home/envclim', 'PATH': '/bin:/usr/bin:/usr/local/bin:/usr/local/CDO/bin', 'XDG_SESSION_ID': '6', '_': '/usr/bin/python'}"

but in the manual case , there is the proxy, as the following:

{'HTTP_PROXY': 'http://10.51.51.51:80/', 'KDE_IS_PRELINKED': '1', 'ALL_PROXY':  'socks://10.51.51.51:80/', 'NO_PROXY': 'localhost,127.0.0.0/8,::1', 'GJS_DEBUG_OUTPUT': 'stderr', 'http_proxy': 'http://10.51.51.51:80/', 'FTP_PROXY': 'http://10.51.51.51:80/',----------} 

In crontab, I add :

SHELL=/bin/sh
PATH=/bin:/usr/bin
PYTHONPATH=/usr/lib/python2.7/site-packages
MAILTO=/var/mail/envclim

I am using fedora19. I tried to disabled proxy, but the script didn't work at all.

please, can anyone solve this problem?

Thanks in advance Zeinab

Upvotes: 0

Views: 4237

Answers (1)

Zeinab
Zeinab

Reputation: 21

Thanks alot for your comments, I solved this problem by adding these lines in crontab:

PYTHONPATH=/usr/lib/python2.7/site-packages
HTTP_PROXY=http://10.51.51.51:80/

and in the bash file that used to run the python script:

export PYTHONPATH=$PYTHONPATH:/usr/lib/python2.7/site-packages/
export LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib:/usr/local/usr/lib

Upvotes: 1

Related Questions