How to Redirect Cron Job Output to Stdout

How to redirect cron job output to stdout

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

Redirect crontab stdout to stdout instead of default email


Redirecting the stderr I got slacktee.sh command not found. So using slacktee.hs as root works but not when root uses it in crontab scheduled job (in a daily scheduled script i use slacktee successfully). Why?

Because the PATH variable for user root and for user cron are different.

Instead of just 'slacktee' use a full path, i.e. /usr/local/bin/slacktee and it should work OK.

Cron redirect stdout and stderr to different files


This way, I'm redirecting both the stdout and stderr to the log_listener.txt file. Is it possible to split them, and send them to two different files?

Youre redirecting b oth to log_listener.txt because you specifically asked that stderr (2)'s output be redirected (>) to stdout (&1).

If that's not what you want, don't do that?

> f is a shortcut for 1>f which redirects output (>) from stdout (1) to the file f, truncating it if it exists.

2 is stderr, so you can just 2> to some other file in order to redirect stderr to a different file than stdout. >> is an appending redirection so 2>> will append stderr to an existing file (or create it) in the same way >> does for stdin.

Therefore you can just have a command of

cron_restart.sh >> log_listener.txt 2>>log_error.txt

to log stdout and stderr to different files.

How to redirect stderr to a file in a cron job

It's the other way around:

*/1 * * * *  sudo /home/pi/coup/sensor.py  >> /home/pi/sensorLog.txt 2>&1

2>&1 will redirect standard error (2) to standard output (1) which -in turn - was redirected to your log file. So, at the end, both stderr and stdout will go to your sensorLog.txt

Redirecting the output of a cron job

Your redirection order is incorrect. Stderr is not being redirected to the file, but is being sent to stdout. That's what you must be receiving in your mail.

Fix the redirection by changing your cron job to:

0 5 * * * /bin/bash -l -c
'export RAILS_ENV=my_env;
cd /my_folder;
./script/my_script.rb > ./log/my_log.log 2>&1'

Cronjob - How to output stdout, and ignore stderr

The 2>&1 redirects stderr to stdout, appending it to the headlines.txt.
So, redirect stderr to /dev/null:

* * * * * /Users/me/bin/scrape-headlines /Users/me/proxies.txt >> /Users/me/headlines.txt 2> /dev/null


Related Topics



Leave a reply



Submit