pythoncluster-computingslurmcdpwd

How to change the working directory on Slurm


I am working on a slurm cluster where I am running couple of jobs. It is hard for me to check the jobs one by one in each directory.

I could manage to check in which directory the jobs are running using

scontrol show job JOB_ID

This command gives me various lines on the output. Few of them are listed below

   OverSubscribe=OK Contiguous=0 Licenses=(null) Network=(null)
   Command=/home/astha/vt-st/scf-test/303030/49/qsub.job
   WorkDir=/home/astha/vt-st/scf-test/303030/49
   StdErr=/home/astha/vt-st/scf-test/303030/49/qsub.job.e1205
   StdIn=/dev/null
   StdOut=/home/astha/vt-st/scf-test/303030/49/qsub.job.o1205
   Power=
   MailUser=(null) MailType=NONE

Where WorkDir (this is an example, the path will be different for each job) from above output is the directory in which I want to switch.

then

cd /home/astha/vt-st/scf-test/303030/49

But typing this long commands make my fingers cry.

I have tried to make a small python script to print scontrol show job

# Try block
try:
    # Take a number
        print("scontrol show job")


# Exception block
except (ValueError):
  # Print error message
  print("Enter a numeric value")

But then how I should improve it so that it takes my given input number and then grep the WorkDir from the output and change the directory.


Solution

  • You will not be able to have a python script change your current working directory easily, and can do it simply in Bash like this:

    $ cdjob() { cd $(squeue -h -o%Z -j "$1") ; }
    

    This will create a Bash function named cdjob that accept a job ID as parameter. You can check it was created correctly with

    $ type cdjob
    cdjob is a function
    cdjob ()
    {
        cd $(squeue -h -o%Z -j "$1")
    }
    

    After you run the above command (which you can place in your startup script .bashrc or .bash_profile if you want it to survive logouts) you will be able to do

    $ cdjob 22078365
    

    and this will bring you to the working directory of job 22078365 for instance. You see that rather than trying to parse the output of scontrol I am using the output formatting options of squeue to simply output the needed information.