Skip to content

Instantly share code, notes, and snippets.

@nmpowell
Last active July 22, 2016 14:39
Show Gist options
  • Save nmpowell/654b56afe9a71ee2f9bdfc7bc1036bca to your computer and use it in GitHub Desktop.
Save nmpowell/654b56afe9a71ee2f9bdfc7bc1036bca to your computer and use it in GitHub Desktop.
Bash / shell script for running Matlab jobs with the Sun Grid Engine. Qsub this whole script, rather than qsubbing Matlab directly.
#!/bin/sh
# matlab_np.sh
# Shell script to run a Matlab function from the bash shell
# Ensure Matlab function is on the path first
# NB Matlab breaks the bash shell, hence stty echo
# stty echo works by itself; stty sane is just-in-case; haven't confirmed that it's necessary.
#
# Example usage: $ matlab_np.sh functionName 'string arg' arg2 ${arg3}...
# Use with Qsub: $ ${QSUB_CMD} matlab_np.sh functionName arguments
# NB do not Qsub matlab directly as your arguments won't be passed to the Matlab function
# Nick Powell 2012-05-10
# # # # # # # # # # # # # # # # #
# Passed-in Variables #
# # # # # # # # # # # # # # # # #
# First argument is the function name in Matlab (ensure it's on the Matlab path)
functionName=$1
# The remaining arguments are arguments for the Matlab function
shift
arguments=$@
# # # # # # # # # # # # # # #
# Fixed Variables #
# # # # # # # # # # # # # # #
# Programs
MATLAB=/share/apps/matlabR2011b/bin/matlab
# # # # # # # # # # # # # # # # # # # # # # # #
# Universal Variables and Functions #
# # # # # # # # # # # # # # # # # # # # # # # #
source /home/npowell/scripts/universal_params_np.sh
source /home/npowell/scripts/functions.sh
# # # # # # # # #
# Go! #
# # # # # # # # #
time_start=`date +%s`
# Set up log file for the whole run
setUpLog ${BASH_SOURCE}
# Check directories
makeDirs ${logDir}
echoStatusTime "Running Matlab function: ${functionName} with arguments: ${arguments}"
# Do not qsub this directly; qsub this whole script instead.
${MATLAB} \
-nodisplay \
-r \
"${functionName} ${arguments}; exit"
# ${MATLAB} -nodesktop -nosplash -nodisplay -r "${functionName} ${arguments}; quit;"
# Because Matlab breaks the shell on quitting otherwise:
stty echo
stty sane
time_end=`date +%s`
time_diff=$(( ${time_end} - ${time_start} ))
echo ""
echo "*******************************************************************************"
echoStatusTime "Matlab function ${functionName} complete! Took ${time_diff} seconds total."
echo "*******************************************************************************"
echo ""
completeFunc
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment