sfdx force:auth:web:login - reuse session on cron (MacOS)?

180 views Asked by At

Here is a simple script that detect if I am authenticated

#!/bin/bash

if [[ $(sfdx auth:list | grep -c "my_org.my.salesforce.com") -eq 0 ]]; then
    echo "sfdx not authenticated, please run : sfdx force:auth:web:login -s -a orgname"
    # echo "DEBUG : config:list [$(sfdx config:list)]" #same output as interactive mode
    # echo "DEBUG : auth:list [$(sfdx auth:list)]" #no value returned via cron
    exit 5
fi

sfdx is authenticated for my user in "$HOME"/.sfdc/[email protected] so my script proceed when run manually

When I run the bash file via cron (crontab -e with 03 * * * * /path/my_script.sh), the auth:list has no entry even though sfdx config:list does have the configured entry

I tried to source my ~/.profile and ~/.bash_profile files to no avail.

Is there a way to specify to use the same credential file used in my interactive terminal for sfdx ran from sfdx ?

1

There are 1 answers

3
Cezu On BEST ANSWER

Maybe you can use the following commands:

$ sfdx force:org:display -u <OrgUsername> --verbose --json >
authFile.json 

$ sfdx auth:sfdxurl:store -f authFile.json

The first command generates a forceurl in the JSON file and the second command uses the file generated by the first to login.