<p>I have a script that has a part that looks like that:</p>

<pre><code>for file in `ls *.tar.gz`; do
  echo encrypting $file
  gpg --passphrase-file /home/$USER/.gnupg/backup-passphrase \
    --simple-sk-checksum -c  $file
done
</code></pre>

<p>For some reason if I run this script manually, works perfectly fine and all files are encrypted. If I run this as cron job, <code>echo $file</code> works fine (I see "encrypting &lt;file&gt;" in the log), but the file doesn't get encrypted and gpg silent fails with no stdout/stderr output.</p>

<p>Any clues?</p>

[{'answer_id': 39877, 'author': 'Nick Berardi', 'author_id': 17, 'author_profile': 'https://Stackoverflow.com/users/17', 'pm_score': 0, 'selected': False, 'text': '<p>make sure the user that is running the cron job has the permissions needed to encrypt the file.  </p>\n'}, {'answer_id': 39898, 'author': 'skinp', 'author_id': 2907, 'author_profile': 'https://Stackoverflow.com/users/2907', 'pm_score': 0, 'selected': False, 'text': "<p>I've came across this problem once.</p>\n\n<p>I can't really tell you why, but I dont think cron executes with the same environment variable as the user do.</p>\n\n<p>I actually had to export the good path for my programs to execute well.\nIs gpg at least trying to execute?</p>\n\n<p>Or are the files you are trying to encypt actually in the current directory when the cron executes?</p>\n\n<p>Maybe try to execute a <code>echo whereis gpg</code> and <code>echo $PATH</code> in your script to see if it's included... Worked for me.</p>\n"}, {'answer_id': 39917, 'author': 'Grey Panther', 'author_id': 1265, 'author_profile': 'https://Stackoverflow.com/users/1265', 'pm_score': 1, 'selected': False, 'text': '<p>You should make sure that GPG is in your path when the cronjob is running. Your best guess would be do get the full path of GPG (by doing <code>which gpg</code>) and running it using the full path (for example <code>/usr/bin/gpp...</code>).</p>\n\n<p>Some other debugging tips:</p>\n\n<ul>\n<li>output the value of <code>$?</code> after running GPG (like this: echo "$?"). This gives you the exit code, which should be 0, if it succeded</li>\n<li>redirect the STDERR to STDOUT for GPG and then redirect STDOUT to a file, to inspect any error messages which might get printed (you can do this a command line: <code>/usr/bin/gpg ...  2&gt;&amp;1 &gt;&gt; gpg.log</code>)</li>\n</ul>\n'}, {'answer_id': 40141, 'author': 'Marcin', 'author_id': 3105, 'author_profile': 'https://Stackoverflow.com/users/3105', 'pm_score': 6, 'selected': True, 'text': "<p>It turns out that the answer was easier than I expected. There is a <code>--batch</code> parameter missing, gpg tries to read from /dev/tty that doesn't exist for cron jobs. To debug that I have used <code>--exit-on-status-write-error</code> param. But to use that I was inspired by exit status 2, reported by echoing <code>$?</code> as Cd-Man suggested. </p>\n"}, {'answer_id': 42790, 'author': 'dr-jan', 'author_id': 2599, 'author_profile': 'https://Stackoverflow.com/users/2599', 'pm_score': 0, 'selected': False, 'text': "<p>@skinp Cron jobs are executed by sh, whereas most modern Unixes use bash or ksh for interactive logins. The biggest problem (in my experience) is that sh doesn't understand things like:</p>\n\n<pre><code>export PS1='\\u@\\h:\\w&gt; '\n</code></pre>\n\n<p>which needs to be changed to:</p>\n\n<pre><code>PS1='\\u@\\h:\\w&gt; '\nexport PS1\n</code></pre>\n\n<p>So if cron runs a shell script which defines an environment variable using the first syntax, before running some other command, the other command will never be executed because sh bombs out trying to define the variable.</p>\n"}, {'answer_id': 7624924, 'author': 'Enginer', 'author_id': 975169, 'author_profile': 'https://Stackoverflow.com/users/975169', 'pm_score': 3, 'selected': False, 'text': "<p>In my case gpg cant find home dir for using keys:</p>\n<blockquote>\n<p>gpg: no default secret key: No secret key</p>\n<p>gpg: 0003608.cmd: sign+encrypt failed: No secret key</p>\n</blockquote>\n<p>So I added <code>--homedir /root/.gnupg</code>. The final command can looks like</p>\n<blockquote>\n<p>echo 'password' | gpg -vvv --homedir /root/.gnupg --batch --passphrase-fd 0\n--output /usr/share/file.gpg --encrypt --sign /usr/share/file.tar.bz2</p>\n</blockquote>\n"}, {'answer_id': 47820507, 'author': 'Mike Lapinskas', 'author_id': 4267138, 'author_profile': 'https://Stackoverflow.com/users/4267138', 'pm_score': 0, 'selected': False, 'text': '<p>In my case: "gpg: decryption failed: Bad session key".</p>\n\n<p>Tried adding /usr/bin/gpg, checking the version, setting --batch, setting --home (with /root/.gnupg and /home/user/.gnupg) and all did not work.</p>\n\n<pre><code>/usr/bin/gpg -d --batch --homedir /home/ec2-user/.gnupg --no-mdc-warning -quiet --passphrase "$GPG_PP" "$file"\n</code></pre>\n\n<p>Turned out that cron on AWS beanstalk instance needed the environment variable being used to set the --passphrase $GPG_PP.  Cron now:</p>\n\n<pre><code>0 15 * * * $(source /opt/elasticbeanstalk/support/envvars &amp;&amp; /home/ec2-user/bin/script.sh &gt;&gt; /home/ec2-user/logs/cron_out.log 2&gt;&amp;1)\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/39867', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3105/']