Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run gpg from a script run by cron?

Tags:

bash

cron

gnupg

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?

like image 619
Marcin Avatar asked Sep 02 '08 15:09

Marcin


2 Answers

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.

like image 160
Marcin Avatar answered Oct 16 '22 16:10

Marcin


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

like image 30
Enginer Avatar answered Oct 16 '22 15:10

Enginer