0

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/boinc.py

Thanks

1
  • Command part can be cd /var/www/html && python ./boinc.py. Jun 8, 2022 at 7:58

2 Answers 2

0

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.

1
  • Thank you, I just changed the location in the script where index.html is saved. Actually quite simple in the end. haha. I'm new to python and Linux. thanks again.
    – David
    Jun 8, 2022 at 8:02
0

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. :)

You must log in to answer this question.

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