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)
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With