thmo
thmo

Reputation: 199

Get the full path of an executed file from a slurm job

I'm launching the following slurm job by launching sbatch path/to/file/filename.sh arg1 arg2. I would like to save the absolute path of path/to/file/ into a bash variable and re use afterwards in the bash script.

#!/bin/bash
#SBATCH -J ...
#SBATCH --gres=gpu:1
#SBATCH --partition=...
#SBATCH --nodes=1
#SBATCH -t 7-0
#SBATCH --output=out%j.out             
#SBATCH --error=out%j.err               

try() {
    SCRIPT_FILE=$(scontrol show job $SLURM_JOB_ID | grep Command= | cut -d'=' -f2)
    SCRIPT_DIR=$(cd "$(dirname "$SCRIPT_FILE")" && pwd)
}

catch() {
    echo "Failed to get script file path. Using fallback method."
    SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)"
}

try
if [ "${SCRIPT_DIR: -3}" != ".sh" ]; then
    catch
fi

I'm having an error saying that scontrol : command not found when executing the file but on the server scontrol works fine. I'm not sure what is happening here.

I have also tried to use : How to get original location of script used for SLURM job? but without success.

Upvotes: 0

Views: 18

Answers (0)

Related Questions