Trapping and returning Syntax error to Jenkins

I use Jenkins to run Matlab projects. Currently we run Matlab on windows 7 and our Jenkins server is on a Linux box.
I use perl to create a "Run_cmd.m" ($ML_CMD_R_CMD) file with my matlab script that I want to run.
$err = system("matlab -wait -logfile \"$ML_LOG_FILE\" -noawt -nosplash -sd \"$WORKSPACE\" -r \"$ML_CMD_R_CMD\" 2>&1 ");
I have my jobs running and I get the data that I want and they usually run in a well behaved manner.
If the job runs without any error the perl script waits and returns back to jenkins that everything was ok when done.
If the Matlab scripts have a runtime error , things get reported and the perl script returns a failed status.
However I am currently working on my error checking and if I generate a matlab script with a syntax error in it. I leave out a quote for example, the matlab session reports the error to my log file, but does not exit. It sits there and never returns and I have to manually log into the system and kill the matlab session by hand. The perl script never detects that the job should of completed as the matlab command never exits.
So my question is: Is there a way to tell the matlab session to die and return if there is a syntax error in the script it is running?
The run_cmd.m file looks like:
try
<Matlab calls>
catch ERR
print error stuff
exit();
end;

Answers (1)

Currently you have
-r \"$ML_CMD_R_CMD\"
Alter that to have a try/catch directly, outside of the .m file that might be faulty:
-r \"try $ML_CMD_R_CMD; catch; end; quit\"
provided that $ML_CMD_R_CMD itself is not syntactically faulty, this will work to catch problems if what is contained inside any file named by $ML_CMD_R_CMD has syntactic problems.

2 Comments

Sorry I might of been a bit misleading, even though I read this 4 times before sending. Should of been written:
I use perl to create a "Run_cmd.m" ($ML_CMD_R_CMD) that I then run under a Matlab session. The perl code shows how matlab gets called with the file that was created.
I included the Matlab snip as a way to show that I was using the try and catch inside the script Run_cmd.m. This is never caught as the script fails to run because of a syntax error.
My solution remains unchanged. In the place you launch MATLAB, include a try/catch block around the invocation of the script file. If the script file has syntax errors, the try/catch block will handle it cleanly. You can also have try/catch inside of your (possibly wrong at any given time) Run_cmd.m : those would protect against errors in the calculation, whereas the try/catch around _invoking Run_cmd will protect against Run_cmd having syntax errors.

Sign in to comment.

Categories

Products

Tags

Asked:

on 18 May 2015

Commented:

on 19 May 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!