LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 06-02-2017, 02:19 AM   #1
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 921

Rep: Reputation: 61
Post 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!
 
Old 06-02-2017, 03:12 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,258
Blog Entries: 3

Rep: Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713
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.
 
2 members found this post helpful.
Old 06-02-2017, 03:27 AM   #3
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 921

Original Poster
Rep: Reputation: 61
Quote:
Originally Posted by Turbocapitalist View Post
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?
 
Old 06-02-2017, 03:33 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,258
Blog Entries: 3

Rep: Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713
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.
 
1 members found this post helpful.
Old 06-02-2017, 03:35 AM   #5
wh33t
Member
 
Registered: Oct 2003
Location: Canada
Posts: 921

Original Poster
Rep: Reputation: 61
Awesome! You've been a great help! Thank you!
 
Old 06-02-2017, 03:45 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,258
Blog Entries: 3

Rep: Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713
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.
 
Old 06-04-2017, 01:55 AM   #7
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
trying to run a script in the background redirect the output to a file jdwalk Linux - Newbie 7 10-30-2019 10:17 AM
Run a script in background mithun119 Linux - Newbie 7 10-15-2014 03:51 AM
run script in background MikeQ Linux - Newbie 2 09-11-2009 06:21 PM
How to run a shell script in a background? anandv_1234 Linux - Newbie 3 12-23-2007 03:03 AM
how do i get a shell script to run in the background? tjyorkshire Programming 4 07-06-2007 02:28 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration