Hi Folks,
In this post we talk about how to view the output of a background process while it is running in Linux. Why is this important? It could come in handy if you need to troubleshoot problems with a program that was started in the background by another program, especially when you’re dealing with third-party software where you can’t easily set up logging as you wish. The way to do this is to use the proc system.
First, find the process id. You can do this by running the following command
ps aux | grep "your program name"
This command will list all processes that match the name of the program you supply. You will also see the id of each process. Once you find the id of the process your program is running under, then use the tail command and proc system to access its output. Run a command like
tail -f /proc/<process id>/fd/1
Note that 1 represents stdout while 2 represents stderr. The tail command displays the last part of a file. The -f flag tells tail not to stop when the end of the file is reached, but to wait for more data to be appended to the input. This gives a streaming effect.
In case tail doesn’t work, you can use the cat command to view all contents of the file by running a command like
cat /proc/<process id>/fd/1
That’s all for now. Till next time, happy software development.
Resources
The /proc Filesystem. The Linux Kernel. https://www.kernel.org/doc/html/latest/filesystems/proc.html#proc-pid-fd-list-of-symlinks-to-open-files