LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   If I run a script in the background, where does it's output go? (https://www.linuxquestions.org/questions/linux-general-1/if-i-run-a-script-in-the-background-where-does-its-output-go-4175607162/)

wh33t 06-02-2017 02:19 AM

If I run a script in the background, where does it's output go?
 
I have a PHP script that I intend to run in the background. Each tick it outputs a myriad of things it may or may not do. I'm curious where all of it's output goes when I send it to the background? Ideally I'd like to be able to retrieve it for monitoring/debugging and send it back into the background when the time calls for it. When I retrieve it I'd like to be able to see the last few hundred lines of output.

Any info/tips greatly appreciated. Thank you!

Turbocapitalist 06-02-2017 03:12 AM

If you run the script in the background the output will still go to stdout and stderr. If you close the terminal it is running in, taking advantage of nohup, then all that disappears even though the script is still running. If you are running as a cron job then it will be captured and mailed to you.

One way to be able to come back and check on the status or results would be to run your script in a terminal multiplexer like tmux. Inside tmux, you'd just run the script and then disconnect or whatever. Then you could later reattach to the tmux session and all your output would still be there. You might have to scroll back in the buffer using "ctr-b [" to see past the top of the screen.

See some tmux tutorials and the manual page for an introduction.

wh33t 06-02-2017 03:27 AM

Quote:

Originally Posted by Turbocapitalist (Post 5718182)
If you run the script in the background the output will still go to stdout and stderr. If you close the terminal it is running in, taking advantage of nohup

nohup was my plan. I was just curious if it wasted ram or harddrive space dumping out text to wherever it goes. Does it?

Turbocapitalist 06-02-2017 03:33 AM

If you're doing nohup, then you'll need to use redirection for stdout and stderr.

Code:

./myscript.php > ~/myscript.out.log 2> ~/myscript.err.log &
Then you can close the terminal and the output will be in those two files, the former stdout and the latter stderr.

Then you'll have to use ps or pgrep or something like that to see if the script is still running. You could follow the output in another terminal window using tail

Code:

tail -f ~/myscript.out.log
But if you don't go out of your way to save the output it is just gone, using no space or other resources.

wh33t 06-02-2017 03:35 AM

Awesome! You've been a great help! Thank you!

Turbocapitalist 06-02-2017 03:45 AM

No problem. Though I just remembered that if you are on a distro afflicted with systemd, you might or might not have to do some weird systemd-related tricks to allow the above.

ondoho 06-04-2017 01:55 AM

when i start an xsession, i can redirect its output to a logfile:
Code:

exec startx > ~/X.log
and i find most commands' outputs there.


All times are GMT -5. The time now is 07:43 AM.