Tim
Tim

Reputation: 99428

running script stop in the middle

I have a script doing some computation and save some invisible plots into image files inside a for-loop on a linux server.

When I run the script, it usually get stuck in somewhere in the middle. I am not sure where exactly it stopped, but I can know at which iteration of the for-loop it stops by print-out. if I rerun it from the iteration where it stopped, it could continue to run past that place. So it seems to me there is no bug.

I just wonder how I can identify at which line it stops?

what might be the cause of the problem and how I can run the whole script from the beginning till the end?

Thanks!


UPDATE:

I use dbstop

dbstop if error  
dbstop if warning  
run path2script

The running still gets stuck somewhre and no message is given regarding why.

Upvotes: 2

Views: 2338

Answers (7)

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21563

Here is one way to find out what is going on:

  1. Turn on dbstop if error
  2. Once your code is really stuck, hit CTRL+C, this will allow you to inspect the situation and see what is going on

  3. If looking is not enough: select the line where the error was induced and hit f9.

  4. If you still didn't find the error, place a conditional breakpoint that only triggers after you reach the conditons during which/after the error occurs. If this is reached multiple times you are stuck in a loop somehow.

Sidenote: If you are not sure whether you are in a loop, recent versions of Matlab have an autoformatting button, use it!

Upvotes: 1

Miebster
Miebster

Reputation: 2404

I'd like to add that aborting (ctrl+c) a script will throw an error indicating what line it was on at the time of the abort.

Upvotes: 0

Juhl
Juhl

Reputation: 473

You could try using the dbstack function and the save the output, I guess overwritten a file in each iteration would do it.

Upvotes: 0

AlexC
AlexC

Reputation: 3710

As groovingandi suggests, set a conditional breakpoint within your code at the start of the iteration where the for loop normally gets stuck. You can do this with a command like:

dbstop in runscript at 500 if iLoop==365 
% where 500 is the first line within the for loop,
% and 365 is the iteration causing problems

If your script gets stuck without breakpoints, but can continue happily past that point if you use breakpoints and then continue, this normally indicates you've got a sporadic failure that's time-dependent, perhaps a race condition. Perhaps you're writing a file to the operating system, and then immediately afterwards looking at the OS to figure out what the next file's name should be, but your filesystem is cacheing slightly? Things like that have caused similar problems for me.

Look carefully at what your code is doing each time round the loop, for anything that might depend on steps before it that might be running asynchronously.

Upvotes: 0

groovingandi
groovingandi

Reputation: 2006

Difficult to tell without any knowledge about the script and the environment. Are you sure it is stuck and not just busy computing something or getting more memory? You could try to set a conditional breakpoint shortly before the iteration where the hang occurs and then interactively step through the following code using the debugger.

Upvotes: 0

Dima
Dima

Reputation: 39389

What exactly happens when your script gets "stuck"? Does it exit back to the prompt, or does matlab simply hang? If it is the latter, then it sounds like you have an infinite loop in your code...

Upvotes: 0

Eli
Eli

Reputation: 1269

What type of script is it?

Are you running out of memory? If you're exceeding the maximum allocated heap size, it could crash. When you re-launch it, it starts with all the memory it originally had and can run to completion before it might use all of its memory again.

I'd recommend checking for memory leaks.

Upvotes: 0

Related Questions