crontab -e is running my python script but its saving the results of the script in my home directory

I’ve got a python script that takes data from some XML files and displays them on a website. The script overwrites the index.html file each time it’s run and replaces some text in a HTML template with data stored in some variables. The script works fine.

I put the command in crontab to run every hour, and it works. But it saves the new updated index.html file to the home directory but I want it saved to /var/www/html/. The python script is running from the same location.

Is there a way I can have crontab run the script and save the index.html file the script generates to the correct location? (/var/www/html)

This is the command I’ve put in crontab:

0 * * * * python /var/www/html/


Asked By: David


The location of the script is irrelevant to its execution. When cron runs a job it sets the current directory to the home directory of the user.

You need to set the working directory yourself, or else modify your script to write to the correct place.

Answered By: roaima

You must always be mindful of cron‘s characteristics. Many assume that just because it’s their user’s crontab, that cron also inherits their user environment from the interactive shell. But this isn’t how it works.

If you’re ever curious about the environment used to run your cron jobs, create this simple job in your crontab, and cron will tell you what its environment is:

* * * * * /usr/bin/printenv > /home/me/mycronenv.txt 2>&1

You can stop that job as soon as you get an answer by editing your crontab again. 🙂

Answered By: Seamus
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.