PhD Study

Run MATLAB as background job

As continuation of my last post, I got a very useful trick of running MATLAB namely to run it as background job.

I am running an optimization problem in MATLAB with hundreds of optimized variables, so it may take  around 30 minutes or more to finish.  The trick is to make a script as following :

#!/bin/csh -f

unsetenv DISPLAY

nohup matlab < $1 > $2 &

save it as matbg and make it as executable, chmod +x matbg.

$1 is your matlab m file and $2 is an output file. For example, you run it: matbg optim.m result.

It is an excellent script. It means I can leave my simulation running while I am doing other activities 🙂

Similarly, I can use that script for other programs in UNIX server just by changing the program name.


9 thoughts on “Run MATLAB as background job

  1. I tried
    matlab -nodisplay -r script &
    and when I use jobs to check the status, matlab’s stopped, and it doesn’t start until I bring it to foreground with fg. Why does adding nohup solve this problem?

    Also, as in my example, if you say -nodisplay, matlab won’t try to display to X. You can also say -nojvm if you don’t need the jvm.

  2. hi,
    it should be straightforward as the three lines unix shell above.

    my question then:
    1. what your unix shell ? as I wrote, it is csh.
    2. why you used -nodisplay -r script ? you see i set unsetenv DISPLAY, and use as an input file and as an output file, & means it will be executed as background job.
    3. nohup means the command will execute right away.

    to check the script is run, check by using top or ps command.

    i am wondering if you have enough unix shell knowledge.

  3. I suggested using -nodisplay -r script because then you don’t have to use input redirection or mess with shell variables. It’s potentially more portable to use matlab’s builtin options as much as possible. And if you use -nojvm, Matlab may even run faster.

    My real question was why does nohup solve my problem of Matlab pausing when you start it in the background. It’s odd that adding nohup works, because apparently all nohup is supposed to do is make a process ignore the HUP signal so it continues running when you log out.

    Which also means you have to be careful using this “nohup matlab … ” construction if you sshed into the computer running matlab. If the matlab process hasn’t completed by the time you log out, you can leave the shell hanging.

    Ughhh. I hate thinking about this stuff. I think I’ll just stick with using screen 🙂

    1. I’m not sure if I understand your question.

      Just make sure your matlab program works in foreground mode, and then it is easy to switch to background mode using the script.
      I don’t use -nojvm option since I don’t produce images in my matlab program. What I want from my matlab program is just a flat file (of course save the result in *.mat file ).

      I made the script because my matlab program, which is running an optimization, may take days. So instead observing the optimization from my desktop, let my server (a Linux box) do it in a background job. Moreover, I can make my matlab program send an email to me once the optimization is finished.

    2. It works if you pass the matlab script you want to run as an argument:

      matlab -nodesktop & info.out &

      This will run MatlabFile.m in the background and write any screen output to the file info.out. The >& also writes any errors to the output file.

      The problem with using -r MatlabFile may be that matlab tries to read input using STDIN, which doesn’t work for background jobs. If you ran job as usual:

      matlab -nodisplay -r script

      then matlab would open, execute script and then just sit there, waiting for user input, which obviously wouldn’t be good for a background job.

  4. I forgot to tell… Yes, I have been using ssh to access my server. And there is no problem as you mentioned. After running the script, I just logout and the script still runs.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s