0

I am trying to schedule running a Python module in cron. This question is somewhat similar to this but I think asks for a different use case.

Task

I have a Python script I run in the shell as a module like this:

python -m myscript

It prints a bunch of numbers (via print) and works when I run it from the shell.

Question

I am now trying to run this every minute using a cron job, like so:

*/1 * * * * python -m myscript

Question1: This does not print to the terminal as expected / I don't see any output. Why? (To test if it is running at all, I redirected output to a file, which creates an empty file every minute).

Question2: My thinking was that any command that works when I run it manually in the shell will also work the same way when started via cron. Is that mistaken? E.g do I still have to make the script executable and such?

Question3: Thinking about it, I was not quite sure where cron will direct e.g. a print / stdout command and could not find in the docs. Do I have to / Should I manually specify the output target if I want it to print to a new shell window?

Currently running this on elementary OS but I want to eventually migrate to a Raspberry Pi. Any help is much appreciated!

patrick
  • 4,455
  • 6
  • 44
  • 61
  • Why print to stdout if you need it to fill a file? Change your script to write a file instead. Check if file exists, if so, count up some number in it and create new (or append to ever same file) ... – Patrick Artner Feb 23 '19 at 14:19
  • 1
    @PatrickArtner thanks for your response, sry I was not quite clear -- I created the file to check if the script runs at all, since I did not see anything in the shell. expected output is print, I will edit – patrick Feb 23 '19 at 14:26
  • This llooks liek a duplicate: [start-python-script-with-cron-and-output-print-to-a-file](https://stackoverflow.com/questions/24957734/start-python#) - please check. If I flag it as such it gets dupe-hammered due to python gold badge – Patrick Artner Feb 23 '19 at 14:28
  • @PatrickArtner thanks for pointing me to the post, I did overlook that. However, my goal is to print to stdout, not redirect to any file as described in the post you linked. Thank you for your patience, I think I will need to clarify my question further. Short: when I run this script by hand, it prints stuff to the shell. When I run it via cron, it does not... – patrick Feb 23 '19 at 14:32

0 Answers0