0

I really can't see what's wrong here. I have a Digital Ocean droplet that runs a bash script every 10 mins to get some records from my DB of a Heroku slug. This is my script:

# /bin/sh
echo "Starting pull from postgres ..."
heroku pg:psql dbname --app app --command "COPY (select wallet from wallets where tokens_sent > 0) TO STDOUT WITH CSV" > wallets.csv

It gets all wallets and save them in the wallets.csv file. If I run this script in the terminal with sh get_tokens.sh, everything works fine! For some unknown reason, when I put this script in my crontab, it doesn't pull the data from Heroku.

I'm a root user and the root user is running the script. Below are the file permissions:

 4 -rwxr-xr-x 1 root root   782 Feb  3 18:18 get_tokens.sh
16 -rw-r--r-- 1 root root 12318 Feb  3 18:21 wallets.csv

Here is my crontab record:

10 * * * * bash ~/get_tokens.sh >> logs.txt

I added the logs.txt to see if potentially some error would pop up but nothing seems wrong.

Any ideas? Thanks in advance

Deric Lima
  • 1,972
  • 2
  • 24
  • 33
  • 1
    Standard cron job troubleshooting: capture errors as well as regular output with `>> logs.txt 2>&1`. Also, cron jobs run with a really minimal environment; what environmental dependencies does your script (and heroku itself) have? Does it work if you run it with `env -i bash ~/get_tokens.sh` (which is not the same as a cron job's environment, but closer than normal)? – Gordon Davisson Feb 03 '22 at 19:23
  • Thanks a lot @GordonDavisson. Thx to you I found the real reason why I was having issues. I got `heroku not found`. For some reason the script couldn't read the `snap/bin/` folder in order to find the heroku bin. I added the complete path and the issue was gone – Deric Lima Feb 04 '22 at 00:59
  • That's a really common problem in cron jobs. One of the things that's minimal in a cron job's environment is `PATH` (i.e. the list of places to look for command binaries) -- it's generally just /bin and /usr/bin, so anything outside those won't be found unless you set `PATH` or use explicit paths to binaries. – Gordon Davisson Feb 04 '22 at 03:12

1 Answers1

1

You aren't providing an absolute path to wallets.csv:

# /bin/sh
echo "Starting pull from postgres ..."
heroku pg:psql dbname --command "COPY... TO STDOUT WITH CSV" > wallets.csv

That file is relative to whatever the current working directory is, which probably isn't the directory you expect. I suggest you provide a full path:

# /bin/sh
echo "Starting pull from postgres ..."
heroku pg:psql dbname --command "COPY... TO STDOUT WITH CSV" > /path/to/wallets.csv

Or you can cd into the right directory:

# /bin/sh
echo "Starting pull from postgres ..."
cd /path/to/some/directory
heroku pg:psql dbname --command "COPY... TO STDOUT WITH CSV" > wallets.csv

See What is the 'working directory' when cron executes a job? on Unix & Linux.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257