6

I am using crontab to autostart the program, using the following command in cronatb

@reboot sudo python /home/Desktop/Appllication.py

Is it possible to write all the logs (errors and other stuff) by appending something to the above command, so that cron writes all errors/ any related events to a log ?

UPDATE: The code prints values in the python idle terminal when run individually using the print command. How do I make this data which is printed on the terminal to be written to the log file. for ex, if my code is like print "food morning" , how do I get this to be written into the log file instead of printing on terminal.

TEST CODE:

import time
while(1):
    print "testing"
    time.sleep(5)
bobdxcool
  • 91
  • 1
  • 1
  • 10

2 Answers2

6

if /home/Desktop/Application.py prints the error log to STDOUT, you can simply redirect the output of the script like this

python /home/Desktop/Application.py >> /home/my_application_log.txt

Expanding the answer with a couple of useful links.

Redirect all output to file is a really good SO thread. You can try the code snippet there using your test code as the output generator.

Also, python comes with an excellent logging module. If you want to incorporate a robust logging system, I highly recommend looking into it: python logging

Community
  • 1
  • 1
Haleemur Ali
  • 26,718
  • 5
  • 61
  • 85
  • The code prints values in the python idle terminal when run indivdually using the print command. How do I make this data which is printed on the terminal to be written to the log file. for ex, if my code is like print "food morning" , how do I get this to be written into the log file instead of printing on terminal. – bobdxcool Dec 27 '14 at 23:04
  • The code I and @Digisec show are meant to do exactly that. Are you sure the log file is not being generated? – Haleemur Ali Dec 28 '14 at 02:34
6

You are actually including a sudo in there. Sudo requires a password, you would have to add a configuration item in sudoers to run that script passwordless. Your other option is to add it to the root cron. Use @Haleemur's redirection command to redirect the output to the logs. I would actually suggest using the following as well just in case it fails and outputs to STDERR

@reboot python /home/Desktop/Application.py >> /home/Desktop/log_file 2>&1

UPDATE:

try running the following command from the terminal

python /home/Desktop/Application.py >> /home/Desktop/log_file 2>&1

if it works in the command line, there is no reason it shouldn't work in cron.

Digisec
  • 700
  • 5
  • 9
  • thank u. I did sudo crontab -e and added the command of urs, and rebooted it. still nothing is being written to the log file. I have added the test script above in the original post above. If I deliberately make mistakes in the code, only then is the error being written to the log file. But, the actual output (print statement of python code) is not getting written. – bobdxcool Dec 28 '14 at 01:41
  • Are you sure that the script is running ? It sounds to me like the script is not running at all. Can we see snippets of the code ? That behavior is odd. – Digisec Dec 28 '14 at 01:44
  • so, i found the problem. my script was running continuously in a while loop.For some reason, it writes to the file, at the end of program execution. So, if it is in a while loop, we cannot see anything being written to the text file log. Is there any solution for this ? – bobdxcool Dec 28 '14 at 02:22
  • Write to the file from within the script. – Digisec Dec 28 '14 at 02:28
  • @bobdxcool: if it's not writing till end of execution, python buffers it's output and flushes the buffer on exit. To prevent that, you can use the paremeter -u to output unbuffered: `python -u scriptname.py` – andreas Aug 02 '19 at 14:39