Saturday, October 8, 2022

[SOLVED] FreeBSd rc.d source another file

Issue

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


Solution

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


Answered By - James Risner
Answer Checked By - Candace Johnson (WPSolving Volunteer)