This is the way to get a lock: system('exec gpu_lock.py --id') As used by: obtain_gpu_lock_id.m Matlab's system command spawns a shell process. Without the use of exec, the lock would be freed when the shell terminates, which is immediately after gpu_lock.py returns. The exec means that gpu_lock.py sees Matlab as the parent process. It is tempting to do: system('gpu_lock.py --id $PPID') but this doesn't work in Matlab if the user's login shell is set to /bin/tcsh (it would work in Octave, which always uses /bin/sh).