pg_dump fails as a cron job in one script but not another

Posted on

pg_dump fails as a cron job in one script but not another – A server stack is the collection of software that forms the operational infrastructure on a given machine. In a computing context, a stack is an ordered pile. A server stack is one type of solution stack — an ordered selection of software that makes it possible to complete a particular task. Like in this post about pg_dump fails as a cron job in one script but not another was one problem in server stack that need for a solution. Below are some tips in manage your linux server when you find problem about linux, backup, cron, database, psql.

I have two cron jobs on my AWS server (triggered via crontab -e) to invoke pg_dump on specific databases.

The two scripts are identical with the exception of the target database they’re backing up ($PG_SRC_DB). One fails at the pg_dump command but outputs no errors nor logs:

PGPASSWORD=$PG_SRC_PASSWORD $PG_DUMP_PATH --no-owner $PG_SRC_DB -h $PG_SRC_HOST -p $PG_SRC_PORT -U $PG_SRC_USER > dump.sql

I know this because I’ve forwarded all stderr and stdout to /tmp/cron.log and I get the error:

mv: cannot stat 'dump.sql': No such file or directory

This command happens after I’ve run the pg_dump and the dump.sql file should have been created, but fails because pg_dump fails

I’ve debugged for hours and have done the following:

  • re-directed stderr to stdout to an error log
  • invoked export <PGVAR> for PGPASSWORD, PGUSER, PGHOST, and PGDATABASE
  • set my PATH to whatever my PATH is when I run the script manually in terminal
  • use the absolute path to pg_dump
  • use #!/bin/bash -l login shell
  • output pg_dump error to a file with ... > dump.sql 2> error.log

Both scripts work in terminal when run manually.
I have no idea how else to debug the pg_dump command as I’m not getting any logs from it.

I’m not entirely sure what happened but it works now. I did these two things:

  1. chmod +x pg_backup.sh — I had already done this, and the script was running in cron, since I could get some output in the logs
  2. run dos2unix on the file to transform any CR or CRLF to LF — I’m not sure how this would solve the issue since I wrote the script in Vim on a Mac

Generally, this happens because environment variable values are different for terminal and cronjobs. You can try to import terminal environment to script. Let’s say if you are running the script under user root. Try to add a line at top of the script.

#!/bin/bash
. /root/.profile

Leave a Reply

Your email address will not be published. Required fields are marked *