Bash script run as cron job outputs different results than running script normally

I made a bash script that updates a generic google account's password with an xkcd style password, and then emails the password to a specified user. This is meant for generic substitute teacher accounts to have a rotating password. My script looks like this:

#!/bin/bash
testpass=$(/usr/local/bin/xkcdpass --min=4 --max=5 --numwords=3 -d='-')
/home/it/bin/gam/gam update user test@example.org password $testpass
/bin/echo -e $(<test.txt) '\n' $testpass | /usr/bin/mail -s "Substitute Account Credentials" secretary@example.org

test.txt just says:

Hello, the sub account password for the week is:

I am able to get the desired results if I just run the script from the console. It sends the email saying

Hello, the sub account password for the week is: 
blitz-cider-idly

However, when run as a cron job, the email seems to have the password twice for some reason, which looks like this:

draw-siren-tulip
 evade-decay-tidy

I changed the commands to all point to the actual path of the binaries, but that didn't seem to make any difference. I know that cron can be a bit weird compared to running things from the normal shell. I also tried to create a cron job that bypassed the script altogether, but that seemed to be even more difficult to do correctly.

Can anyone see what might be causing this issue?

1 answer

  • answered 2021-02-22 22:48 Mischa Friegang

    Thanks to @barmar for pointing me in the right direction. The script needed to point to the full path for test.txt

    Simple solution, thanks everyone!