Writing Scripts

Job Scripts

Interactive jobs are great for development and testing but if you’re running something that takes a long time then they aren’t ideal. Typically it is better to write a script to automate everything that you would have had to do by hand. A minimal jobscript for the PBS/Torque batch system used on Saguaro might look something like this:

#!/bin/bash

#PBS -l nodes=1

#PBS -j oe

#PBS -o My_Log_file.txt

#PBS -l walltime=00:01:00

 

cd $PBS_O_WORKDIR

Your job is now waiting in the queue to run. You can follow its progress with the ‘qstat’ command.
saguaro2:~/ > qsub my_job_script.pbs
4213885.moab.local
If you login to a head node, you can save this in a file such as “my_job_script.pbs” and submit it to the queue with the ‘qsub’ command.
Generally, the most important thing will be the ‘S’ (Status) column. In this case the job we submitted has status ‘C’ (Completed). Other important states include ‘Q’ (Queued), ‘R’ (Running) and the dreaded ‘E’ (Error).

 

Shell Scripts

Here is how to automate the submission of multiple jobs. Say you want to run a parameter sweep with one matlab job per point. One approach may be to pass the parameter value to your matlab orogram (call it “myprogram.m”) as an argument. Now, all we need to do is automate the writing of the submission script. Let’s create one file called “scripttop.”

#!/bin/bash 
#PBS -l nodes=1:ppn=1
#PBS -N matlab
#PBS -l walltime=1:00:00
#PBS -j oe          
cd $PBS_O_WORKDIR
module load matlab/R2012b

Now we just need to create a set of unique subdirectories from which to run our matlab jobs. We write a short script (call this “job_launcher.sh”) that looks like this:
for f in 1 2 3 4 5 ; do
mkdir $f
cd $f
cat ../scripttop > runjob
echo “matlab -nodesktop -nodisplay -nojvm -r \”myprogram($f)\”” >> runjob
qsub runjob
cd ..
done

and you can run this with:
saguaro2:~/ > chmod u+x job_launcher
saguaro2:~/ > ./job_launcher