0

Is there a way to view the console outputs of a particular script that is running in the background on a machine?

Maybe using the pid or something similar.

For example, to view the output of this script

script.pm

while (1) {
  print "OUTPUT\n"
}

Example Usage

-> ps ax | grep script.pm

<- 1234 ? S 0:05 /var/lib/script.pm

-> monitor 1234 #this is a fake command

<- OUTPUT

<- OUTPUT

<- OUTPUT

<- OUTPUT

<- OUTPUT

Ankur S
  • 1,218
  • 3
    Why not just redirect the output of the background script to a file and then monitor the output of the file? – Raman Sailopal Apr 23 '18 at 14:58
  • it's for a mojo server spread across many files in deployment and I'm trying to see at any level where errors are occurring, or just general diagnostic output. @RamanSailopal – Bigbob556677 Apr 23 '18 at 15:00
  • 2
    I agree with @RamanSailopal, you'll want to ship STDOUT and STDERR from your application to a log file, and then up to a log ingestion server that can process and query the logs from all the different application servers. Look into the ELK stack or Graylog. – BoomShadow Apr 23 '18 at 15:05
  • maybe GNU Screen can help. Also, take a look at this question – Ankur S Apr 23 '18 at 15:11

2 Answers2

0

If the process is running in the background, you can move it to the foreground of your current shell with the fg command. The processes output will then start writing to your shell.

If you want to send the process back to the background, you can press CTRL+Z to suspend the process, then the bg command to send it back to the background.

Usage:
fg PID
bg PID

Peschke
  • 4,148
0

Is there a way to view the console outputs of a particular script that is running in the background on a machine?

There is now, via a brand new utility called pw (Pipe Watch).

How it would go is like this. We redirect the output of the script to pw, and put it into the background:

$ script.pm | pw &
[1] 1234

Now both the script and pw are executing in the background. pw is reading the output. It is aware that it is executing as a member of a background process group of a job control shell, and consequently refrains from trying to display anything on the terminal.

Then, at any time we feel like it, we do this:

$ fg

to bring the job to the foreground. Now pw activates its interactive display, which is being refreshed with the script's output.

When we don't want to look at that any more, then we can use CtrlZ to instruct pw to suspend itself, and then bg command will resume the background execution.

Kaz
  • 8,273