diff --git a/src/scripts/starttask.sh b/src/scripts/starttask.sh index 587c74e0c..4c8712b27 100755 --- a/src/scripts/starttask.sh +++ b/src/scripts/starttask.sh @@ -28,24 +28,35 @@ readonly service_name="cloudron-task-${task_id}" systemctl reset-failed "${service_name}" || true readonly id=$(id -u $SUDO_USER) - readonly ubuntu_version=$(lsb_release -rs) + if [[ "${ubuntu_version}" == "16.04" ]]; then - memory_limit="-p MemoryLimit=400M" + options="-p MemoryLimit=400M --remain-after-exit" else - memory_limit="-p MemoryMax=400M" + options="-p MemoryMax=400M --pipe --wait" + + # 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. + # BindsTo does not work on ubuntu 16, this means that even if box is stopped, the tasks keep running + [[ "$BOX_ENV" == "cloudron" ]] && options="${options} -p BindsTo=box.service" fi -# 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 - -# DEBUG has to be hardcoded because it is not set in the tests -systemd-run --unit "${service_name}" --pipe --wait --nice "${nice}" --uid=${id} --gid=${id} ${binds_to} ${memory_limit} \ - -E HOME=${HOME} -E USER=${SUDO_USER} -E DEBUG=box:* -E BOX_ENV=${BOX_ENV} -E NODE_ENV=production \ +# DEBUG has to be hardcoded because it is not set in the tests. --setenv is required for ubuntu 16 (-E does not work) +systemd-run --unit "${service_name}" --nice "${nice}" --uid=${id} --gid=${id} ${options} \ + --setenv HOME=${HOME} --setenv USER=${SUDO_USER} --setenv DEBUG=box:* --setenv BOX_ENV=${BOX_ENV} --setenv NODE_ENV=production \ "${task_worker}" "${task_id}" "${logfile}" +exit_code=$? +if [[ "${ubuntu_version}" == "16.04" ]]; then + sleep 3 + # we cannot use systemctl is-active because unit is always active until stopped with RemainAfterExit + while [[ "$(systemctl show -p SubState ${service_name})" == *"running"* ]]; do + echo "Waiting for service ${service_name} to finish" + sleep 3 + done + exit_code=$(systemctl show "${service_name}" -p ExecMainStatus | sed 's/ExecMainStatus=//g') + systemctl stop "${service_name}" || true # because of remain-after-exit we have to deactivate the service +fi + +echo "Service ${service_name} finished with exit code ${exit_code}" +exit "${exit_code}" diff --git a/src/scripts/stoptask.sh b/src/scripts/stoptask.sh index c4efecfc8..4fb985b57 100755 --- a/src/scripts/stoptask.sh +++ b/src/scripts/stoptask.sh @@ -23,8 +23,10 @@ if [[ "${task_id}" == "all" ]]; then systemctl list-units --full --no-legend cloudron-task-* # just to show who was running systemctl kill --signal=SIGTERM cloudron-task-* || true systemctl reset-failed cloudron-task-* || true + systemctl stop cloudron-task-* || true # because of remain-after-exit in Ubuntu 16 we have to deactivate the service else readonly service_name="cloudron-task-${task_id}" systemctl kill --signal=SIGTERM "${service_name}" || true + systemctl stop "${service_name}" || true # because of remain-after-exit in Ubuntu 16 we have to deactivate the service fi