1

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?

2

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.
0

You can try this!

* * * * * cd /home/ && /usr/bin/python /home/hello.py >> /dev/tty3

(or your tty, if not tty3)

  • 1
    The user should be login that tty first. Otherwise we go back to the question - where goes STDOUT within Cron? :) – pa4080 Apr 3 '18 at 20:38

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.