3

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?

burger
  • 209
  • 1
  • 6
  • 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 05 '19 at 16:30
  • No. That is some location used by SLURM. I am going to clarify the question. – burger Feb 05 '19 at 16:32
  • 1
    Why do you think that is *not* the path of the script that's executing? – Jeff Schaller Feb 05 '19 at 16:33
  • Because I know the exact script I am executing. – burger Feb 05 '19 at 16:34
  • 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 05 '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 05 '19 at 16:38
  • How do you submit the job, exactly? – Jeff Schaller Feb 05 '19 at 16:40
  • sbatch ... myscript.sh – burger Feb 05 '19 at 16:42
  • 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](https://unix.stackexchange.com/questions/498844/restore-0-or-bash-source-after-it-is-modified-by-the-cluster?noredirect=1#comment918412_498844) and then you find the new location when run by slurm, but now you want to know the original location? – Jeff Schaller Feb 05 '19 at 16:47
  • Yes, you're right. I did not expect SLURM to actually copy and rename my script, but that may be indeed what is happening. If I use `--wrap`, it is executed in place. – burger Feb 05 '19 at 16:48
  • 1
    What do you need `$BASH_SOURCE` for in your script? ... and what does not currently work as expected? – Kusalananda Feb 05 '19 at 20:03

1 Answers1

1

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.

burger
  • 209
  • 1
  • 6