0

On startup I manually run source /usr/local/etc/credentials.sh.

I've made a script /usr/local/etc/rc.d/load_credentials to run command for me at start-up. However the script does not succeed:

$ /usr/local/etc/rc.d/load_credentials start
eval: source: not found

load_credentials

The script is based on the example in rc-scripting docs.

#!/bin/sh 

. /etc/rc.subr 

name="load_credentials" 
start_cmd="${name}_start" 
stop_cmd=":" 

load_credentials_start() 
{
    source /usr/local/etc/psql_creds.sh
}

load_rc_config $name 
run_rc_command "$1"

credentials.sh

export DB_USER="JimmyJohn"
export DB_PASS="password123"

EDITs

Moved files as advised by @Michael-O

DannyDannyDanny
  • 838
  • 9
  • 26
  • /etc/rc.d is for the base system only. Don't touch it. Move your file to /usr/local/etc/rc.d and your cred file to /usr/local/etc/{name}.conf or similar – Michael-O Sep 30 '21 at 11:42
  • `source` is a built-in of `bash`. The RC scripts run under `/bin/sh`. Use `.` instead of `source`. – Richard Smith Sep 30 '21 at 13:22
  • @RichardSmith Thank you! The script no longer crashes. However, there's simply no output and the variables inside of credential.sh have not been sourced. For example: `echo $DB_USER` returns nothing. – DannyDannyDanny Sep 30 '21 at 13:35
  • 2
    Are you expecting these credentials to be available to your login shell? Look at the manual page for your preferred shell for its list of startup files. For example, for `bash` you can use `~/.bash_profile` – Richard Smith Sep 30 '21 at 13:59
  • @RichardSmith will the credentials be available for crontasks running from my user? – DannyDannyDanny Oct 04 '21 at 08:44
  • @DannyDannyDanny probably not. Your cron tasks do not run under your login shell. They are run by the cron daemon. – Richard Smith Oct 04 '21 at 08:50

1 Answers1

0

Based on your comment

will the credentials be available for crontasks running from my user?

it would seem you wish this to work in crontasks.

Here is a sample cronjob that I tested working and it does indeed work in crontasks.

Make a file in /usr/local/bin/crondo.sh

# chmod +x /usr/local/bin/crondo.sh
# cat /usr/local/bin/crondo.sh
#!/bin/sh
. /usr/local/etc/psql_creds.sh
echo $DB_USER

Add this line to crontab with crontab -e

# Run 22 minutes after every hour.
22      *       *       *       *       /usr/local/bin/crondo.sh > /tmp/out
James Risner
  • 5,451
  • 11
  • 25
  • 47