running Matlab script through unix bash script
90 views (last 30 days)
Show older comments
Hello,
I am trying to set up a crontab in Unix to run a Matlab script once a day. When I test my shell script on the Unix command line it runs fine and I get my output from the Matlab script. However, when I try setting up my crontab, nothing seems to happen. My crontab looks like this:
30 12 * * * /Datadisk/mcevoyd/spi_spei_auto/runmatlab.sh
And runmatlab.sh looks like this:
#!/bin/bash
matlab -nodisplay -nodesktop -r "run /Datadisk/mcevoyd/spi_spei_auto/get_HCN_data.m"
Again, runmatlab.sh works fine when run from the Unix command line. Does anyone know how I can get my crontab to run this matlab script?
Thanks, Dan
3 Comments
Walter Roberson
on 7 May 2018
You would need a MATLAB license to run it in the above form.
If you had a MATLAB Compiler license, then you could compile it on a Linux system, after which you would be able to install it on other Linux systems and run it without a license (after installing MCR).
Note, though, that not everything can be compiled, and that there can be some code changes needed to get it to refer to files properly (see matlabroot())
Answers (2)
Walter Roberson
on 20 Feb 2012
Are you certain that matlab is on the path that cron uses?
Where are you expecting the output files to go? Which directory is cron starting your task from? Is it possible a directory there is no write access to?
I would also suggest a modification for greater certainty of execution:
( cd /Datadisk/mcevoyd/spi_spei_auto; matlab -nodisplay -nodesktop -r "try; get_HCN_data; catch; end; quit" ) > YourOutputLogFile 2> YourErrorLogFile
3 Comments
Walter Roberson
on 20 Feb 2012
Yep, those kinds of things can be important.
I still advise the try/catch on execution: when MATLAB is being executed with stdin redirected (e.g. cron uses </dev/null ) and MATLAB does not see a quit in the command stream perhaps because a problem crashed the program, then MATLAB has a tendency to try to read command from stdin indefinitely :( :(
Jason Ross
on 9 Apr 2012
It's also likely that rather than trying to set a few environment variables, you could just call whatever script initializes your environment -- most likely a .cshrc somewhere. This would ensure that you have the same environment both when you run via cron and interactive from the terminal, eliminating a source of potential confusion.
NOTE: I'm assuming that you are running this cron job as the user "mcevoyd", and not as "root".
Milenko
on 9 Apr 2012
I have similiar problem but it does not work:
#!/bin/bash
setenv USER milenko
setenv LOGNAME milenko
setenv HOME /home/milenko
setenv PATH /home/milenko/fg77/fuzzycluster/np1ep1
setenv LANG en_US.UTF-8
matlab -nodisplay -nodesktop -r "scale1.m"
matlab -nodisplay -nodesktop -r "scale2.m"
But:
./proba: line 3: setenv: command not found
./proba: line 4: setenv: command not found
./proba: line 5: setenv: command not found
./proba: line 6: setenv: command not found
./proba: line 7: setenv: command not found
./proba: line 9: matlab: command not found
./proba: line 10: matlab: command not found
2 Comments
Jason Ross
on 9 Apr 2012
When you add /home/milenko/fg77/fuzzycluster/np1ep1 to the PATH, you need to include the previous PATH in there. Right now the PATH goes from (for example) /bin:/usr/bin:/sbin to /home/milenko/fg77/fuzzycluster/np1ep1. You want it to be /bin:/usr/bin:/sbin:/home/milenko/fg77/fuzzycluster/np1ep1
The syntax will be something like:
PATH=${PATH}:/home/milenko/fg77/fuzzycluster/np1ep1
Daniel Shub
on 9 Apr 2012
This is really a new question and should be asked as such. I am pretty sure bash does not have a setenv command. Either switch you shell or set the environment variables in a way that bash likes.
See Also
Categories
Find more on Entering Commands in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!