I have a script that has a part that looks like that:
for file in `ls *.tar.gz`; do echo encrypting $file gpg --passphrase-file /home/$USER/.gnupg/backup-passphrase \ --simple-sk-checksum -c $file done
For some reason if I run this script manually, works perfectly fine and all files are encrypted. If I run this as cron job, echo $file
works fine (I see "encrypting <file>" in the log), but the file doesn't get encrypted and gpg silent fails with no stdout/stderr output.
Any clues?
It turns out that the answer was easier than I expected. There is a --batch
parameter missing, gpg tries to read from /dev/tty that doesn't exist for cron jobs. To debug that I have used --exit-on-status-write-error
param. But to use that I was inspired by exit status 2, reported by echoing $?
as Cd-Man suggested.
In my case gpg cant find home dir for using keys:
gpg: no default secret key: No secret key
gpg: 0003608.cmd: sign+encrypt failed: No secret key
So I added --homedir /root/.gnupg
. The final command can looks like
echo 'password' | gpg -vvv --homedir /root/.gnupg --batch --passphrase-fd 0 --output /usr/share/file.gpg --encrypt --sign /usr/share/file.tar.bz2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With