Industry guru Dave Taylor offers tech support on technical and business topics, including iPhone, iPod, Microsoft Windows, Sony PSP, cellphones, online advertising, CSS, Web design, business, Unix, Linux, SEO, Mac OS X, and shell script programming.     


My background jobs in Linux have become invisible?

Last night i started downloading all of Fedora core via rsync. It was getting late, so I put it in the background and logged out. i just logged in this morning, and wanted to see where it was at, so I typed "jobs", but surprise, it isn't there. I know it's still running, because the lights on my hub are still blinking, it shows up in "top", and i can see (du -bs fedora/) that the folder is still growing. How do i get the output back on my terminal?


Dave's Answer:

First, the bad news. Once you've disassociated a running application from an input or output device, you can't get it back. Hopefully it's all working without a glitch so that's not a big problem.

Now, let me explain what happened with your Linux process and show you how to avoid this problem in the future.

When you log in to your computer, your shell creates three I/O channels that are then used by all running applications spawned by (that is, launched from within) the shell unless specified otherwise. They are stdin (standard input), stdout (standard output, for all non-error message output), and stderr (standard error, only for error messages).

When you launched the rsync process, it was assigned the same three channels as an ls or who command would have been given. Since rsync doesn't require any input, that was safely ignored, but as you know, stdout was going to your terminal window or screen.

Putting the job into the background, accomplished by the commands ^Z and bg, severed the ties with the three I/O channels, but most likely if the program had any input or output needs, it would have gone into what's called an I/O Block state and output a message like "stopped (waiting for input)" on your screen. Of course, you might have set your login shell to ignore those I/O blocks, in which case output from background jobs just pours onto your screen, regardless of what you're doing.

In either case, once you logged out, the three I/O channels were destroyed, even though they were ostensibly in use by your background job. Instead, the OS typically reassigns the channels to /dev/null, meaning that while rsync keeps chugging along, it can't get any input (not a big deal) but can't produce any visible output either.

Got it?

Now, to avoid this problem in the future, the classic Unix way to push a job into the background is to redirect its important I/O channels along the way (well, its output channels). Here's how I would have done it:

$ nohup rsync args > /tmp/rsync.log &

Then, if you want to watch it working, simply use:

$ tail -f /tmp/rsync.log

and it'll be as if you were running it directly, but it'll survive you logging out and logging in again.

I hope that's helpful for you, and good luck with Fedora Core too. Oh, and one last thought: you might find my best-selling book Teach Yourself Unix in 24 Hours good supplemental reading too.


More Useful Unix and Linux Help Articles:
✔   Copy and Paste from the Mac OS X Command Line?
I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...
✔   Shell script to convert lowercase to title case?
As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...
✔   Can I script renaming files based on an XML data map?
I have a folder full of files which are named with four digits and a file extension e.g. 0312.file and an XML-file describing...
✔   Test for valid numbers in a Bash shell script?
In a different discussion on this site [see Redirecting input in a shell script] a visitor commented that "I was too busy trying...
✔   Review: iSSH for the iPad/iPhone
If you're running an online business like I am, there are times when you need to connect and log in to the server...

Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
    Enter your name: and your email addr:  









Reader Comments To Date: 2

Michael Clark said, on October 17, 2005 1:20 PM:

I like using the screen command to run commands that will take a long time to process. Screen creates a new shell that can be exited without hurting the environment or jobs that are running. simply type "screen" at a prompt. Then start your rsync process like normal. Press Control-A, then the letter D to detach the screen. You can exit your shell but the screen shell keeps running. Later you can log into the shell, and type screen -list to see what shells there are, and screen -r to re-attach. The screen system works great when I have to start a long job from work, go home, and then finish the job.

thirumurugan said, on August 10, 2007 7:55 PM:

please tell me the command to view the running jobs with status.

thiru

Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!

I do have a comment, now that you mention it!











I will never send you any unsolicited email. Ever.






Check This Out Too...

 
Look for Answers
Need Help? Ask Dave Taylor!
Powered By
Linux Journal: Free Issue!


Follow Me on Pinterest

Find Me on Google+
ADT on G+
© 2002 - 2013 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. My lawyer says "Thanks".
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.