gw1500se has asked for the wisdom of the Perl Monks concerning the following question:

I am having a problem with GnuPG::Interface and cannot seem to find any specific resource for it (there seems to be a defunct or inactive list on perl.org but that is all). If this is not the appropriate place my apologies and if someone has a better forum please let me know. I have installed GnuPG and it seems to be working fine. I then installed GnuPG::Interface in perl and wrote a script that tries to decrypt a file. Everything seems to be working fine and the file gets decrypted. My problem occurs when I try to run the script in background (cron or nohup). I get an error pointing to the line that calls the 'decrypt' method. It says "fh is not defined." I don't have a variable by that name so I don't have a clue what it is referring to other then it must be in the decrypt method somewhere. I tried setting $gnupg->options->batch(1); but that did not help. Can someone help me figure out what is wrong? Thanks.

Replies are listed 'Best First'.
Re: GnuPG::Interface Problem
by andyford (Curate) on Sep 19, 2006 at 19:01 UTC
    It's usually best to post a test case.

    Perhaps the module defaults to writing to a tty that you're not getting in the cron environment.

    Are you doing this type of thing? I just lifted this from the module docs.

    # how we create some handles to interact with GnuPG my $input = IO::Handle->new(); my $output = IO::Handle->new(); my $handles = GnuPG::Handles->new( stdin => $input, stdout => $output );

    andyford
    or non-Perl: Andy Ford

Re: GnuPG::Interface Problem
by mattr (Curate) on Sep 21, 2006 at 04:40 UTC
    google: gpg cron google: gpg cron tty

    Looks like this error, not uncommon. As above poster mentioned. Also maybe path in cron environment is different too (path to gpg, or to keys/files may be different)... Many people are posting about problems running gpg programs under crontab not due to Perl.

    Try adding --no-tty as well as --batch

    gpg: cannot open `/dev/tty': No such device or address

    You would think the batch(1) would fix it but maybe not.. perhaps you can insert some print statements in the module, run under cron and check the messages emailed to you.

    > I was getting the same problem and I included --batch in my commands and it
    > worked for me.
    >
    > cat /root/.gnupg/pass | $GPG --batch --passphrase-fd 0 --quiet \
    > --output $SIGNED --clearsign $REPORT

    Apparently the cron environment also may have a different locale which can mess up the signature but that is not your problem. The fh is short for filehandle and refers to the pipe the module opens to the gpg program.

    Another post notes:



    You're running gpg as a batch job and from cron, ie no tty available. GPG can in some occasions still print warnings to the tty even though --batch is used as an option.

    > And even when "--no-tty" and "--batch" are given on the > commandline, GPG may ask questions, if I remember correctly. No, there shall be no interactive input with --batch. If you noticed such a case, it is a bug and should be reported.

    And even when "--no-tty" and "--batch" are given on the commandline, GPG may ask questions, if I remember correctly. For encrpytion it should work anyway, without questions.


    Incidentally one thread recommends making a wrapper (your perl program I expect, if you are running with perl as interpreter not bash) to set up the necessary environment instead of changing the username under which the cron command runs with vixie-cron or su.

    Finally it is possible a message is being generated as noted is possible above, if it finds an untrusted key and wants to ask you if it is okay to use it. Here is how that was solved:
    gpg: WARNING: --honor-http-proxy is a deprecated option. gpg: please use "--keyserver-options honor-http-proxy" instead gpg: D61B227D: There is no indication that this key really belongs to +the owner gpg: cannot open /dev/tty: No such device or address the first one can be fixed by removing/commenting out the #honor-http-proxy option in the ~/.gnupg/options file the /dev/tty problem can be fixed by appending --always-trust to the command line, because gpg tries to ask for a passphrase or something to approve the use of an untusted key.