9

I'm starting a python script using cron, like this:

#Python script called every 3 minutes in case script crashes
*/3 * * * * /home/ubuntu/python_script.sh &

And my script has several prints for debugging. I want to print those messages into a file, for example /home/ubuntu/Logs.txt

My script also check if the python call is already running so it won't start it again:

#!/bin/bash
lines=$(ps -ef | grep python_script.py | wc -l)

if [ $lines -ne 2 ]; then
    python python_script.py &
fi

How do I print my messages to this file? I'm using python 2.7


Update: Edited my code with the following:

1) Added this as the first line of my python script:

from __future__ import print_function

2) Opened the log file after the last import, and right after that added a test print:

log = open("/home/ubuntu/Logs.txt", "w+")
print("Testing...", file = log)

If I simple run it from the terminal, it prints to the file fine. But if I schedule it with cron it doesn't write to the file.


Update 2: Ended up stumbling on a solution with "logger" command. On my shell script I had to add "2>&1" (in the shell script, not crontab as described by ElmoVanKielmo) and " | logger &". Final solution looks like this:

#!/bin/bash
lines=$(ps -ef | grep python_script.py | wc -l)

if [ $lines -ne 2 ]; then
    python python_script.py 2>&1 | logger &
fi

and it outputs any prints to /var/log/syslog file, which is good enough for me.

Vini.g.fer
  • 11,639
  • 16
  • 61
  • 90
  • Tried the "from __future__ import print_function" way, but not working. Edited the question to include what I tried. – Vini.g.fer Jul 25 '14 at 14:21
  • 1
    @alfasin no. Combined with cron, together with existing prints in OP's code, this is not a good idea. 1. You don't know if OP uses this script outside of cron. If so, then user might want to see the output on the terminal. 2. Script can possibly deployed on different machines and hard-coding a path to the output file is wrong. – ElmoVanKielmo Jul 25 '14 at 14:23
  • @ElmoVanKielmo On the contrary, printing from cron-job is a bad idea since you don't control the output. Second, it is actually a *good* idea to have the logs under the same path in different machines! – Nir Alfasi Jul 25 '14 at 14:28

1 Answers1

17

In your crontab put this:

*/3 * * * * /home/ubuntu/my_script.sh >> /home/ubuntu/Logs.txt

It will redirect stdout to this file.
You might also want to have stderr redirected (exceptions etc.), so:

*/3 * * * * /home/ubuntu/my_script.sh >> /home/ubuntu/Logs.txt 2>> /home/ubuntu/errors.txt

Or you can have them all in one file:

*/3 * * * * /home/ubuntu/my_script.sh >> /home/ubuntu/Logs.txt 2>&1
ElmoVanKielmo
  • 10,907
  • 2
  • 32
  • 46
  • 1
    None of those schedules on cron worked for me. Somehow python seems to be hiding the output, because this works with a simple "echo" or "ping" command. – Vini.g.fer Jul 25 '14 at 16:45
  • @Vini.g.fer seems like a permissions problem. Are you running this cronjob as `ubuntu` user or more importantly as the same user as from command line? – ElmoVanKielmo Jul 28 '14 at 08:50
  • 1
    Same user as the command line, which is ubuntu user. I call cron like this: sudo crontab -e -u ubuntu – Vini.g.fer Jul 28 '14 at 10:34