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

**URL:** <https://community.openpbs.org/t/how-to-start-the-pbs-job-script-in-the-directory-that-it-was-submitted-from/1967>\
**Category:** Users/Site Administrators\
**Created:** [January 23, 2020, 12:17am UTC](https://community.openpbs.org/t/how-to-start-the-pbs-job-script-in-the-directory-that-it-was-submitted-from/1967 "2020-01-23T00:17:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jon](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.openpbs.org/jon/32/23_2.png) [@jon](https://community.openpbs.org/u/jon)\
**Post date:** [January 23, 2020, 12:17am UTC](https://community.openpbs.org/t/how-to-start-the-pbs-job-script-in-the-directory-that-it-was-submitted-from/1967/1 "2020-01-23T00:17:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![adarsh](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@adarsh](https://community.openpbs.org/u/adarsh)\
**Post date:** [January 23, 2020, 10:55pm UTC](https://community.openpbs.org/t/how-to-start-the-pbs-job-script-in-the-directory-that-it-was-submitted-from/1967/2 "2020-01-23T22:55:56Z")

</div>

> [@jon](#):
>
> I also tried to add this line in my ~/.bash\_profile
> 
> if [! -z ${PBS\_O\_WORKDIR}]; then cd $PBS\_O\_WORKDIR;fi

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

---

<div class="post-metadata">

**Author:** ![jon](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.openpbs.org/jon/32/23_2.png) [@jon](https://community.openpbs.org/u/jon)\
**Post date:** [January 24, 2020, 2:03am UTC](https://community.openpbs.org/t/how-to-start-the-pbs-job-script-in-the-directory-that-it-was-submitted-from/1967/3 "2020-01-24T02:03:06Z")

</div>

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**

---

<div class="post-metadata">

**Author:** ![jon](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.openpbs.org/jon/32/23_2.png) [@jon](https://community.openpbs.org/u/jon)\
**Post date:** [January 24, 2020, 4:06am UTC](https://community.openpbs.org/t/how-to-start-the-pbs-job-script-in-the-directory-that-it-was-submitted-from/1967/4 "2020-01-24T04:06:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![arwild01](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@arwild01](https://community.openpbs.org/u/arwild01)\
**Post date:** [January 24, 2020, 6:19am UTC](https://community.openpbs.org/t/how-to-start-the-pbs-job-script-in-the-directory-that-it-was-submitted-from/1967/5 "2020-01-24T06:19:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dtalcott](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.openpbs.org/dtalcott/32/410_2.png) [@dtalcott](https://community.openpbs.org/u/dtalcott)\
**Post date:** [January 28, 2020, 9:29pm UTC](https://community.openpbs.org/t/how-to-start-the-pbs-job-script-in-the-directory-that-it-was-submitted-from/1967/6 "2020-01-28T21:29:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jon](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.openpbs.org/jon/32/23_2.png) [@jon](https://community.openpbs.org/u/jon)\
**Post date:** [February 6, 2020, 6:06pm UTC](https://community.openpbs.org/t/how-to-start-the-pbs-job-script-in-the-directory-that-it-was-submitted-from/1967/7 "2020-02-06T18:06:40Z")

</div>

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