7/21/2008

Run multiple commands on a Unix/Linux system without a GUI

How to run multiple commands on a Unix/Linux system without a GUI?

Try to use screen (if it is installed on your system -- read man 1 screen).

You can
- create a new "window" with Ctrl-A c
- kill a "windows" with Ctrl-A k
- switch between "windows" with Ctrl-A and Ctrl-A
- you can detach the screen (run in background -- you can even logout -- your jobs will still run): Ctrl-A d
- reattaching:
$ screen -r

That's only for starters. Read the built-in help: Ctrl-A ? and the manual for more information.

Of course you can work with background jobs too, but it will be a quite demanding task when you are juggling between 4 or 5 jobs.
It gets even more complicated when you're using && and || operators or redirections.

You can send your job to the background with either "&" at the end of the line or just by pressing Ctrl-Z (stop) and the executing "bg" command (check your shells manual)
You can "retrieve" a background job by issuing the fg command.

Example:
- start 2 long running jobs:

$ ./compile &
$ ./update_doc &

The & operator marks the end of line, so you could actually use single line instead:

$ ./compile & ./update_doc &


- check the job list:

$ jobs

[1]- Running ./compile &
[2]+ Running ./update_doc &

$ "foreground" the first job and send it back to background

$ fg %1
Ctrl-Z
$ bg

kill the second job:

$ kill %2
[2]+ Terminated ./update_doc

Believe me -- you'll prefer screen ;-)

Edit:
yet another solution -- if you're an Emacs user -- just start your editor, M-X shell, voila! I know some hard-core Emacs users who actually _never_ leave their editor.

0 comments: