0

On my Debian server I use crontab to schedule a python code. The problem is: when I add a line like this:

6 9 * * * python /path/to/daily_task.py &> $HOME/out.txt

Not too surprisingly the output states that python command is not found.

Of course I can just add a full path to crontab entry like /opt/python/3.9.2/bin/python, but I don't like this idea because it looks messy and this issue also persists when I need to, for example, do something like os.system("python task.py") in daily_task.py.

I tried adding the line like this to /etc/environment:

export PATH="$PATH:/opt/python/3.9.2/bin/"

After a reboot I indeed could use just python in my terminal window, but crontab's $PATH still only consists of /usr/bin.

Where should I put this code to change $PATH everywhere, including crontab (without altering the crontab file)?

2
  • 2
    Related if not a duplicate - how to set crontab PATH variable Commented Apr 23 at 14:09
  • @user9102437 when i open my crontab file, there is the following comment # You can also override PATH, but by default, newer versions inherit it from the environment #PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin, setting a "#" for your path variable, will comment the PATH line and the PATH will be inherited from the environment. Commented Apr 23 at 14:16

2 Answers 2

0

If you look at the contents of /bin, you'll see a lot of symlinks because many programs aren't placed directly into the bin dirs. You can do the same:

sudo ln -s /opt/python/3.9.2/bin/python /usr/bin

This way, you don't need to modify your crontab.

0

You can add environment variables in your crontab file itself like this:

PATH=/usr/bin:/whatever/bin
HOME=/home/myuser
6 9 * * * python /path/to/daily_task.py &> $HOME/out.txt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .