Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

{start,stop}-mycroft.sh: make POSIX sh compatible and fix shellcheck issues #2686

Merged
merged 2 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 61 additions & 53 deletions start-mycroft.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/bin/sh
forslund marked this conversation as resolved.
Show resolved Hide resolved

# Copyright 2017 Mycroft AI Inc.
#
Expand All @@ -14,15 +14,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

SOURCE="${BASH_SOURCE[0]}"
SOURCE="$0"
PureTryOut marked this conversation as resolved.
Show resolved Hide resolved

script=${0}
script=${script##*/}
cd -P "$( dirname "$SOURCE" )" || exit 1 # Enter scripts folder or fail!
DIR="$( pwd )"
VIRTUALENV_ROOT=${VIRTUALENV_ROOT:-"${DIR}/.venv"}

function help() {
help() {
echo "${script}: Mycroft command/service launcher"
echo "usage: ${script} [COMMAND] [restart] [params]"
echo
Expand Down Expand Up @@ -60,7 +60,7 @@ function help() {
}

_module=""
function name-to-script-path() {
name_to_script_path() {
case ${1} in
"bus") _module="mycroft.messagebus.service" ;;
"skills") _module="mycroft.skills" ;;
Expand All @@ -77,47 +77,47 @@ function name-to-script-path() {
esac
}

function source-venv() {
source_venv() {
# Enter Python virtual environment, unless under Docker
PureTryOut marked this conversation as resolved.
Show resolved Hide resolved
if [ ! -f "/.dockerenv" ] ; then
source "${VIRTUALENV_ROOT}/bin/activate"
. "${VIRTUALENV_ROOT}/bin/activate"
fi
}

first_time=true
function init-once() {
init_once() {
if ($first_time) ; then
echo "Initializing..."
"${DIR}/scripts/prepare-msm.sh"
source-venv
source_venv
first_time=false
fi
}

function launch-process() {
init-once
launch_process() {
init_once

name-to-script-path "${1}"
name_to_script_path "${1}"

# Launch process in foreground
echo "Starting $1"
python3 -m ${_module} "$@"
}

function require-process() {
require_process() {
# Launch process if not found
name-to-script-path "${1}"
name_to_script_path "${1}"
if ! pgrep -f "python3 (.*)-m ${_module}" > /dev/null ; then
# Start required process
launch-background "${1}"
launch_background "${1}"
fi
}

function launch-background() {
init-once
launch_background() {
init_once

# Check if given module is running and start (or restart if running)
name-to-script-path "${1}"
name_to_script_path "${1}"
if pgrep -f "python3 (.*)-m ${_module}" > /dev/null ; then
if ($_force_restart) ; then
echo "Restarting: ${1}"
Expand All @@ -131,7 +131,7 @@ function launch-background() {
fi

# Security warning/reminder for the user
if [[ "${1}" == "bus" ]] ; then
if [ "${1}" = "bus" ] ; then
echo "CAUTION: The Mycroft bus is an open websocket with no built-in security"
echo " measures. You are responsible for protecting the local port"
echo " 8181 with a firewall as appropriate."
Expand All @@ -141,29 +141,29 @@ function launch-background() {
python3 -m ${_module} "$@" >> "/var/log/mycroft/${1}.log" 2>&1 &
}

function launch-all() {
launch_all() {
echo "Starting all mycroft-core services"
launch-background bus
launch-background skills
launch-background audio
launch-background voice
launch-background enclosure
launch_background bus
launch_background skills
launch_background audio
launch_background voice
launch_background enclosure
}

function check-dependencies() {
check_dependencies() {
if [ -f .dev_opts.json ] ; then
auto_update=$( jq -r ".auto_update" < .dev_opts.json 2> /dev/null)
else
auto_update="false"
fi
if [ "$auto_update" == "true" ] ; then
if [ "$auto_update" = "true" ] ; then
# Check github repo for updates (e.g. a new release)
git pull
fi

if [ ! -f .installed ] || ! md5sum -c &> /dev/null < .installed ; then
if [ ! -f .installed ] || ! md5sum -c > /dev/null 2>&1 < .installed ; then
# Critical files have changed, dev_setup.sh should be run again
if [ "$auto_update" == "true" ] ; then
if [ "$auto_update" = "true" ] ; then
echo "Updating dependencies..."
bash dev_setup.sh
else
Expand All @@ -179,83 +179,91 @@ function check-dependencies() {

_opt=$1
_force_restart=false

if [ $# -eq 0 ]; then
help
return
fi

shift
if [[ "${1}" == "restart" ]] || [[ "${_opt}" == "restart" ]] ; then
if [ "${1}" = "restart" ] || [ "${_opt}" = "restart" ] ; then
_force_restart=true
if [[ "${_opt}" == "restart" ]] ; then
if [ "${_opt}" = "restart" ] ; then
# Support "start-mycroft.sh restart all" as well as "start-mycroft.sh all restart"
_opt=$1
fi
shift
fi

if [ $# -gt 0 ]; then
shift
fi
fi

if [[ ! "${_opt}" == "cli" ]] ; then
check-dependencies
if [ ! "${_opt}" = "cli" ] ; then
check_dependencies
fi

case ${_opt} in
"all")
launch-all
launch_all
;;

"bus")
launch-background "${_opt}"
launch_background "${_opt}"
;;
"audio")
launch-background "${_opt}"
launch_background "${_opt}"
;;
"skills")
launch-background "${_opt}"
launch_background "${_opt}"
;;
"voice")
launch-background "${_opt}"
launch_background "${_opt}"
;;

"debug")
launch-all
launch-process cli
launch_all
launch_process cli
;;

"cli")
require-process bus
require-process skills
launch-process "${_opt}"
require_process bus
require_process skills
launch_process "${_opt}"
;;

# TODO: Restore support for Wifi Setup on a Picroft, etc.
# "wifi")
# launch-background ${_opt}
# launch_background ${_opt}
# ;;
"unittest")
source-venv
source_venv
pytest test/unittests/ --cov=mycroft "$@"
;;
"singleunittest")
source-venv
source_venv
pytest "$@"
;;
"skillstest")
source-venv
source_venv
pytest test/integrationtests/skills/discover_tests.py "$@"
;;
"vktest")
source "$DIR/bin/mycroft-skill-testrunner" vktest "$@"
"$DIR/bin/mycroft-skill-testrunner" vktest "$@"
;;
"audiotest")
launch-process "${_opt}"
launch_process "${_opt}"
;;
"wakewordtest")
launch-process "${_opt}"
launch_process "${_opt}"
;;
"sdkdoc")
source-venv
source_venv
cd doc || exit 1 # Exit if doc directory doesn't exist
make "$@"
cd ..
;;
"enclosure")
launch-background "${_opt}"
launch_background "${_opt}"
;;

*)
Expand Down
55 changes: 28 additions & 27 deletions stop-mycroft.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/bin/sh
PureTryOut marked this conversation as resolved.
Show resolved Hide resolved

# Copyright 2017 Mycroft AI Inc.
#
Expand All @@ -14,13 +14,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

SOURCE="${BASH_SOURCE[0]}"
# This script is never sourced but always directly executed, so this is safe to do
SOURCE="$0"

script=${0}
script=${script##*/}
cd -P "$( dirname "$SOURCE" )" || exit 1 # quit if change of folder fails

function help() {
help() {
echo "${script}: Mycroft service stopper"
echo "usage: ${script} [service]"
echo
Expand All @@ -40,36 +41,36 @@ function help() {
exit 0
}

function process-running() {
if [[ $( pgrep -f "python3 (.*)-m mycroft.*${1}" ) ]] ; then
process_running() {
if [ "$( pgrep -f "python3 (.*)-m mycroft.*${1}" )" ] ; then
return 0
else
return 1
fi
}

function end-process() {
if process-running "$1" ; then
end_process() {
if process_running "$1" ; then
# Find the process by name, only returning the oldest if it has children
pid=$( pgrep -o -f "python3 (.*)-m mycroft.*${1}" )
echo -n "Stopping $1 (${pid})..."
kill -SIGINT "${pid}"
printf "Stopping %s (%s)..." "$1" "${pid}"
kill -s INT "${pid}"

# Wait up to 5 seconds (50 * 0.1) for process to stop
c=1
while [ $c -le 50 ] ; do
if process-running "$1" ; then
if process_running "$1" ; then
sleep 0.1
(( c++ ))
c=$((c + 1))
else
c=999 # end loop
fi
done

if process-running "$1" ; then
if process_running "$1" ; then
echo "failed to stop."
pid=$( pgrep -o -f "python3 (.*)-m mycroft.*${1}" )
echo -n " Killing $1 (${pid})..."
printf " Killing %s (%s)...\n" "$1" "${pid}"
kill -9 "${pid}"
echo "killed."
result=120
Expand All @@ -87,33 +88,33 @@ result=0 # default, no change


OPT=$1
shift
if [ $# -gt 0 ]; then
shift
fi

case ${OPT} in
"all")
;&
"")
""|"all")
echo "Stopping all mycroft-core services"
end-process skills
end-process audio
end-process speech
end-process enclosure
end-process messagebus.service
end_process skills
end_process audio
end_process speech
end_process enclosure
end_process messagebus.service
;;
"bus")
end-process messagebus.service
end_process messagebus.service
;;
"audio")
end-process audio
end_process audio
;;
"skills")
end-process skills
end_process skills
;;
"voice")
end-process speech
end_process speech
;;
"enclosure")
end-process enclosure
end_process enclosure
;;

*)
Expand Down