Ubuntu – Crontab doesnt run python script

bashcommand linecronpythonscripts

I have the following issue: my 'hello world' python script can't be run by crontab.

If I set crontab instruction like this:

* * * * * cd /home/ && /usr/bin/python /home/hello.py

Text doesn't appear in terminal.

But if I do:

* * * * * cd /home/ && /usr/bin/python /home/hello.py >> /home/log.txt

Ubuntu appends 'hello world' text to log.txt

here is my script:

#!/usr/bin/env python
print('Hello World!')

What am I doing wrong?

P.S. already read this topic Why crontab scripts are not working?

Best Answer

Your script is executed by Cron and everything works as it is expected. Just Cron isn't designed to output anything into a terminal. So, IMO, the correct question here should be something like: Where the standard output goes within Cron?

Unless it is redirected (>, >>) or piped (|) to another program, everything that usually will be outputted to the STDOUT (if you are execute a command in the command-line), including all error messages, will be sent to the local mailbox of the user that runs the Cronjob. To send/receive these emails you should apply a minimal configuration as it is described here: How do I set Cron to send emails?

Most of the suggestions in the proposed duplication explain how to redirect the output of a Cronjob to TTY or terminal window, but to get the output there you should be login (in that TTY or terminal window) in advance. Here are few additional examples:


In addition, in this case:

  • cd /home/ is not needed because your script doesn't write anything there, and the script is called by its full path.
  • /usr/bin/python is not neede, because you tell the system that is Python script by the shebang #!/usr/bin/env python. But in this case the file should have executable permissions: chmod +x /home/hello.py.
Related Question