0

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?

3 Answers 3

0

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.

1
  • 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
    Jul 19, 2013 at 19:22
0

Try:

$(screen -S TOP -D -m top -b -p $PID > top.log) &
1
  • Thanks. I had the same issue with the redirection where the file was created but the redirection did not work.
    – oche
    Jul 19, 2013 at 19:15
0

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.

1
  • 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
    Jul 19, 2013 at 19:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .