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

Commit

Permalink
{start,stop}-mycroft.sh: port to POSIX sh
Browse files Browse the repository at this point in the history
This makes the start and stop scripts compatible with POSIX shells.

Overview of the changes:
- "function" statements removed, not necessary and incompatible
- dashes in function and variable names for lower ones (- to _)
- source statements changed for .
- double square brackets replaced for single ones
- double equal statements replaced for single ones
- &> (piping stdout and stderr to the same file) replaced for 2>&1 >
- sourcing of mycroft-skill-testrunner replaced with direct execution
with Bash
- replaced BASH_SOURCE with $0, these scripts are never sourced anyway
- replaced "echo -n" statements with "printf"
- merged the "" and "all" cases to a single one
  • Loading branch information
PureTryOut committed Nov 4, 2021
1 parent 3974b2f commit 97e52d3
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 80 deletions.
117 changes: 64 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

# 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"

script=${0}
script=${script##*/}
cd -P "$( dirname "$SOURCE" )" || exit
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,48 +77,50 @@ function name-to-script-path() {
esac
}

function source-venv() {
# Enter Python virtual environment, unless under Docker
if [ ! -f "/.dockerenv" ] ; then
source_venv() {
# Enter Python virtual environment, unless under Docker or when a virtualenv is already active
virtualenv=$(python3 -c 'import sys; sys.stdout.write("1") if (hasattr(sys, "real_prefix") or (hasattr(sys, "base_prefix") and sys.base_prefix != sys.prefix)) else sys.stdout.write("0")')

if [ ! -f "/.dockerenv" ] && [ "$virtualenv" -eq 0 ] ; then
# shellcheck source=/dev/null
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} "$_params"
}

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 @@ -132,7 +134,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 @@ -142,29 +144,29 @@ function launch-background() {
python3 -m ${_module} "$_params" >> "/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 2>&1 > /dev/null < .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 @@ -180,83 +182,92 @@ 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

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

if [[ ! "${_opt}" == "cli" ]] ; then
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
make "${_params}"
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

# 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

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

0 comments on commit 97e52d3

Please sign in to comment.