[node.sh] run node.sh as non-root user is possible

Signed-off-by: Leo Chen <leo@harmony.one>
pull/1115/head
Leo Chen 6 years ago
parent 27b273aaa1
commit 0b825aec20
  1. 50
      scripts/node.sh

@ -49,6 +49,15 @@ function myip() {
fi fi
} }
function check_root
{
if [[ $EUID -ne 0 ]]; then
msg "this script must be run as root to setup environment"
msg please use \"sudo ${progname}\"
exit 1
fi
}
function add_env function add_env
{ {
filename=$1 filename=$1
@ -58,6 +67,8 @@ function add_env
function setup_env function setup_env
{ {
check_root
# setup environment variables, may not be nessary # setup environment variables, may not be nessary
sysctl -w net.core.somaxconn=1024 sysctl -w net.core.somaxconn=1024
sysctl -w net.core.netdev_max_backlog=65536 sysctl -w net.core.netdev_max_backlog=65536
@ -78,21 +89,19 @@ function setup_env
} }
######## main ######### ######## main #########
if [[ $EUID -ne 0 ]]; then
msg "this script must be run as root"
msg please use \"sudo $0\"
exit 1
fi
print_usage() { print_usage() {
cat <<- ENDEND cat <<- ENDEND
usage: ${progname} [-1ch] [-k KEYFILE]
-c back up database/logs and start clean usage: ${progname} [-1ch] [-k KEYFILE]
(use only when directed by Harmony) -c back up database/logs and start clean
-1 do not loop; run once and exit (use only when directed by Harmony)
-h print this help and exit -1 do not loop; run once and exit
-k KEYFILE use the given BLS key file (default: autodetect) -h print this help and exit
ENDEND -k KEYFILE use the given BLS key file (default: autodetect)
-s run setup env only (must run as root)
-S run the ${progname} as non-root user (default: run as root)
ENDEND
} }
usage() { usage() {
@ -101,14 +110,15 @@ usage() {
exit 64 # EX_USAGE exit 64 # EX_USAGE
} }
unset start_clean loop unset start_clean loop run_as_root
start_clean=false start_clean=false
loop=true loop=true
run_as_root=true
${BLSKEYFILE=} ${BLSKEYFILE=}
unset OPTIND OPTARG opt unset OPTIND OPTARG opt
OPTIND=1 OPTIND=1
while getopts :1chk: opt while getopts :1chk:sS opt
do do
case "${opt}" in case "${opt}" in
'?') usage "unrecognized option -${OPTARG}";; '?') usage "unrecognized option -${OPTARG}";;
@ -117,6 +127,8 @@ do
1) loop=false;; 1) loop=false;;
h) print_usage; exit 0;; h) print_usage; exit 0;;
k) BLSKEYFILE="${OPTARG}";; k) BLSKEYFILE="${OPTARG}";;
s) setup_env; exit 0;;
S) run_as_root=false ;;
*) err 70 "unhandled option -${OPTARG}";; # EX_SOFTWARE *) err 70 "unhandled option -${OPTARG}";; # EX_SOFTWARE
esac esac
done done
@ -128,6 +140,10 @@ case $# in
;; ;;
esac esac
if ${run_as_root}; then
check_root
fi
case "${BLSKEYFILE}" in case "${BLSKEYFILE}" in
"") "")
unset -v f unset -v f
@ -193,7 +209,9 @@ NODE_PORT=9000
PUB_IP= PUB_IP=
if [ "$OS" == "Linux" ]; then if [ "$OS" == "Linux" ]; then
setup_env if ${run_as_root}; then
setup_env
fi
# Kill existing soldier/node # Kill existing soldier/node
fuser -k -n tcp $NODE_PORT fuser -k -n tcp $NODE_PORT
fi fi

Loading…
Cancel
Save