3

When you are performing time consuming operations in bash like installing new software, is it possible to write text that will appear at the prompt when the operation has finished.

E.g

Imagine running:

apt-get install eclipse-platform

Then it will use quite a while to finish while you see the installation log, meanwhile I want to create new folders (workspaces) which Eclipse later will use.

Is this possible without opening a new terminal (or tab)?

5 Answers 5

4

You can run the installation in the background. This leaves the foreground free for you to type further commands. You will be notified when the background job has finished.

Check the man page for apt-get and look for options that make it work quietly without writing lots of output. Check for options that make it work without requiring user input (e.g. confirmations)

Use redirection to have apt-get write messages and errors to files, use the ampersand suffix to run the command in background

nohup apt-get --be-quiet --dont-ask \
    install eclipse-platform > agiep.out 2>agiep.err &

You probably dont need nohup (see man page). I invented --be-quiet and -dont-ask so check man apt-get for real equivalents (if any).

See also fg in your shell's man-page.

Note: many of the above features are shell-dependent. Should be OK in bash, ksh and their ilk.

4

If the program in question doesn't consume stdin then you can enter commands followed by Enter and have them executed after the program finishes (and as long as none consume stdin).

1
  • A good command to try this technique with would be one which results in an audible signal.
    – pavium
    May 6, 2011 at 11:11
4

There are many ways to accomplish this.

You can pause a task at anytime by pressing ctrl + z. Then, to resume the task in background, use the command bg. You can bring a task back to foreground with fg

Therefore, if apt-get is running, do ctrl+z , then run bg. this will make it run in background.

You could also read on screen or byobu.

3

One handy alias that comes with ubuntu's .bashrc (I don't know if it also comes in debian) is this one:

# Add an "alert" alias for long running commands.  Use like so:
#   sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

It uses notify-send (desktop environment): With notify-send you can send desktop notifications to the user via a notification daemon from the command line. These notifications can be used to inform the user about an event or display some form of information without getting in the user's way.

You can issue your command in a subshell in the background so you can continue doing things in that shell. Also you can add checks to get different messages if the command went OK or not:

( sleep 5 && alert "sleeped OK" || alert "something nasty happened with sleep") &
3

This is probably a little overkill especially if you're running something like Gnome or KDE where you can have multiple bash windows, but you should look into screen.

Screen lets you run multiple terminal sessions in a single window. It also allows you to quit screen but leave the background processes running and at any later time reattach all those background processes so that you can't even tell you quit.

Basically to start screen you would start the terminal and then type screen . It will give you a message about itself, just press enter to move past it. Screen is a full fledged program so I'm barely going to scratch the surface but here are some basic tasks. This is all assuming the default configurations (which since you haven't used the program should be the same).

There is a set of meta keys that must be pressed before each screen command, otherwise the input goes directly to which ever terminal session is shown. The default meta key is ctrl-a. So for example to make a new window you would first hit ctrl-a then you would hit c

Useful Screen Commands

To create a new window: ctrl-a c

To advance to the next window: ctrl-a n

To go back a window: ctrl-a p

To jump to a window: ctrl-a # where # is the number of the session you want to switch to

To view all open windows: ctrl-a "

To close a window: type exit as normal

To detach a session: ctrl-a d

To reattach a session: When starting screen add the -r option

How to do What You Asked

So to do what you asked then you would follow these steps

  1. Open the terminal

  2. type screen

  3. type apt-get install eclipse-platform

  4. type ctrl-a c

  5. Run your other commands

When your on step 5 your command from step 3 will continue running. To check on it at any time simply type ctrl-a p. To get back to where you were typing your other commands type ctrl-a n. When you are all done you can either quit screen and the terminal completely by typing exit in all your windows. But if you are in the middle of a set of commands and wish to come back to it all later simply detach your session using ctrl-a d. When you want to come back and finish simply start screen with the -r option like so screen -r

Once again this is just a simple example and it doesn't get into everything you can do with screen. But if you're doing a lot of work in the terminal screen can be a great tool.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.