So, I have this script that needs to run every minute, so I set up a cronjob to run the script, lets say the command is:


I use Webmin to setup the cronjob and set it to execute as www-data. I also use it to see the output when running the command.

When I execute the cronjob I get a permission error, when trying to create a file. But when I run the following command it works.

sudo -u www-data /var/www/script.py

Can anyone tell me why?


The file it is trying to create is called runnning.txt and is placed next to the script (/var/www/running.txt)

The error the command generates when running as a cronjob

Traceback (most recent call last):
  File "/var/www/lager-scanner/filer/pluk_script.py", line 237, in <module>
    with open(filename, "w+") as f:
PermissionError: [Errno 13] Permission denied: 'running.txt'

The contents of /etc/crontab


# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

Output of sudo -u www-data crontab -l

# * * * * * /var/www/script.py

I don't know how webmin does its thing. It's a webinterface, where I just select what user the command is supposed to be run by.

  • What file? Where? What exact error? What is the script doing? How does webmin tell it to run as www-data? Please edit your question and give us more details. Also show us the contents of /etc/crontab and sudo -u www-data crontab -l. – terdon Jun 1 '16 at 10:19
  • Added the requested info – Folkmann Jun 1 '16 at 10:28
  • OK, it looks like webmnin creates a user crontab. Is the # really part of the output of sudo -u www-data crontab -l? Does it make any difference if you delete the /var/www/running file before cron runs the script? Also, what happens if you run echo foo | sudo -u www-data tee /var/www/test? – terdon Jun 1 '16 at 10:31
  • Yes the # is part of the output, but that might be because I disabled the job in webmin, because it doesn't work. The script creates the running.txt when it starts and deletes it when it is done. If I run the echo foo | sudo -u www-data tee /var/www/test I just get foo back. – Folkmann Jun 1 '16 at 10:35
  • When I enable the job and run sudo -u www-data crontab -l, I get * * * * * /var/www/script.py. – Folkmann Jun 1 '16 at 10:54

Your Answer

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

Browse other questions tagged or ask your own question.