Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

This is my first question ever on Stack Exchange! Ive been using it for years and always seem to find my solution, except for this one.

I am trying to get top to run in a seperate screen so I can monitor the performance of a single process in the background. I have named the screen instance to enable me easily attach to it when I need to. I have put all this in a script as it is all part of a larger monitoring experiment i'm trying to run, it goes something like this.

screen -S TOP -D -m top -b -p $PID > top.log &

What this should do is that it should create a new screen session called top, then detach from it on creation and run the top command such that creates a binary file top.log and put it in the background.

Everything works fine except the file is created but is not populated with any top data, whereas when I run the command without screen it works fine.It looks like screen isnt regarding the redirection as part of the top command but is creating the file anyways, maybe as part of a separate command.

For now, I've defaulted to using nohup to run top in the background, but I would very much like to be able to attach to the screen.

Any ideas?

share|improve this question

3 Answers 3

up vote 0 down vote accepted

Your redirection will only refer to the output of the screen command itself, not the output in the screen terminal. I suggest you create a special screenrc file for this screen instance with the following contents:

session top -b -p $MYPID
sessionname TOP
logfile /path/to/top.log
log on
detach

The environment variable MYPID must be set before invoking the screen command. You then invoke screen using the -c option and the name of the screenrc file:

MYPID=$PID screen -c /path/to/top-screenrc

The screenrc file contains screen commands as explained in the man page. It first creates a new session with top, sets a session name, defines a logfile for it, then starts logging. As the last step, the session is detached and the command returns to the prompt. You will not have to send it in the background explicitly (i.e. appending & should not be necessary).

I hope this matches your problem.

share|improve this answer
    
Thanks. This is very similar to what I did for another process I needed to monitor. Matter of fact I wish I had seen this earlier, would have saved me a couple of hours of searching. –  oche omobamibo Jul 19 '13 at 19:22

Try:

$(screen -S TOP -D -m top -b -p $PID > top.log) &
share|improve this answer
    
Thanks. I had the same issue with the redirection where the file was created but the redirection did not work. –  oche omobamibo Jul 19 '13 at 19:15

It's not screen, but rather the shell (bash, I presume) that redirects output. The command line provided will redirect output from the screen program, rather than the screeen session named TOP:

screen -S TOP -D -m top -b -p $PID > top.log &

I'd try this next:

screen -S TOP -D -m " top -b -p $PID > top.log " &

You might need to make the top command into a one-line script file if screen treats the section in quotes as a single command or file name and fails.

share|improve this answer
    
Thanks, I had tried this but no joy. It sees the entire quoted command as one non-existent command. Maybe turning it into a one-liner would work, but Ive decided to stick to using nohup. This command is already part of a larger script, which is also called by another script. I dont want to have too many scripts and then not be able to make sense of it all in a few months. –  oche omobamibo Jul 19 '13 at 19:21

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.