Crontab doesnt run python script

12,450

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.
Share:
12,450

Related videos on Youtube

Artem Andreev
Author by

Artem Andreev

Updated on September 18, 2022

Comments

  • Artem Andreev
    Artem Andreev over 1 year

    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?

  • pa4080
    pa4080 about 6 years
    The user should be login that tty first. Otherwise we go back to the question - where goes STDOUT within Cron? :)