Issue
I have a cron job like this
*/5 * * * * /vault/configure_snapshots.sh > /var/log/cron/snapshots
I created a script and uploaded it during initialization (e.g. AWS EC2 user-data). Afterwards I realized that I had to do some changes for testing. I added `set -x:
set -x
server_ip=$(curl http://169.254.169.254/latest/meta-data/local-ipv4)
source /etc/environment
export VAULT_TOKEN=$(aws secretsmanager get-secret-value --secret-id $VAULT_ROOT_TOKEN_AWS_SECRET_ID | jq -r '.SecretString' | jq -r '.root_token')
# Check if the server is the leader in the cluster
/usr/local/bin/vault operator raft list-peers -format=json > /vault/peers.json
leader_ip=$(jq -r '.data.config.servers[] | select(.leader == true).address' /vault/peers.json | cut -d ':' -f 1)
if [[ $server_ip == $leader_ip ]]; then
echo "Taking snapshot..."
# ... trimmed
else
echo "Not the leader. Skipping snapshot."
fi
The result is:
root@ip-100-73-25-168:/var/snap/amazon-ssm-agent/4046# cat /var/log/cron/snapshots
Not the leader. Skipping snapshot.
Why is not everything being printed althouth I set -x?
Solution
Replace
> /var/log/cron/snapshots
with
> /var/log/cron/snapshots 2>&1
to get stderr, too.
Answered By - Cyrus