Running vault management in crontab

Hello,

I’m using the Vault Management API to make daily exports of my Vault. I captured all the necessary steps in a bash script.
Running the bash script manually on the command line is always successful.

However, when I run the script inside a cron, strange things sometimes are happening with the export

  1. CSV exports a zero byte file
  2. JSON exports a file, but the list of items is empty

Here is the script I put together

#!/bin/bash
source ~/bw/api_keys.sh

BW="/usr/local/bin/bw"
${BW} login --apikey
BW_SESSION=$(${BW} unlock --passwordenv BW_PASSWORD --raw)

${BW} list items --session $BW_SESSION > ~/bw/raw.items
${BW} export --format json --output ~/bw/bw.json --session $BW_SESSION
${BW} export --format csv --output ~/bw/bw.csv --session $BW_SESSION
${BW} lock
${BW} logout

Sometimes it works, sometimes it does not.
Any thoughts?

Regards,
Carl

does it work if you unlock vault first and run a second time?, maybe a timeout?