Child pages
  • Tutorial - Submitting a job using qsub
Skip to end of metadata
Go to start of metadata

qsub Tutorial

  1. Synopsis
  2. What is qsub
  3. What does qsub do?
  4. Arguments to control behavior


qsub Synopsis

For detailed information, see this page.

What is qsub?

qsub is the command used for job submission to the cluster. It takes several command line arguments and can also use special directives found in the submission scripts or command file. Several of the most widely used arguments are described in detail below.

Useful Information

For more information on qsub do

More information on qsub

What does qsub do?


All of our clusters have a batch server referred to as the cluster management server running on the headnode. This batch server monitors the status of the cluster and controls/monitors the various queues and job lists. Tied into the batch server, a scheduler makes decisions about how a job should be run and its placement in the queue. qsub interfaces into the the batch server and lets it know that there is another job that has requested resources on the cluster. Once a job has been received by the batch server, the scheduler decides the placement and notifies the batch server which in turn notifies qsub (Torque/PBS) whether the job can be run or not. The current status (whether the job was successfully scheduled or not) is then returned to the user. You may use a command file or STDIN as input for qsub.

Environment variables in qsub

The qsub command will pass certain environment variables in the Variable_List attribute of the job. These variables will be available to the job. The value for the following variables will be taken from the environment of the qsub command:

  • HOME (the path to your home directory)
  • LANG (which language you are using)
  • LOGNAME (the name that you logged in with)
  • PATH (standard path to excecutables)
  • MAIL (location of the users mail file)
  • SHELL (command shell, i.e bash,sh,zsh,csh, ect.)
  • TZ (time zone)

These values will be assigned to a new name which is the current name prefixed with the string "PBS_O_". For example, the job will have access to an environment variable named PBS_O_HOME which have the value of the variable HOME in the qsub command environment.

In addition to these standard environment variables, there are additional environment variables available to the job.

  • PBS_O_HOST (the name of the host upon which the qsub command is running)
  • PBS_SERVER (the hostname of the pbs_server which qsub submits the job to)
  • PBS_O_QUEUE (the name of the original queue to which the job was submitted)
  • PBS_O_WORKDIR (the absolute path of the current working directory of the qsub command)
  • PBS_ARRAYID (each member of a job array is assigned a unique identifier)
  • PBS_ENVIRONMENT (set to PBS_BATCH to indicate the job is a batch job, or to PBS_INTERACTIVE to indicate the job is a PBS interactive job)
  • PBS_JOBID (the job identifier assigned to the job by the batch system)
  • PBS_JOBNAME (the job name supplied by the user)
  • PBS_NODEFILE (the name of the file contain the list of nodes assigned to the job)
  • PBS_QUEUE (the name of the queue from which the job was executed from)
  • PBS_WALLTIME (the walltime requested by the user or default walltime allotted by the scheduler)

Arguments to control behavior

As stated before there are several arguments that you can use to get your jobs to behave a specific way. This is not an exhaustive list, but some of the most widely used and many that you will will probably need to accomplish specific tasks.

Declare the date/time a job becomes eligible for execution

To set the date/time which a job becomes eligible to run, use the -a argument. The date/time format is [[[[CC]YY]MM]DD]hhmm[.SS]. If -a is not specified qsub assumes that the job should be run immediately.


To test -a get the current date from the command line and add a couple of minutes to it. It was 10:45 when I checked. Add hhmm to -a and submit a command from STDIN.

Example: Set the date/time which a job becomes eligible to run

Handy Hint

This option can be added to pbs script with a PBS directive such as

Equivalent PBS Directive

Defining the working directory path to be used for the job

To define the working directory path to be used for the job -d option can be used. If it is not specified, the default working directory is the home directory.

Example: Define the working directory path to be used for the job

Handy Hint

This option can be added to pbs script with a PBS directive such as

Equivalent PBS Directive

Manipulate the output files

As a default all jobs will print all stdout (standard output) messages to a file with the name in the format <job_name>.o<job_id> and all stderr (standard error) messages will be sent to a file named <job_name>.e<job_id>. These files will be copied to your working directory as soon as the job starts. To rename the file or specify a different location for the standard output and error files, use the -o for standard output and -e for the standard error file. You can also combine the output using -j.

Create a simple submission file:
Create a simple submission file:

Handy Hint

This option can be added to pbs script with a PBS directive such as

Equivalent PBS Directive
Submit your job with the standard error file renamed:

Handy Hint

This option can be added to pbs script with a PBS directive such as

Equivalent PBS Directive
Combine them using the name sleep.log:

Handy Hint

This option can be added to pbs script with a PBS directive such as

Equivalent PBS Directive


The order of two letters next to flag -j is important. It should always start with the letter that's been already defined before, in this case 'o'.

Place the joined output in another location other than the working directory:

Mail job status at the start and end of a job

The mailing options are set using the -m and -M arguments. The -m argument sets the conditions under which the batch server will send a mail message about the job and -M will define the users that emails will be sent to (multiple users can be specified in a list seperated by commas). The conditions for the -m argument include:

  • a: mail is sent when the job is aborted.
  • b: mail is sent when the job begins.
  • e: main is sent when the job ends.
Using the sleep.pbs script created earlier, submit a job that emails you for all conditions:

Handy Hint

This option can be added to pbs script with a PBS directive such as

Equivalent PBS Directive

Submit a job to a specific queue

You can select a queue based on walltime needed for your job. Use the 'qstat -q' command to see the maximum job times for each queue.

Submit a job to the bigmem queue:

Handy Hint

This option can be added to pbs script with a PBS directive such as

Equivalent PBS Directive

Submitting a job that is dependent on the output of another

Often you will have jobs that will be dependent on another for output in order to run. To add a dependency, we will need to use the -W (additional attributes) with the depend option. We will be using the afterok rule, but there are several other rules that may be useful. (man qsub)


To illustrate the ability to hold execution of a specific job until another has completed, we will write two submission scripts. The first will create a list of random numbers. The second will sort those numbers. Since the second script will depend on the list that is created we will need to hold execution until the first has finished.


Once the file are created, lets see what happens when they are submitted at the same time:

Submit at the same time

Since they both ran at the same time, the sort script failed because the file rand.list had not been created yet. Now submit them with the dependencies added.

Submit them with the dependencies added

We now see that the sort.pbs job is in a hold state. And once the dependent job completes the sort job runs and we see:

Job status with the dependencies added

Useful Information

  • afterany:jobid[:jobid...] implies that job may be scheduled for execution after jobs jobid have terminated, with or without errors.
  • afterok:jobid[:jobid...] implies that job may be scheduled for execution only after jobs jobid have terminated with no errors.
  • afternotok:jobid[:jobid...] implies that job may be scheduled for execution only after jobs jobid have terminated with errors.

Submitting multiple jobs in a loop that depend on output of another job

This example show how to submit multiple jobs in a loop where each job depends on output of job submitted before it.


Let's say we need to write numbers from 0 to 999999 in order onto a file output.txt. We can do 10 separate runs to achieve this, where each run has a separate pbs script writing 100,000 numbers to output file. Let's see what happens if we submit all 10 jobs at the same time.

The script below creates required pbs scripts for all the runs.

Create PBS Scripts for all the runs
Change permission to make it an executable
Run the Script
List of Created PBS Scripts
PBS Script
Submit Multiple Jobs at a Time

This clearly shows the nubmers are in no order like we wanted. This is because all the runs wrote to the same file at the same time, which is not what we wanted.

Let's submit jobs using qsub dependency feature. This can be achieved with a simple script shown below.

Simple Script to Submit Multiple Dependent Jobs
Let's make it an executable
Submit dependent jobs by running the script
Output after first run
Output after final run

This shows that numbers are written in order to output.txt. Which in turn shows that jobs ran one after successful completion of another.

Opening an interactive shell to the compute node

To open an interactive shell to a compute node, use the -I argument. This is often used in conjunction with the -X (X11 Forwarding) and the -V (pass all of the users environment)

Open an interactive shell to a compute node

Passing an environment variable to your job

You can pass user defined environment variables to a job by using the -v argument.


To test this we will use a simple script that prints out an environment variable.

Passing an environment variable

Next use qsub without the -v and check your standard out file

qsub without -v

Then use the -v to set the variable

qsub with -v

Handy Hint

This option can be added to pbs script with a PBS directive such as

Equivalent PBS Directive

Useful Information

Multiple user defined environment variables can be passed to a job at a time.

Passing Multiple Variables

Passing your environment to your job

You may declare that all of your environment variables are passed to the job by using the -V argument in qsub.


Use qsub to perform an interactive login to one of the nodes:

Passing your environment: qsub with -V

Handy Hint

This option can be added to pbs script with a PBS directive such as

Equivalent PBS Directive

Once the shell is opened, use the env command to see that your environment was passed to the job correctly. You should still have access to all your modules that you loaded previously.

Submitting an array job: Managing groups of jobs

Sometimes users will want to submit large numbers of jobs based on the same job script. Rather than using a script to repeatedly call qsub, a feature known as job arrays exists to allow the creation of multiple jobs with one qsub command. Additionally, this feature includes a new job naming convention that allows users to reference the entire set of jobs as a unit, or to reference one particular job from the set. Each job submitted will have a job id in the format <id>[<num>].hostname. In the case of a submission number of 5554444, each 5554444[x] job has an environment variable called PBS_ARRAYID, which is set to the value of the array index of the job, so 55544440.hostname would have PBS_ARRAYID set to 0. This will allow you to create job arrays where each job in the array will perform slightly different actions based on the value of this variable, such as performing the same tasks on different input files. One other difference in the environment between jobs in the same array is the value of the PBS_JOBNAME variable.


First we need to create data to be read. Note that in a real application, this could be data, configuration setting or anything that your program needs to run.

Create Input Data

To create input data, run this simple one-liner:

Creating input data

Submission Script
Submission Script: array.pbs

Submit & Monitor

Instead of running five qsub commands, we can simply enter:

Submitting and Monitoring Array of Jobs



pbstop by default doesn't show all the jobs in the array. Instead, it shows a single job in just one line in the job information. Pressing 'A' shows all the jobs in the array. Same can be achieved by giving the command line option '-A'. This option along with '-u <NetID>' shows all of your jobs including array as well as normal jobs.



Typing 'A' expands/collapses array job representation.

Comma delimited lists

The -t option of qsub also accepts comma delimited lists of job IDs so you are free to choose how to index the members of your job array. For example:

Comma delimited lists

A more general for loop - Arrays with step size

By default, PBS doesn't allow array jobs with step size. qsub -t 0-10 <pbs.script> increments PBS_ARRAYID in 1. To submit jobs in steps of a certain size, let's say step size of 3 starting at 0 and ending at 10, one has to do

To make it easy for users we have put a wrapper which takes starting point, ending point and step size as arguments for -t flag. This avoids default necessity that PBS_ARRAYID increment be 1. The above request can be accomplished with (which happens behind the scenes with the help of wrapper)

Here, 0 is the starting point, 10 is the ending point and 3 is the step size. It is not necessary that starting point must be 0. It can be any number. Incidentally, in a situation in which the upper-bound is not equal to the lower-bound plus an integer-multiple of the increment, for example

wrapper automatically changes the upper bound as shown in the example below.

Arrays with step size


By default, PBS doesn't support arrays with step size. On our clusters, it's been achieved with a wrapper. This option might not be there on clusters at other organizations/schools that use PBS/Torque.


If you're trying to submit jobs through ssh to login nodes from your pbs scripts with statement such as

arrays with step size wouldn't work unless you either add

to your pbs script that's in bash or add this to your .bashrc in your home directory. Adding this makes alias for qsub come into effect there by making wrapper act on command line options to qsub (For that matter this brings any alias to effect for commands executed via SSH).

If you have

in your pbs script you don't need to add this either to your pbs script or to your .bashrc in your home directory.

A List of Input Files/Pulling data from the ith line of a file

Suppose we have a list of 1000 input files, rather than input files explicitly indexed by suffix, in a file file_list.text one per line:

A List of Input Files/Pulling data from the ith line of a file

In this example, the '-n' option suppresses all output except that which is explicitly printed (on the line equal to PBS_ARRAYID).

Let’s say you have a list of 1000 numbers in a file, one number per line. For example, the numbers could be random number seeds for a simulation. For each task in an array job, you want to get the ith line from the file, where i equals PBS_ARRAYID, and use that value as the seed. This is accomplished by using the Unix head and tail commands or awk or sed just like above.

A List of Input Files/Pulling data from the ith line of a file

You can use this trick for all sorts of things. For example, if your jobs all use the same program, but with very different command-line options, you can list all the options in the file, one set per line, and the exercise is basically the same as the above, and you only have two files to handle (or 3, if you have a perl script generate the file of command-lines).


Delete all jobs in array

We can delete all the jobs in array with a single command.

Deleting array of jobs

Delete a single job in array

Delete a single job in array, e.g. number 4,5 and 7

Deleting a single job in array



PBS Script Generator
An interactive tool that generates PBS script based on user's input. Check this page for more details.
Front-Line HPC Consulting
HPC consultations are available once a week, Monday 1-3 PM. Appointments are required. Please make an appointment at




  • No labels