From 9be068962c8371e95931835b00c5364e876e0c7a Mon Sep 17 00:00:00 2001 From: James Kolpack Date: Wed, 18 Mar 2026 22:13:50 -0400 Subject: [PATCH] Refactor piscal_launcher.sh and piscal_manager.sh for improved quoting and error handling. Ensure proper handling of variables and directory paths to enhance script robustness. --- piscal-manager/piscal_launcher.sh | 9 +++++---- piscal-manager/piscal_manager.sh | 29 ++++++++++++++++++----------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/piscal-manager/piscal_launcher.sh b/piscal-manager/piscal_launcher.sh index b80e6b6..5161a16 100755 --- a/piscal-manager/piscal_launcher.sh +++ b/piscal-manager/piscal_launcher.sh @@ -92,10 +92,11 @@ if [ ! -d "$run_directory" ]; then fi # run piscal -pushd $run_directory >> /dev/null +pushd "$run_directory" >> /dev/null if [ -z "$test_output_directory" ]; then - eval $piscal_executable + # `piscal_executable` comes from a cfg file and may include args. + eval "$piscal_executable" else cp -r "$test_output_directory"/* "$output_directory_base"/ fi @@ -112,10 +113,10 @@ if [ -z "$suppress_storage_copy" ]; then if [ ! -f "$mv_script" ]; then mv_script="/srv/subdir_year.sh" fi - pushd "$storage_directory"/output >> /dev/null + pushd "$storage_directory/output" >> /dev/null "$mv_script" popd >> /dev/null - pushd "$storage_directory"/input >> /dev/null + pushd "$storage_directory/input" >> /dev/null "$mv_script" popd >> /dev/null fi diff --git a/piscal-manager/piscal_manager.sh b/piscal-manager/piscal_manager.sh index 5f10c63..1a9af66 100755 --- a/piscal-manager/piscal_manager.sh +++ b/piscal-manager/piscal_manager.sh @@ -118,9 +118,9 @@ fi ## Process task if [ "$task" = "launch" ]; then # verify process isn't running yet - pid=$(head -n 1 $pid_path 2>/dev/null) + pid="$(head -n 1 "$pid_path" 2>/dev/null)" # if the pid exists, check the process status using ps - if ps -p $pid &>/dev/null; then + if [ -n "$pid" ] && ps -p "$pid" &>/dev/null; then # if it is in ps, then it's still running echo still running exit 1 @@ -143,10 +143,10 @@ if [ "$task" = "launch" ]; then fi # launch it, sending error output to file - nohup ${command} > $working_directory/$stderr_filename 2>&1 & + nohup ${command} > "$working_directory/$stderr_filename" 2>&1 & # write the PID to a temp file to check for completion later - echo $! > $pid_path + echo $! > "$pid_path" echo started elif [ "$task" = "get_status" ] || [ "$task" = "get_status_cleaned_input" ]; then # if the pid doesn't exist, then process hasn't started @@ -155,9 +155,13 @@ elif [ "$task" = "get_status" ] || [ "$task" = "get_status_cleaned_input" ]; the exit fi - pid=$(head -n 1 $pid_path 2>/dev/null) + pid="$(head -n 1 "$pid_path" 2>/dev/null)" + if [ -z "$pid" ]; then + echo "not started" + exit + fi # if the pid exists, check the process status using ps - if ps -p $pid > /dev/null; then + if ps -p "$pid" > /dev/null; then # if it is in ps, then it's still running echo running else @@ -197,9 +201,9 @@ elif [ "$task" = "cleanup" ]; then exit 0 fi - pid=$(head -n 1 $pid_path 2>/dev/null) + pid="$(head -n 1 "$pid_path" 2>/dev/null)" # if the pid exists, check the process status using ps - if ps -p $pid > /dev/null; then + if [ -n "$pid" ] && ps -p "$pid" > /dev/null; then # if it is in ps, then it's still running echo still running exit 1 @@ -214,11 +218,14 @@ elif [ "$task" = "kill" ]; then exit 0 fi - pid=$(head -n 1 $pid_path 2>/dev/null) + pid="$(head -n 1 "$pid_path" 2>/dev/null)" + if [ -z "$pid" ]; then + exit 0 + fi # if the pid exists, check the process status using ps - if ps -p $pid > /dev/null; then + if ps -p "$pid" > /dev/null; then # if it is in ps, then it's still running - kill $pid + kill "$pid" fi echo killed fi