[SOLVED] If I run a script in the background, where does it's output go?
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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?
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.