Question:
How do I run multiple commands on a Unix/Linux system without a GUI?
Brendan R
2008-07-02 00:17:50 UTC
I have a gentoo linux system with no GUI. I want to compile stuff, and while it's running, execute more stuff at the command line. How can I give it one command, then give it more commands without installing a GUI (running multiple terminals) or using SSH?
Five answers:
Rav
2008-07-02 01:02:42 UTC
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.
2016-05-27 01:34:28 UTC
Hello, Tux. "People" assume that everybody in the world uses Windows XP or Vista, depending on which one they use. A few "Other People" assume that half the world uses osX and the other half uses older Macs and Windows, but that you can tell they use a Mac. One in ten people is smart enough to know that there are many versions of Windows and MacOS, and sometimes even recognize Unix/Linux systems like Ubuntu and Fedora. In fact, some people assume that the problem doesn't relate to their operating system at all but is universal to all computers. Some people even assume that "everything is possible" if a nerd does it so they ask anyway.
zephyrwind
2008-07-02 00:23:40 UTC
The way to make a task run in the background is to add a "&" at the end of the line.



(stupid example)



javac hugecompile.java &



This will compile in the background! If you need to kill the task, you'll have to use the kill command with the PID.
Andy T
2008-07-02 00:24:44 UTC
Try to run the command with multitasking symbol, that is the ampersand symbol.



So things like 'grep "hi" big.txt' becomes 'grep "hi" big.txt &'
koppe74
2008-07-02 01:49:07 UTC
Several ways...



For one, you have several virtual terminals. Just press Ctrl-Alt (you could use just Alt, but if you ever install X, you need Ctrl-Alt to leave X, so better get used to Ctrl-Alt from the beginning) and one of the keys between F1 and F6. Odds are that you're in F1, X is usually on F7, and on F2 through F6 are five *other* (than F1) virtual terminals you can log into.



+++



If you don't want to log-in again -- and you're *not* running something interactive -- you can run the command in the background.



To start a command in the background, follow the name with a &:



$ g++ -o myprog myprog.cc &



(Note: If the program suddenly need keyboard input, it will stop until you put it into the foreground and give it some input.)



Then you can start some other program, while the first one (or several others) is working in the background:



$ nano myfile.txt



(Programs like nano are interactive, so they're no use in the background)



If you ever install X, know that most X-clients (X-programs) can be started in a terminal and put in the background without problem. After all, they recieves input from windows they draw, and not from the terminal.



+++



Some commands will write status-messages to the screen after being put into the background. This will clutter-up the screen for any foreground programs. But you can redirect this output to a file (for reviewing later) or to /dev/null (to just get rid of it). An additional stream is used to print error-messages, but for many programs these too may be redirected.



To redirect output from a background program to a file:

$ g++ -o myprog myprog.cc > /tmp/compile.log &

To redirect output to the black hole /dev/null:

$ g++ -o myprog myprog.cc > /dev/null &

To throw away output, but save serious errors:

$ g++ -o myprog myprog.cc > /dev/null 2> /tmp/crror.log &

To merge output and error and throw both away (for bash-shell):

$ g++ -o myprog myprog.cc >& /dev/null &



+++



If you have started the program -- or some program; then Ctrl-Z will stop and suspend it. You can then start an other program (and remember to put it into the background), or you can type in 'bg' to put it into the background (similar to starting it with an &).



$ g++ -o myprog myprog.cc

Ctrl-Z

$ bg

...is the same as:

$ g++ -o myprog myprog.cc &



The command 'jobs' lists all processes stopped or running in the shell. % (eg. %3) is the job-number the process have (in the current shell). "+" is a shorthand for "current process" (last) and "-" is a shorthand for "previous process" (one before last). The job-number is used with 'fg' and 'bg'; without the job-number, the + process is assumed.



You can also continue a program that was stopped in the foreground with the command 'fg':

$ nano

Ctrl-Z (stopps nano)

$ g++ -o myprog myprog.cc & (starts compiler in background)

$ jobs

===> %1 - nano *Stopped*

===> %2 + gcc &

$ fg %1 (continues 'nano' in foreground)



+++



Often you don't need background-programs to run at full speed, so to save more processing power for your other (foreground) programs, you can run the background processes at lower priority:



$ nice -n 15 g++ -o myprog myprog.cc &

(Runs with quite low priority)



To make a background-process surviving log-out (but *not* computer shut-down), use nohup:



$ nohup g++ -o myprog myprog.cc &

$ exit

(Output is saved to a file)



+++



As mentioned, this will not work with interactive programs (like pine, nano, tin, emacs). A program called 'screen'; allows you to "multiplex" one virtual-terminal/xterm/serial-line, creating several "windows" that each may have a program and let you switch between them. You can also "disconnect" screen and let the programs run although you're not logged on... and later you can reconnect from the same or another vt/xterm/serial-line.



$ screen



Ctrl-A C to create new window

Ctrl-A [Space] to switch to next window

Ctrl-A A to switch to last visited window

Ctrl-A D to disconnect screen



$ screen -r (to resume a disconnected screen-session)



$ screen -D -r (to disconnect and log out one place, and re-connect the session "here")



A similar program for just running one interactive command is 'disconnect', a program found in the 'expect' package for the Tcl/Tk language. 'disconnect' is really just an expect-script. It works well with interactive-programs that need to be in the foreground but can "run by themselves" for a while (like mp3blaster). You may the "re-connect" the program later (somewhere else). Ctrl-] is used to "escape" the program you're running, so you can "talk" to 'disconnect' itself.



$ disconnect mp3blaster (Starts the program)



Ctrl-] (Escapes to the 'disconnect' program)

Ctrl-D ("Disconnects" 'disconnect'... mp3blaster still runs)



$ disconnect





(You may need to make the disconnected program "redraw" itself. For may terminal-based programs, this is done by Ctrl-L.)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...