How to redirect complete output of a cron script

前端 未结 4 547
星月不相逢
星月不相逢 2021-01-03 07:13

I have a simple cronjob running every day at 18:35:

05 18 * * * ~/job.sh 2>&1 >> ~/job.log

So the output of ~/job.sh should be

4条回答
  •  粉色の甜心
    2021-01-03 07:46

    You need to redirect the output of the python script. Learning Python 2nd Edition recommends the following:

    import sys
    sys.stdout = open('log.txt', 'a')   # Redirects output to file
    ...
    print x, y, x                       # shows up in log.txt
    

    and goes on to say:

    "Here, we reset sys.stdout to a maually-opened output file object opened in append mode. After the reset, every print statement anywhere in the program will write its text to the end of file log.txt. instead of the original output stream."

提交回复
热议问题