3

I hava executable a.py file worked fine when running in CLI. But after I wrote a a.sh script /root/a.py >> /root/a.log and start a crontab * * * * * /bin/sh /root/a.sh, it worked fine except no outputting in the log file.
The logging part of a.py was configured as follows:

DATE_FORMAT = '%a, %d %b %Y %H:%M:%S'
LOG_FILE = 'log'
RESULT_LOG_FILE = 'result.log'
LOG_FORMAT = '[%(asctime)s] [%(filename)s:%(lineno)d] [%(levelname)s] [%(threadName)s] [%(process)d] %(message)s'
logging.basicConfig(format=LOG_FORMAT, datefmt=DATE_FORMAT, level=logging.INFO, filename=LOG_FILE)  
logging.error('ERROR')  

I have tried to add /usr/local/bin/python in front of /root/a.py in a.sh but it did not work. I have no idea why this happened.

3
  • what log file did you check? the result.log or the a.log? according to your code the log file will be result.log and a.log should be empty. Commented Dec 10, 2015 at 1:46
  • Both of them no log outputting. In the code listed above it should be 'log' file, I configured in logging.basicConfig(filename=LOG_FILE) Commented Dec 10, 2015 at 2:01
  • I mean both of 'log' and 'result.log' no output, there are some print statement output in a.log Commented Dec 10, 2015 at 2:11

1 Answer 1

7

The command /root/a.py >> /root/a.log does output redirection into the file /root/a.log. The >> = append, whereas > would overwrite.

Your script is logging to result.log, not a.log. And unless you have a print statement, nothing is going to go into a.log.

In your logging.basicConfig, log events are not output to console, only to the log file, so there is no "output" to re-direct to a.log.

(Add a print 'hello console' in your script, you should see that in a.log.)

Edit from comments:

result.log may not be where you think. Since its path is determined by where it's executed from, not where the script is. Change RESULT_LOG_FILE = 'result.log' to RESULT_LOG_FILE = '/root/result.log'

Sign up to request clarification or add additional context in comments.

2 Comments

Actually I have print statement in a.py file and it shown in a.log. But the output of logging.error('ERROR') did not record in the log(LOG_FILE) as what I configured in logging.basicConfig.
result.log may not be where you think. Since its path is determined by where it's executed from, not where the script is. Change RESULT_LOG_FILE = 'result.log' to RESULT_LOG_FILE = '/root/result.log' and try.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.