I
I
Ilyas2018-06-07 18:54:45
linux
Ilyas, 2018-06-07 18:54:45

How to redirect all script output to a file?

Hello, I wrote a script, it should be executed periodically, I registered it in the cron, but there is a problem with output to a file
here crontab

* * * * *   root python /home/root/main.py >> /home/root/app.log 2>&1

here is an example script
import logging

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

def main():
    pass

if __name__ == '__main__':
    logger.info('Run script...')
    main()

If I run it manually
python /home/user/main.py >> /home/user/app.log 2>&1

then the entire output with all the logs ends up in the file, but when it is executed in the cron, the file remains empty, the script fulfills 100%
UPD: if this is of course important - all this is launched under the docker

Answer the question

In order to leave comments, you need to log in

3 answer(s)
J
jcmvbkbc, 2018-06-07
@jcmvbkbc

registered it in the crown
In /etc/crontab? Only you need to specify the user in it, this field is not in user crontab.
Is that right 100%? What can be seen in /var/log/syslog?

S
Stanislav Pugachev, 2018-06-07
@Stqs

there was also such a case in my practice
, by default, cron uses not bash but sh, because of which you can observe how the same thing works in bash and does not work in cron
, this is because bash is a thing actually user and what if cron was posix compatible it uses the standard shell interpreter

A
Alex F, 2018-06-17
@delvin-fil

It's not clear why there is no indication of the time.
Clearly stated:
# mhdmw user command
1 * * * * /bin/sh /path_you_script
And of course it must have execute permissions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question