Files
cloudron-box/src/scripts/starttask.sh

44 lines
1.1 KiB
Bash
Raw Normal View History

#!/bin/bash
set -eu -o pipefail
readonly script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly task_worker="${script_dir}/../taskworker.js"
if [[ ${EUID} -ne 0 ]]; then
echo "This script should be run as root." > /dev/stderr
exit 1
fi
if [[ $# -eq 0 ]]; then
echo "No arguments supplied"
exit 1
fi
if [[ "$1" == "--check" ]]; then
echo "OK"
exit 0
fi
readonly task_id="$1"
readonly logfile="$2"
readonly nice="$3"
readonly service_name="cloudron-task-${task_id}"
systemctl reset-failed "${service_name}" || true
readonly id=$(id -u $SUDO_USER)
# Note: BindsTo will kill this task when the box is stopped. but will not kill this task when restarted!
# For this reason, we have code to kill the tasks both on shutdown and startup
if [[ "$BOX_ENV" == "cloudron" ]]; then
binds_to="-p BindsTo=box.service"
else
binds_to=""
fi
systemd-run --unit "${service_name}" --pipe --wait --property=OOMScoreAdjust=-1000 --nice "${nice}" --uid=${id} --gid=${id} \
${binds_to} -E HOME=${HOME} -E USER=${SUDO_USER} -E DEBUG=${DEBUG} -E BOX_ENV=${BOX_ENV} -E NODE_ENV=production \
"${task_worker}" "${task_id}" "${logfile}"