Nicholas
Nicholas

Reputation: 1935

Run several matlab instances in a nohup bash script

I'm trying to do something like the following. Right now, I connect via ssh to a remote machine and run my analysis with nohup command as

nohup matlab -nodesktop -nodisplay < mycode.m > output.txt 2>&1 &

Now I want to write a script that runs in background several nohup commands one after the other one. I managed to do

#!/bin/bash
matlab -nodesktop -nodisplay -r "mycode;quit;" > output.txt

But not only the code works with one CPU only, but also it goes in an infinite loop and never finishes the job.

I can I solve that? It is important that I could close the terminal after launching the script.sh

EDIT: Thanks to you I manage to do and let the following thing to work well

ssh user@ipaddress
screen
cd folder1/
nohup matlab -nodesktop -nodisplay < mycode.m > output.txt 2>&1 &
exit
screen
cd folder2/
nohup matlab -nodesktop -nodisplay < mycode.m > output.txt 2>&1 &
exit

Now is it possible to do a script of that? Because I noticed that any time I type screen I have to press Enter right after.

EDIT2: @Peter I did what you suggested

#!/bin/bash
cd folder1/
matlab -nodesktop -nodisplay -r "mycode;quit;" < /dev/null  > output.txt
cd folder2/
matlab -nodesktop -nodisplay -r "mycode;quit;" < /dev/null  > output.txt

But only the first matlab runs, how is it possible?

Upvotes: 4

Views: 3332

Answers (2)

Nicholas
Nicholas

Reputation: 1935

Thank all of you for helping me. I think I've found that I was looking for.

  1. I ssh into my machine
  2. create a new screen with the command screen -dmS analysis
  3. run the script (see below) nohup ./script.sh &
  4. close the screen exit
  5. check the running with top (as usual :))

This is my script.sh

#!/bin/bash
matlab -nodesktop -nodisplay -r "cd folder1/; run('mycode.m'); quit"  < /dev/null  > output.txt
matlab -nodesktop -nodisplay -r "cd folder2/; run('mycode.m'); quit"  < /dev/null  > output.txt

The analyses run one after the other! Great!

Upvotes: 1

konsolebox
konsolebox

Reputation: 75498

Compiling all suggestions and ideas you could try these:

#!/bin/bash
ssh user@ipaddress "
cd folder1/
nohup matlab -nodesktop -nodisplay < mycode.m > output.txt 2>&1 &
cd folder2/
nohup matlab -nodesktop -nodisplay < mycode.m > output.txt 2>&1 &
"

Or

#!/bin/bash
ssh user@ipaddress "
cd folder1/
nohup matlab -nodesktop -nodisplay -r 'mycode;quit;' < /dev/null  > output.txt 2>&1 &
cd folder2/
nohup matlab -nodesktop -nodisplay -r 'mycode;quit;' < /dev/null  > output.txt 2>&1 &
"

Or

#!/bin/bash
ssh user@ipaddress "
cd folder1/
screen -dm matlab -nodesktop -nodisplay < mycode.m > output.txt 2>&1
cd folder2/
screen -dm matlab -nodesktop -nodisplay < mycode.m > output.txt 2>&1
"

Or

#!/bin/bash
ssh user@ipaddress "
cd folder1/
screen -dm matlab -nodesktop -nodisplay -r 'mycode;quit;' < /dev/null  > output.txt 2>&1
cd folder2/
screen -dm matlab -nodesktop -nodisplay -r 'mycode;quit;' < /dev/null  > output.txt 2>&1
"

You could also try expect to control matlab instead of sending mycode.m or using -r to it.

Try doing nohup on screen as well. And I think you no longer need to add & to it since screen runs as a daemon by default already.

Upvotes: 4

Related Questions