gus
gus

Reputation: 365

Leaving a process running using screen without sleeping

I connect to a server using ssh, and then created a screen using the "screen" command. Then I run a Matlab script using: "matlab < NameofFile.m", and Matlab starts running the script.

I log out the screen (with Ctl+a+d) and disconnect from the server. Then, when I connect to the server again and check the screen I created appears that Matlab is runnig, but when I use the command "top", the information provided indicates that the process I left running in the screen is in sleep mode (S).

How can I leave a process running using a screen in such a way that when I leave the screen, the process is properly running (ie, indicates "R" when I use the command "top")?

Thanks in advance for any help you could provide.

Upvotes: 1

Views: 1263

Answers (1)

bishop
bishop

Reputation: 39434

See thread for discussion, answered here for posterity.

Sleep is a normal process state, occurring whenever a process waits for something (usually I/O). The process remains in the ready queue and will run when it's done waiting. So I'm not surprised your process sleeping.

That said, your script could be completely CPU-bound, like: ( while :; do :; done ) & ps e $! -- which will always show R. Completely CPU-bound processes are extremely rare -- they really have to output something at some point to be useful, and such I/O will move them to sleeping. Moreover, even if they don't output for a long time, there are usually other processes vying for the CPU, which the kernel will force with preemption. (Unless you have a very specific nice level or a very specific kernel configuration that lets your non-output generating CPU script completely hog the CPU. Which is a really, really rare scenario.)

If you want to better monitor the state changes, you can use top -d .01 -pPID (replace PID with the actual process ID). That let's you gauge if it's sleeping for a long time or hitting the CPU in contention with another process. If you have prstat or latencytop commands, you can look at the LAT for each (eg, prstat -m 2), which will tell you how much contention that process experiences. The closer to 0, the less contention.

Answering the general question of "How can I leave a process running", one could do: nohup matlab < script.m &. That keeps the process alive even after terminal disconnect. But using screen means you don't need to do that, because screen manages the terminal session, and as far as matlab knows, the screen is still connected.

Upvotes: 1

Related Questions