Difference between revisions of "Running scripts in the background"

From Dynamo
Jump to navigation Jump to search
(Created page with "This information refers to Linux and Mac operative systems = The nohup command = The <t>nohup</tt> command allows to run a command in the background, in a fashion that interru...")
 
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
This information refers to Linux and Mac operative systems
+
This information refers to Linux and Mac operative systems.
 +
 
 
= The nohup command =
 
= The nohup command =
The <t>nohup</tt> command allows to run a command in the background, in a fashion that interruptions of the connection will not terminate the job. The terminal containing the job might get closed and the job will still carry on.
+
The <tt>nohup</tt> command allows to run a job in the background, in a fashion that interruptions of the connection will not terminate the job. The terminal containing the job might get closed and the job will still carry on.
In order to keep track of the PID (process identifier) assigned by the process to the command launched, remember to use the <tt>&</tt> at the end of the line that invokes nohup. This PID might be needed to interrupt the execution of the background job.
+
In order to keep track of the PID (process identifier) assigned by the process to the command launched, remember to use the <tt>&</tt> at the end of the line that invokes nohup, this will force the system to show the PID after launching the job. This PID might be needed to interrupt the execution of the background job.
 +
 
 +
== Killing a background process ==
 +
Use the command <tt>kill -9</tt> followed by the PID of the process that you want to kill. For instance, for a job with PID <tt>12657</tt>, write:
 +
<tt>kill -9 12657 </tt>
  
 
== Retrieving the PID of your process==
 
== Retrieving the PID of your process==
Use the <tt>pgrep</tt> command, followed by the command that you nohup-ed. For instance:
+
You can use the <tt>pgrep</tt> command, followed by the command that you nohup-ed. For instance:
  
 
  <tt>pgrep MATLAB</tt>
 
  <tt>pgrep MATLAB</tt>
  
to locate PIDs that carry a MATLAB process. Note that in your system, capital letters might not apply
+
to locate PIDs that carry a MATLAB process and that are ran by nohup. Note that in your system, capital letters might not apply.
 +
 
 +
More generally,
 +
<tt>ps -a</tt>
 +
will list all user jobs currently under execution.
  
 
=Running Matlab scripts=
 
=Running Matlab scripts=
 +
This informations refers only to Matlab scripts defined by the user. For 'Dynamo' projects, see below.
  
 
==With a license==
 
==With a license==
Line 22: Line 32:
  
 
==With the standalone==
 
==With the standalone==
 +
You can replace <tt>matlab</tt> with <tt>dynamo</tt>, and use
 +
 +
<tt>nohup dynamo < myTest.m > output.txt & </tt>
 +
in a terminal where you have activated Dynamo previously.
  
 
== Matlab scripts including Dynamo commands ==
 
== Matlab scripts including Dynamo commands ==
Remember that you need to include a line that activates 'Dynamo' inside the script that you submit into nohup.
+
Remember that you need to include a line that activates 'Dynamo' inside the script that you submit into nohup.  
 +
This is not necessary if you use  the standalone with
 +
<tt>nohup dynamo < myTest.m > output.txt & </tt>
  
= Running projects in the background=
+
=Running projects in the background=
'Dynamo' projects can be run directly by nohup. A project script <tt>myProject.exe</tt> can be ran by
+
'Dynamo' projects can be run directly by <tt>nohup</tt>. A project script <tt>myProject.exe</tt> can be ran by
 
  <tt>nohup myProject.exe</tt>
 
  <tt>nohup myProject.exe</tt>
 +
 +
=Use Dynamo to run nohup=
 +
 +
There is a convenience object to initiate nohup processes from Dynamo itself. The syntax is
 +
 +
<tt>mbsys.nohup.launch('myScript')</tt>
 +
 +
The output can be directed to an object:
 +
 +
<tt>nh = mbsys.nohup.launch('myScript')</tt>
 +
 +
where <tt>nh</tt> gathers the info of the process (PID number, output file, etc), at includes some utilities for typical functionalities (killing the process, checking its status, etc).

Latest revision as of 10:45, 19 November 2020

This information refers to Linux and Mac operative systems.

The nohup command

The nohup command allows to run a job in the background, in a fashion that interruptions of the connection will not terminate the job. The terminal containing the job might get closed and the job will still carry on. In order to keep track of the PID (process identifier) assigned by the process to the command launched, remember to use the & at the end of the line that invokes nohup, this will force the system to show the PID after launching the job. This PID might be needed to interrupt the execution of the background job.

Killing a background process

Use the command kill -9 followed by the PID of the process that you want to kill. For instance, for a job with PID 12657, write:

kill -9 12657 

Retrieving the PID of your process

You can use the pgrep command, followed by the command that you nohup-ed. For instance:

pgrep MATLAB

to locate PIDs that carry a MATLAB process and that are ran by nohup. Note that in your system, capital letters might not apply.

More generally,

ps -a

will list all user jobs currently under execution.

Running Matlab scripts

This informations refers only to Matlab scripts defined by the user. For 'Dynamo' projects, see below.

With a license

If you want to run the script myTest.m through nohup, the advised syntax is:

nohup -nosplash matlab < myTest.m > output.txt & 

where

  • no splash inhibited the popup of the Matlab welcome window
  • output.txt is a text file that will catch the text that would be shown in the screen if the script would be ran directly (instead of through nohup).

With the standalone

You can replace matlab with dynamo, and use

nohup dynamo < myTest.m > output.txt & 

in a terminal where you have activated Dynamo previously.

Matlab scripts including Dynamo commands

Remember that you need to include a line that activates 'Dynamo' inside the script that you submit into nohup. This is not necessary if you use the standalone with nohup dynamo < myTest.m > output.txt &

Running projects in the background

'Dynamo' projects can be run directly by nohup. A project script myProject.exe can be ran by

nohup myProject.exe

Use Dynamo to run nohup

There is a convenience object to initiate nohup processes from Dynamo itself. The syntax is

mbsys.nohup.launch('myScript')

The output can be directed to an object:

nh = mbsys.nohup.launch('myScript')

where nh gathers the info of the process (PID number, output file, etc), at includes some utilities for typical functionalities (killing the process, checking its status, etc).