Best practices for job scripts

Following a few guidelines can help to make your job scripts more robust and reusable. In this tip we annotate a simple script with some advice and provide pointers towards other useful documentation.

Line 1: Job scripts are assumed to be written in Bash. Making this explicit via #!/bin/bash enables text editors to treat the file appropriately (amongst other benefits).

Line 2: The minimum required of a job script - assuming you don’t specify your resource requirements entirely via qsub - is a #PBS directive.

Line 4: Using set to configure Bash appropriately can help to avoid common issues - these flags ensure that your job will fail as soon as error occurs in your job script and that you don’t reference unset environment variables.

Line 6: The queuing system provides you with access to a several useful environment variables, including PBS_O_WORKDIR: the directory from which you submitted the job.

Line 8: As with any other software development, documenting scripts via comments (# …) can help to explain their purpose and operation to you and others.

Line 12: If you have any tasks that need to be executed before your job completes then you can use timeout to ensure that time is reserved in order to do so (5 minutes in this case). These tasks could include summarising results or tidying up ephemeral files. In general it is preferable to use job dependencies for this purpose.

Line 15: echo $SECONDS is an easy way to log how long your job took to run. It complements the memory and CPU usage metrics that are automatically logged to your job’s .o log file (see Controlling logging for jobs for further advice).

Further resources