2

I have a python script that I need to run from crontab routing all traffic via a HTTP proxy.

I have already set the proxy in /etc/profile using

http_proxy=http://192.168.0.1:3128    # (Address changed for privacy)
https_proxy=http://192.168.0.1:3128
export http_proxy
export https_proxy

Of course this works fine if I run myscript.py from terminal but when adding the job to crontab it reverts to using standard server IP upon running.

What is the safest way to assure the proxy is used by any python script run from cron? I did find one mention of adding

HTTP_PROXY=http://192.168.0.1:3128
HTTPS_PROXY=http://192.168.0.1:3128

to the top of the crontab. This seems to work when testing with a simple python script to ping an IP checker website, but is it the safest way?

There is no documentation on this other than one old post I found.

1

That second example looks like the right thing to do, but you should change the variable names to lower-case, as the HTTP libraries expect.

The manual page for the crontab file format (i.e. crontab(5) - not crontab(1)) says:

An active line in a crontab will be either an environment setting or a cron command. The crontab file is parsed from top to bottom, so any environment settings will affect only the cron commands below them in the file. An environment setting is of the form,

      name = value

So that's exactly the way to set environment variables for your cron jobs.


Note also, if this is intended to be a system-wide setting (I'm guessing it is, given your reference to /etc/profile), then

On the Debian GNU/Linux system, cron supports the pam_env module, and loads the environment specified by /etc/environment and /etc/security/pam_env.conf. It also reads locale information from /etc/default/locale.

If your cron is similarly configured, it may make sense to move your defaults from /etc/profile to /etc/environment.

  • Thank you Toby. Yes this is intended to be as system wide as possible. It is running in a VMware Ubuntu image with only one job intended. I shall read some more about pam. I had previously seen it mentioned – mburnatacu1980 Sep 7 '17 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.