This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new Linux.com!

Linux.com

Feature

CLI magic: background foreground suspend

By Joe Barr on March 08, 2004 (8:00:00 AM)

Share    Print    Comments   

This week, you lame and laggardly louts, we're going to "Bravely go where no Noobies have gone before." We'll fly into the background from the command line interface, and return! But before we do, some good news. It seems that the professional guidance I reported last week may not have been correct after all. There really are some noobies out there who want to learn a little something about the CLI. At least, there is a new book for them. It's from O'Reilly. It's called the "Linux Pocket Guide" by Daniel Barrett. O'Reilly describes it as "a useful alternative for new and experienced Linux users who need a quick and handy means to look up Linux commands." Imagine that, and for normal users to boot. Wish I had thought of something like that and proposed it to a publisher.

Here's the background

Last week's column included a series of commands which would cause it to speak the current time, wait 60 seconds, and then repeat. The command looked like this:

while true; do saytime; sleep 60; done &

See the ampersand? That's the "&" thingie at the end of the line. It puts the task run by the commands above into the background, out of your way. That frees up the CLI so that you can do other things from there while the commands above are running. It's the magic of multitasking, which is something that Linux excels at.

In the original version of the column last week I made a mistake by not bringing the task back to the foreground before terminating it with CTL-C. Luckily, a sharp-eyed reader pointed that out and I corrected the text by insisting that you type the "fg" command before entering CTL-C. The "fg" brings the task back to the foreground where the CTL-C can act on it.

You're suspended!

If you enter the commands shown above without the ampersand, it still runs. But you can't do anything else from that console while it does, because the task is running in the foreground. But if you put the task asleep, you can then put it in the background. Here's how. Enter CTL-Z to suspend the task. Then enter the "bg" command to move it to the background. It's as easy as that.

You can have more than one task running in the background. It takes a little more work to get them front and center in the foreground again when you do, but not much more. Let's start three tasks running in the background. Enter each of the three lines of commands shown below:

while true; do saytime; sleep 60; done &
while true; do saytime; sleep 120; done &
while true; do saytime; sleep 180; done &

Now type "jobs" and hit enter. You should see something like this:

wart@libranet:~$ jobs
[1] Running while true; do
saytime; sleep 60;
done &
[2]- Running while true; do
saytime; sleep 120;
done &
[3]+ Running while true; do
saytime; sleep 180;
done &

To bring any of those 3 jobs to the foreground, all you need to do is enter "fg X" where X is 1, 2, or 3. Nothing to it. Let's bring the first job up front, then suspend it and see what our jobs list looks like then. Enter "fg 1" and then a CTL-Z. Then type "jobs" again. You should see something like this:

wart@libranet:~$ jobs
[1] Stopped while true; do
saytime; sleep 60;
done &
[2]- Running while true; do
saytime; sleep 120;
done &
[3]+ Running while true; do
saytime; sleep 180;
done &

As you can see from the output above, the first job (the one with the [1] in front of it) is stopped. When a task is stopped it is frozen in time - suspended, not running - until you do something with it. You can start it running again by putting it back into the background or bringing it to the foreground. Use "bg 1" or "fg 1" depending on which you want to do.

We better stop here before the Linux User Rating Cops catch us and move you up out of the noobie category. If anyone asks, just tell them that yes, you did try the CLI, but you didn't inhale. And speaking of noobies, did you realize that we've been doing a little programming the past couple of weeks? Our saytime commands could just as easily be a shell script as a command line entry. But that's a topic for another day.

Share    Print    Comments   

Comments

on CLI magic: background foreground suspend

There are no comments attached to this item.

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya