29

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?

shafeeq
  • 1,499
  • 1
  • 14
  • 30
  • 4
    Possible duplicate of [how to redirect complete output of a script](http://stackoverflow.com/questions/1887618/how-to-redirect-complete-output-of-a-script) – Bhavesh Apr 06 '16 at 04:11
  • http://unix.stackexchange.com/questions/52330/how-to-redirect-output-to-a-file-from-within-cron – Bhavesh Apr 06 '16 at 04:13
  • `cron` is running in the background of the shell, so to which `stdout` you want it redirect the output to? – Avihoo Mamka Apr 06 '16 at 06:23
  • In my case I can enable debug log from a terminal. I want to set that terminal as stdout – shafeeq Apr 06 '16 at 07:28
  • @AvihooMamka: It's not running in the background *of the shell*. It's not associated with any shell. – Keith Thompson Dec 31 '16 at 20:27
  • That's probably not a sensible thing to do. What do you want to happen when you log out or quit your current shell process, and the cron job keeps running? – Keith Thompson Dec 31 '16 at 20:29
  • achoora's answer worked for me. In the case of shell exit, we will need to specify new terminal name if need the log again. – shafeeq Jan 02 '17 at 07:19

3 Answers3

52

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
codeforester
  • 39,467
  • 16
  • 112
  • 140
gaga5lala
  • 1,218
  • 13
  • 17
  • How is that useful? On my system, for example, stdout and stderr of the `/usr/bin/cron` process are directed to sockets. – Keith Thompson Dec 31 '16 at 20:36
  • 12
    @KeithThompson My scenario is that I use a docker container running cron service (use `cron -f` make cron running at foreground), and since I want to collect logs for monitoring and analytics. According to 12 factor app - `Treat logs as event streams` so I redirect cron log to stdout. – gaga5lala Jan 01 '17 at 17:52
  • Does that actually work? Are you able to access data written to those sockets? – Keith Thompson Jan 01 '17 at 20:16
  • @KeithThompson Yes, it works for me at debian:jessie. I don't access the sockets directly (haven't try it.), but using `docker logs` and fluentd. – gaga5lala Jan 04 '17 at 03:16
  • 6
    It crosses my mind that for the docker scenario it is more accurate to specify `procid=1` : `/proc/1/fd/1 2>&1` because proc 1 is what goes out to docker logs – Chris F Carroll Dec 08 '17 at 10:28
  • 1
    How can you then tail this? Because if I try `tail -f $(/proc/$(cat /var/run/crond.pid)/fd/1)` I get permission denied – P.Lorand May 07 '19 at 09:48
  • 1
    For `docker` you [don't need](https://gist.github.com/mowings/59790ae930accef486bfb9a417e9d446) `crond.pid`. And please make it clear that you provide a `docker`-only solution. Otherwise it's confusing. – x-yuri Dec 20 '20 at 20:14
  • 1
    @gaga5lala can u provide some code snippets of your solution? I have tried it for two days now, but cannot get it working. – Max Schindler Apr 21 '21 at 12:03
  • 1
    Is it sufficient to use `>` when one wants to append and not overwrite in this case, or will that still overwrite previous logs? If it overwrites logs, could you add a note for other people, that one needs to choose between `>>` and `>`? – Zelphir Kaltstahl Jan 09 '22 at 14:24
10

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

achoora
  • 1,270
  • 1
  • 16
  • 32
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.