简体   繁体   English

使Matlab自动化而无需用户登录

[英]Automating Matlab without requiring the user to be logged in

Is it possible to setup Matlab to run a specific script in the background when the user is NOT logged in? 当用户未登录时,是否可以将Matlab设置为在后台运行特定脚本? The script works fine on its own on a Windows Server 2008 machine with Matlab R2014a. 该脚本在带有Matlab R2014a的Windows Server 2008计算机上可以正常运行。 It doesn't need a gui for the script to complete, but I'm guessing that Matlab requires user-specific environments to be set. 它不需要GUI即可完成脚本,但是我猜测Matlab需要设置用户特定的环境。 Is there a place where this can be set ahead of time maybe? 有没有可以提前设置的地方?

I have tried "Task Scheduler" and it works just fine, but you have to set the setting to run only when that particular user is logged in or else nothing happens. 我已经尝试过“任务计划程序”,并且工作正常,但是您必须将设置设置为仅在该特定用户登录时运行,否则什么也不会发生。 The problem, of course, is the user session would require continuous monitoring in order to remain logged in (power outage, updates, etc.). 当然,问题在于用户会话需要连续监视才能保持登录状态(断电,更新等)。

Has anyone dealt with this in the past? 过去有人处理过吗? We've considered compiling it, but apparently there are certain functions and objects that the script uses (I didn't write it) that don't carryover during compilation. 我们已经考虑过对其进行编译,但是显然脚本使用的某些函数和对象(我没有写过)在编译过程中不会残留。

Any thoughts or suggestions are welcome! 欢迎任何想法或建议!

I've done some work for a client where we have an instance of MATLAB running continuously on a server, doing some stuff. 我为客户端做了一些工作,在该客户端上,我们有一个MATLAB实例在服务器上连续运行,做了一些工作。 The server occasionally fails (power outages, IT dept screw-ups etc), and it needs to be brought back up automatically. 服务器偶尔会发生故障(停电,IT部门发生故障等),因此需要自动将其恢复。

Note that MATLAB does need to be run as a user for licensing reasons, so our MATLAB instance always runs under a designated account, with a license dedicated to running that instance continuously. 请注意,由于许可的原因,MATLAB确实需要以用户身份运行,因此我们的MATLAB实例始终在指定的帐户下运行,并具有专用于连续运行该实例的许可证。

We have a Windows batch file to start up a suitable MATLAB instance, that contains a command similar to the following: 我们有一个Windows批处理文件来启动合适的MATLAB实例,其中包含与以下命令类似的命令:

CALL matlab.exe -nosplash -nodesktop -sd "myStartupFolder" -r "myMATLABCommand"

We then have a scheduled task set up so that 5 minutes after that account logs in, the batch file runs, and we have Windows set up so that when Windows starts, that account is automatically logged in (I'm no Windows admin, but I think we had to do some weird stuff in order to enable that, such as adding the account to some special domain group, or giving the account special privileges - you may need to research that a little more). 然后,我们设置了计划任务,以便在该帐户登录5分钟后运行批处理文件,并设置Windows,以便在Windows启动时自动登录该帐户(我不是Windows管理员,但是我认为我们必须做一些奇怪的事情才能做到这一点,例如将帐户添加到某个特殊的域组中,或赋予该帐户特殊的权限-您可能需要研究更多内容。

Anyway, that solved the issue for us. 无论如何,这为我们解决了这个问题。 If the server goes down and then recovers (perhaps IT bring it back up), the account is automatically logged in, the batch file runs, and the MATLAB instance is brought back up. 如果服务器出现故障然后恢复(可能是IT对其进行了备份),则帐户将自动登录,批处理文件将运行,并且MATLAB实例也将恢复。 If we need (rarely) to log in directly under that account without the task running, we have a 5 minute window to stop the scheduled task from running, which is no problem. 如果我们需要(很少)直接在该帐户下登录而不执行任务,那么我们有5分钟的时间可以停止计划任务的运行,这没问题。

Hope that helps! 希望有帮助!

Unfortunately and afaik, Matlab can only be startet without GUI on Linux (maybe on Mac OS X too?). 不幸的是,Matlab只能在没有Linux上的GUI的情况下启动(也许在Mac OS X上也是如此)。

~$ cat /tmp/stackoverflow.m 
s='stackoverflow';
length(s)

~$ ./R2013a/bin/matlab -nodisplay -nojvm -nodesktop -nosplash -r "run /tmp/stackoverflow.m, exit"

                                                                        < M A T L A B (R) >
                                                              Copyright 1984-2013 The     MathWorks, Inc.
                                                                R2013a (8.1.0.604) 64-bit (glnxa64)
                                                                         February 15, 2013


To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.


ans =

    13

~$ 

However Matlab itself is not capable of Shebang #! 但是Matlab本身没有能力Shebang #! in a Bash script. 在Bash脚本中。 So it's always a workaround. 因此,这始终是一种解决方法。

A better solution might be to run your Matlab instance continuously and write a daemon/script, which will run you .m script time-dependent for example. 更好的解决方案可能是连续运行Matlab实例并编写守护程序/脚本,例如,.m脚本将随时间运行。

A much better way is to use the Matlab Coder Toolbox (if you have it) and compile a stand-alone binary from you .m file. 更好的方法是使用Matlab Coder Toolbox(如果有)并从.m文件中编译独立的二进制文件。 This binary should be easily runable with task schedular on Windows. 此二进制文件应该可以在Windows上按计划的任务轻松运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM