From 7eb060a82f61ee0cf47ffbbe092cd45b031df8ec Mon Sep 17 00:00:00 2001 From: ur name Date: Tue, 12 Jan 2021 09:57:02 +0000 Subject: [PATCH] uses sudo instead of su and more helpful on errors --- lgsm/functions/command_install_init.sh | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/lgsm/functions/command_install_init.sh b/lgsm/functions/command_install_init.sh index 0b2464432..2c041aaeb 100755 --- a/lgsm/functions/command_install_init.sh +++ b/lgsm/functions/command_install_init.sh @@ -8,6 +8,14 @@ commandaction="systemd service file" functionselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")" fn_firstcommand_set +fn_print_content_on_error() { + fn_print_fail_nl "sudo exited with non 0 exit code - something went wrong" + fn_print_information_nl "You can place the contents of the service file manually in $1" + fn_print_information_nl "Contents: " + printf "\n$2" + exit 1 +} + fn_install_systemd() { file_c=$'[Unit] Description=LinuxGSM gamename Server @@ -25,19 +33,21 @@ RemainAfterExit=yes [Install] WantedBy=multi-user.target' file_c=$(printf "$file_c" | sed s/'gamename'/$gamename/g | sed "s|path|$path|g" | sed s/'whoami'/$(whoami)/g) + path="/etc/systemd/system/${selfname}-lgsm.service" fn_print_ok_nl "Generated the file contents" - fn_print_information_nl "Enter the password of root:" - su -c "echo \"$file_c\" > /etc/systemd/system/${selfname}-lgsm.service" || return 1 && return 0 + fn_print_information_nl "Enter the password of root: (only if you haven't this terminal in the past few minutes)" + printf "$file_c" | sudo tee $path && return 0 || fn_print_content_on_error $path "$file_c" } fn_install_systemd if [ $? -eq 1 ] then - fn_print_error_nl "su exited with non 0 exit code.. something went wrong"; - fn_script_log_fatal "su exited with non 0 exit code." + fn_script_log_fatal "sudo exited with non 0 exit code." else + echo fn_print_complete_nl "Placed the file in /etc/systemd/system/ as ${selfname}-lgsm.service" fn_print_information_nl "run \`systemctl enable ${selfname}-lgsm.service\` (as root), to enable the game on boot" - fn_script_log_pass "sucessfullly installed the systemd service file" + fn_script_log_pass "successfullly installed the systemd service file" fi core_exit.sh +