There are three options I use when I have a bash script that I want to behave differently when it is sourced vs. when it is executed (or in other words have data items in a script that I want access to without executing any code at that time). The comments touched on them to an extent.
Option One
Determine when being sourced and end 'sourcing' at appropriate time
Separate script into two sections, exit from script when sourced before getting to lower second
Create an upper section of script with definitions (functions / variable assignments / etc), but no direct code execution.
Just before the executable code section starts, place logic that will exit the script if it detects it is being sourced.
The following code segment will do this:
file1.sh
#!/usr/bin/env bash
export var1="/data/share"
export var2='password'
# --- End Definitions Section ---
# check if we are being sourced by another script or shell
[[ "${#BASH_SOURCE[@]}" -gt "1" ]] && { return 0; }
# --- Begin Code Execution Section ---
echo "Hello"
echo $var1
echo $var2
file2.sh
#!/usr/bin/env bash
source file1.sh
echo "$var1"
echo "$var2"
Output of running ./file2.sh
$ ./file2.sh
/data/share
password
Option Two
This one is usually only used in complex situations, and for this particular example it's overkill. I create a function in the file that I want to source, and in that function determine what should be available to the caller. In this case it's the two exported variables. Normally I use this mode when I have associative arrays, which are otherwise nearly impossible to hand around. Also, the tmp file should be deleted by the caller; but I didn't in this case:
file1.sh
#!/usr/bin/env bash
export var1="/data/share"
export var2='password'
exportCfg() {
tmpF=$(mktemp)
declare -p var1 var2 > "$tmpF"
echo "$tmpF"
}
if [ "$1" == "export" ]; then
exportCfg;
exit 0;
fi
echo "Hello"
echo $var1
echo $var2
file2.sh
#!/usr/bin/env bash
source $(./file1.sh export)
echo "$var1"
echo "$var2"
The output from executing file2.sh is the same as above
Option 3
The final common way I handle this is simply with a library file that only holds definitions, and has no code that will execute when sourced or directly run.
This is simply a matter of dividing up your code. I have a group of bash 'libs' that contain frequently used functions, and on a per project basis usually setup a small sourceable library for storing configuration data (constants). If that data includes populated arrays, then I will also use a version of Option 2.
Best Answer
You must use an absolute path with
sudo
, for security reasons:Check the output of
sudo -l
to confirm. From thesudoers
man page (1.7.x):sudo xargs
works becausexargs
is (almost certainly) found in a trusted path (/usr/bin
).Also, check out
pgrep
andpkill
, it will save you the needlessps
pipe acrobatics.You have the potential for resource leaks and other unwanted behaviour with an unconditional
kill -9
, see https://unix.stackexchange.com/questions/8916/why-not-kill-9-a-process .Update you've added that you run this via root's crontab -- root has no need to use
sudo
, and in some cases root may be prevented from runningsudo
, check whatsudo -l
says when you are root. If you want to to be able to start a program (that doesn't switch its own uid) as a specific userid then the common way issu - username -c "command"
.