How to redirect cron job output to stdout

删除回忆录丶 提交于 2019-12-18 11:32:34

问题


I have a cron job and its output is now redirected into a file. It looks like the following

0 9 * * * /bin/sh /bin/cleanup.sh > /home/darkknight/cleanup.log

Can any one help me to rediect its output to stdout?


回答1:


Enter tty on any terminal and we will get device file for that particular teminal window like /dev/pts/1. Redirct the cron job into this file as cleanup.sh > /dev/pts/1




回答2:


Running process has a PID and its fd (file descriptor) is mapping to /proc/<PID>/fd. And we can find PID of the running cron process at /var/run/crond.pid.

To send cron log to stdout, we could write log to fd number 1 of the process started by cron.

0 9 * * * /bin/sh /bin/cleanup.sh > /proc/$(cat /var/run/crond.pid)/fd/1 2>&1



回答3:


Run cat /home/darkknight/cleanup.log then you get the output on STDOUT. If you can't see what you expect as output, maybe you need to modify the cron as following:

0 9 * * * /bin/sh /bin/cleanup.sh > /home/darkknight/cleanup.log 2>&1

To get what cleanup.sh writes on its STDERR.

If you don't want to lose the output of yesterday, modify as following:

0 9 * * * /bin/sh /bin/cleanup.sh >> /home/darkknight/cleanup.log 2>&1

Or, just execute /bin/sh /bin/cleanup.sh then you get both STDOUT and STDERR on your terminal.



来源:https://stackoverflow.com/questions/36441312/how-to-redirect-cron-job-output-to-stdout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!