How to start the PBS Job script in the directory that it was submitted from

I would like to submit a job from an application that calls its own program like

qsub -q workq -l select=2:ncpus=1,place=scatter:excl – -i file.in option2 option3

The application fails since it can’t find the file. I could put the full path to the file.in but I don’t want to hard code the path since I would like to submit from various directories and I don’t want all of the stdout and stderr files in my home dir.

I also tried to add this line in my ~/.bash_profile

if [ ! -z ${PBS_O_WORKDIR} ]; then cd $PBS_O_WORKDIR;fi

This works if I run the job interactively but not in batch.

Is there a submission flag I can use when submitting my job to start it in the right directory or is there a way that I can get it to load my ~/.bash_profile before the script is launched.

I have not tested this , but just checking
Would adding above line to /etc/profile.d/pbs_work_dir.sh would work ?

Looks like I found my answer in the user guide ;( Does anyone know the non easy way or is the answer to write a hook for this?

2.3.4 Submitting Jobs by Specifying Executable on Command Line

You can run a PBS job by specifying an executable and its arguments instead of a job script. When you run qsub this way, it runs the executable directly. It does not start a shell, so no shell initialization scripts are run, and execution paths and other environment variables are not set. There is not an easy way to run your command in a different directory. You should make sure that environment variables are set correctly, and you will usually have to specify the full path to the command.

To submit a job directly, you specify the executable on the command line:

For example, to run myprog with the arguments a and b:

To run myprog with the arguments a and b, naming the job JobA,

To use environment variables you define earlier: qsub [] – [] qsub – myprog a b qsub -N JobA – myprog a b

export INFILE=/tmp/myinfile export INDATA=/tmp/mydata qsub – a.out $INFILE $INDATA

1 Like

Well I found a solution I think. I wrote a wrapper script for bash called pbs_bash that contains the following

#!/bin/bash

Get the aliases and functions

if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

if [ ! -z ${PBS_O_WORKDIR} ]; then cd $PBS_O_WORKDIR;fi

“$@”

I then do

qsub – /full/path/to/pbs_bash program arg1 arg2 …

and it seems to work as expected. I would be nice to have an option in PBS to start the job in the current working directory.

1 Like

I wish PBSPro would steal an idea from torque. Torque’s qsub had a flag (-d) which would set the variable PBS_O_INITDIR. If -d wasn’t passed PBS_O_INITDIR defaulted to the user’s home directory.

Torque would always chdir() to $PBS_O_INITDIR immediately prior to invoking the jobscript. This was true for both interactive and batch jobs,

If PBSPro’s qsub had such an option you could have done:

qsub … -d $PWD – program arg1 arg2

1 Like

Minor bug, but the last line should be
"$@"
rather than just
$@

Otherwise, any command line arguments with spaces or
other special characters will get broken up.

Thanks for the pointer @dtalcott. I have made the update.