Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

To run my Matlab scripts, I've created a shell script to which I give two parameters - the path to the matlab file ($1) and to the log file ($2):

nohup time matlab -some_parameters -r "run $1;exit" &>> "$2" &

When I need to kill one of the Matlab processes, it's sometimes difficult to tell which one is which. Would it be possible to somehow include the pid of the Matlab process in the log file (i.e. in $2)?

share|improve this question

3 Answers 3

There are several ways of marking the instances of a process. You can do that via the command name in the process list (e.g. matlab_1 instead of matlab) or via an environment variable. Using the PID is possible, too. You just have to output it before matlab is started:

nohup time bash -c 'echo $$; exec matlab -some_parameters -r "run '"$1"';exit"' &>> "$2" &

Edit 1:

$$ is the PID of the shell but due to the exec the matlab process just overwrites the shell process i.e. the PID doesn't change. exec means that at the end of the called program there is no jump back to the shell; because there is no shell any more.

For an explanation of the other options I mentioned see my answer to this question.

share|improve this answer
    
Using the $$ strategy, wouldn't it give me the pid of the shell instead of the pid of the Matlab process? –  John Manak May 16 '13 at 12:52
    
Could you please also elaborate a bit on the other two options? Thanks! –  John Manak May 16 '13 at 12:57
    
@JohnManak See my edit. –  Hauke Laging May 16 '13 at 13:07
    
Thanks but unfortunately the numbers still slightly differ. I thought it might be Matlab spanning another process, so I've changed the parameters to -r "$1;exit" but it doesn't help. –  John Manak May 16 '13 at 13:29
    
@JohnManak I made a quoting mistake. The bash sees $1 in its command but does not have parameters (resulting in -r run ;exit). Try the corrected version. You may use strace -f matlab ... for checking whether matlab forks. –  Hauke Laging May 16 '13 at 13:38
up vote 1 down vote accepted

In the end, it seems that the Matlab command is subsequently spanning other processes (JVM) when called. However, there is an undocumented function feature that returns the PID of the running Matlab process:

nohup time matlabR2012b -nodesktop -nosplash -nodisplay \
  -r "fprintf('PID: %s\n', num2str(feature('getpid')));run $1; exit" &> "$2" &
share|improve this answer

There is a way to get the pid but setting it as the first line could be a bit tricky since as soon as the process starts, it'll start writing to the log file.

Assuming that the goal here is not to set the first line as pid but to know the pid in order to kill the process, as soon as you have done nohup, you can save the pid of the command in a pid file and kill using it.

nohup some_command some_parameters &
echo $! > matlab_program_1.pid
kill `cat matlab_program_1.pid`
share|improve this answer
2  
That is oversimplified. If you make some_command into time matlab you would get the process id of the time from $! not of matlab –  Anthon May 16 '13 at 12:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.