Thread: error messages not getting logged when running script from cron
I have a cron script that does the following:
PGPASSWORD=$PGPASSWORD /opt/PostgreSQL/9.4/bin/pg_dump -t RECORDER -Fc $i -U postgres -Z0 | xz -9 > "$backup_dir/$i-$timeslot-database"
xzcat "$backup_dir/$i-$timeslot-database" | /opt/PostgreSQL/9.4/bin/pg_restore -h $backupHost -U postgres -d backupDB -c -p 5434 -v
My cron tab entry:
0 20 * * * db_backup.sh 1> /dev/null 2>&1 | mail -s "backup cron" myemail@comp.com
I am re-directing stderr to stdout and then sending that to email.
I get an email nightly with that title, but no error messages.
Last night, the restore never finished, but there was no error output.
Any ideas to get an email with error info.
thanks a lot
On Fri, Nov 20, 2015 at 5:09 PM, anj patnaik <patna73@gmail.com> wrote: > 0 20 * * * db_backup.sh 1> /dev/null 2>&1 | mail -s "backup cron" myemail@comp.com > > I am re-directing stderr to stdout and then sending that to email. But you are redirecting stdout (and therefore also stderr) to /dev/null -- therefore there is no body input to the mail command. > I get an email nightly with that title, but no error messages. In that case you will probably be fine if you remove "1> /dev/null" from the command. -- Kevin Grittner EDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company
anj patnaik <patna73@gmail.com> writes: > My cron tab entry: > 0 20 * * * db_backup.sh 1> /dev/null 2>&1 | mail -s "backup cron" > myemail@comp.com > I am re-directing stderr to stdout and then sending that to email. Uh, read it again: you're redirecting stdout to /dev/null and then redirecting stderr to go where stdout goes. So all output is going to the bit bucket, not the pipe. regards, tom lane
You could also try tweaking the following attached backup script.
Caution, I wrote this quickly from a skeleton script and has not been tested.On Mon, Nov 23, 2015 at 3:21 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
anj patnaik <patna73@gmail.com> writes:
> My cron tab entry:
> 0 20 * * * db_backup.sh 1> /dev/null 2>&1 | mail -s "backup cron"
> myemail@comp.com
> I am re-directing stderr to stdout and then sending that to email.
Uh, read it again: you're redirecting stdout to /dev/null and then
redirecting stderr to go where stdout goes. So all output is
going to the bit bucket, not the pipe.
regards, tom lane
--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general
--
Melvin Davidson
I reserve the right to fantasize. Whether or not you
wish to share my fantasy is entirely up to you.
I reserve the right to fantasize. Whether or not you
wish to share my fantasy is entirely up to you.

Attachment
On 11/23/2015 12:21 PM, Tom Lane wrote: >> >0 20 * * * db_backup.sh 1> /dev/null 2>&1 | mail -s "backup cron" >> >myemail@comp.com >> >I am re-directing stderr to stdout and then sending that to email. > Uh, read it again: you're redirecting stdout to /dev/null and then > redirecting stderr to go where stdout goes. So all output is > going to the bit bucket, not the pipe. the 2>&1 notation is not completely intuitive.... if you want to redirect stderr to the pipe and bitbucket stdout, do it in the opposite order. .... 2>&1 1> /dev/null | ... that sends stderr to the file that stdout was assigned, and sends stdout to the bit bucket... note specifically that redirecting stdout won't affect the file stderr is being sent. -- john r pierce, recycling bits in santa cruz
I am seeing a bizarre behavior. The command works fine when called directly from prompt, but when invoked via cron, there is no output nor mail.
This works fine:
script 2>&1 | tee /tmp/ff ; mailx -s "email" -myemail@company.com < /tmp/ff
Now, I place the identical line in crontab file and the output file /tmp/ff does not get generated.
Inside script, i am running a pg_restore which dumps output to stdout. The usage of tee is the only way to cause it to go into a file.
Any idea here? Thanks
On Mon, Nov 23, 2015 at 3:29 PM, Melvin Davidson <melvin6925@gmail.com> wrote:
You could also try tweaking the following attached backup script.Caution, I wrote this quickly from a skeleton script and has not been tested.On Mon, Nov 23, 2015 at 3:21 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:anj patnaik <patna73@gmail.com> writes:
> My cron tab entry:
> 0 20 * * * db_backup.sh 1> /dev/null 2>&1 | mail -s "backup cron"
> myemail@comp.com
> I am re-directing stderr to stdout and then sending that to email.
Uh, read it again: you're redirecting stdout to /dev/null and then
redirecting stderr to go where stdout goes. So all output is
going to the bit bucket, not the pipe.
regards, tom lane
--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general
--Melvin Davidson
I reserve the right to fantasize. Whether or not you
wish to share my fantasy is entirely up to you.
On 11/23/2015 04:33 PM, anj patnaik wrote: > I am seeing a bizarre behavior. The command works fine when called > directly from prompt, but when invoked via cron, there is no output nor > mail. > > This works fine: > script 2>&1 | tee /tmp/ff ; mailx -s "email" -myemail@company.com > <mailto:myemail@company.com> < /tmp/ff > > Now, I place the identical line in crontab file and the output file > /tmp/ff does not get generated. Does the script run? Remember cron has a limited environment. In you actual crontab line do you have a complete path to 'script' or is the path in the crontab PATH. To get your above code to run I had to do something like: cat cron_txt.txt 2>&1 | tee /tmp/ff ; mailx -s "email" aklaver </tmp/ff Note, no - before the email address(in this case a local address). If you are doing this locally have yoy though about setting MAILTO in the crontab and let cron do the mailing. Probably have to use pg_restore -v ... if you are not already. > > Inside script, i am running a pg_restore which dumps output to stdout. > The usage of tee is the only way to cause it to go into a file. > > Any idea here? Thanks > > On Mon, Nov 23, 2015 at 3:29 PM, Melvin Davidson <melvin6925@gmail.com > <mailto:melvin6925@gmail.com>> wrote: > > You could also try tweaking the following attached backup script. > Caution, I wrote this quickly from a skeleton script and has not > been tested. > > On Mon, Nov 23, 2015 at 3:21 PM, Tom Lane <tgl@sss.pgh.pa.us > <mailto:tgl@sss.pgh.pa.us>> wrote: > > anj patnaik <patna73@gmail.com <mailto:patna73@gmail.com>> writes: > > My cron tab entry: > > > 0 20 * * * db_backup.sh 1> /dev/null 2>&1 | mail -s "backup cron" > >myemail@comp.com <mailto:myemail@comp.com> > > > I am re-directing stderr to stdout and then sending that to email. > > Uh, read it again: you're redirecting stdout to /dev/null and then > redirecting stderr to go where stdout goes. So all output is > going to the bit bucket, not the pipe. > > regards, tom lane > > > -- > Sent via pgsql-general mailing list > (pgsql-general@postgresql.org <mailto:pgsql-general@postgresql.org>) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-general > > > > > -- > *Melvin Davidson* > I reserve the right to fantasize. Whether or not you > wish to share my fantasy is entirely up to you. > > -- Adrian Klaver adrian.klaver@aklaver.com