I am using a shared SLURM cluster. I am trying to get the path of the bash script from inside the script itself.

There is already an excellent thread here: https://stackoverflow.com/questions/59895/get-the-source-directory-of-a-bash-script-from-within-the-script-itself. Unfortunately, none of those solutions work for me. They work when I execute the bash script normally, but not when I submit the script as a job. I tried $BASH_SOURCE, $0, $_, and readlink /proc/$$/fd/255. They all return something like /cm/local/apps/slurm/var/spool/jobXXXXXX/slurm_script, which is a path auto-generated by SLURM and has nothing to do with the script I submitted. I doubt there are any other solutions that directly answer my question that are not already mentioned, but is there maybe some way to override whatever the cluster is doing? Can I somehow restore standard bash behavior?

  • 1
    It seems to me like you have recovered the name of the script: /cm/local/apps/slurm/var/spool/jobXXXXXX/slurm_script. What were you expecting instead, and why does it have to be different? – Jeff Schaller Feb 5 '19 at 16:30
  • 1
    Why do you think that is not the path of the script that's executing? – Jeff Schaller Feb 5 '19 at 16:33
  • 1
    I don't use slurm, but it's conceivable to me that the system would copy the source script to a worker node in some sort of spooling directory for that node to execute it. Sounds like you might have different expectations? – Jeff Schaller Feb 5 '19 at 16:37
  • 2
    SLURM copies your script. On a SLURM cluster, you may not even have a home directory, so the working directory may well be at another path than what you would see when you run your script locally. – Kusalananda Feb 5 '19 at 16:38
  • 1
    I've gone in a circle now, so I may have to give up, but -- you start off knowing the path of your script and then you find the new location when run by slurm, but now you want to know the original location? – Jeff Schaller Feb 5 '19 at 16:47

The problem was resolved in the comments.

To summarize: It turns out that I did not properly diagnose the initial problem. SLURM did not modify $BASH_SOURCE or $0. I assumed it simply executed my script, but it actually copied my script to a new location (/cm/local/apps/slurm/var/spool/jobXXXXXX/slurm_script). To get the behavior I expected, I submitted the job with the --wrap parameter which wraps the specified command string in a shell script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.