tags:

views:

207

answers:

3

Hello,

I'm trying to get cron to call in the correct path's. When I run a Python script from shell the script run's fine as it uses the path's set in bashrc but when I use cron all the path's are not used from bashrc. Is there a file I can enter the path's into for cron like bashrc or a way to call the path's from bashrc.

Sorry I don't think I worded this correctly, I can get the correct script to run (meaning the path to the script in crontab is not the problem here), it's just when that script is running I run a build and this uses the PATH's set in .bashrc. When I run the script when I'm logged in, the .bashrc path's are pulled in. Since cron doesn't run in a shell per say it does not pull in .bashrc. So is there a way of pulling this in without having to write a bash script wrapper?

Thanks

+3  A: 

You should put full paths in your crontab. That's the safest option.
If you don't want to do that you can put a wrapper script around your programs, and set the PATH in there.

e.g.

01 01 * * * command

becomes:

01 01 * * * /full/path/to/command

Also anything called from cron should be be very careful about the programs it runs, and probably set its own choice for the PATH variable.

EDIT:

If you don't know where the command is that you want execute which <command> from your shell and it'll tell you the path.

EDIT2:

So once your program is running, the first thing it should do is set PATH and any other required variable (e.g. LD_LIBRARY_PATH) to the values that are required for the script to run.
Basically instead of thinking how to modify the cron environment to make it more suitable for your program/script - make your script handle the environment it's given, by setting an appropriate one when it starts.

Douglas Leeder
if its in your path use 'which command' and it will give you the full path
Paul Whelan
@Douglas Leeder -- When you say put the full path's into cron, do you mean put it into crontab or another file? If it is how would you go about that if the cron command is: '01 01 * * * command'. Thanks
chrissygormley
@chrissygormley - Yes `crontab`.
Douglas Leeder
@Douglas Leeder -- Sorry there must be some confusion. I have reworded the question above.
chrissygormley
A: 

The default environment for cron jobs is very sparse and may be very different from the environment you develop your python scripts in. For a script that might be run in cron, any environment that you depend on should be set explicitly. In the cron file itself, include full paths to python executables and to your python scripts.

mobrule
A: 

I used /etc/crontab. I used vi and entered in the PATH's I needed into this file and ran it as root. The normal crontab overwrites PATH's that you have set up. A good tutorial on how to do this is: http://unixhelp.ed.ac.uk/CGI/man-cgi?crontab+5.

The system wide cron file look's like this:

       This has the username field, as used by /etc/crontab.
   # /etc/crontab: system-wide crontab
   # Unlike any other crontab you don't have to run the `crontab'
   # command to install the new version when you edit this file.
   # This file also has a username field, that none of the other crontabs do.

   SHELL=/bin/sh
   PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

   # m h dom mon dow user    command
   42 6 * * *    root    run-parts --report /etc/cron.daily
   47 6 * * 7    root    run-parts --report /etc/cron.weekly
   52 6 1 * *    root    run-parts --report /etc/cron.monthly
   01 01 * * 1-5 root python /path/to/file.py
chrissygormley