Tuesday, July 3, 2012

nohup and MATLAB

Occasionally, I need to run a large ensemble of MATLAB simulations and would prefer to offload this to a server. This can easily be accomplished with the following script, called matbg.sh (MATLAB background shell script):
#! /bin/bash
# Clear the DISPLAY.
unset DISPLAY  # unset DISPLAY for some shells
# Call MATLAB with the appropriate input and output,
# make it immune to hangups and quits using ''nohup'',
# and run it in the background.
nohup nice -10 matlab -nodisplay -nodesktop -nojvm -nosplash <$1 >$2 &
Log on to your machine, place this script in the same location as the m-file you wish to run and then execute:
./matbg.sh myscript1.m mylogfile.out
You're then welcome to log out and wait for the job to finish, but make sure that your m-file saves your output for post processing. If for some reason, such as if you are using some aspect of the parallel computing toolbox, you need Java, just remove the -nojvm flag from the above script. The next you may want is to be alerted to the completion of the job. This can be accomplished by tacking on the following to the end of your m-file:
% Email message.  Feel free to add in details about how long the job took,
% if it completed successfully, etc.:
mesg = sprintf('finished job');
% The command which will send the email:
cmd =...
sprintf(['echo "sending email notification" | mail -s "%s" ''%s'],...
mesg,'my.email@domain.com');
system(cmd);

No comments:

Post a Comment