Use & at the end of the command. This puts the job in the background. For example:
./myjob 10 &
To see all your background jobs (the ones started in the current shell), enter:
jobs
To bring one of these to the foreground, use:
fg %n
where n is the job number (yes it needs a % in front of it). To move it to the background again, hit CTRL-Z. This however doesn't just move the job to the background, it suspends it. To get it running again, use
bg %n
Use nohup. For example, suppose you want to run ./myjob 20. In this case, you could use:
nohup nice ./myjob 20 > out_file &
There are several things to note here:
The & at the end of the command line is there so you get the command prompt back immediately (see the previous section).
We have redirected the output to a file out_file with > out_file. If you don't do this, any output to STDIN (normally the console) will automatically go into a file called nohup.out.
We've niced the job to reduce it's execution priority. This is courteous .
If you want to see what's in nohup.out (or whatever you called it) at any time, you can do the usual
cat nohup.out
Here's another potentially more useful command:
tail -f nohup.out
tail shows you the last few lines of the files. In addition, the -f flag ("follow") means that as lines get added to the file, they are displayed on the screen. This looks just like when you run the program without nohup, except when you hit CTRL-C, the program keeps on running in the background.
One nice refinement is to have the output mailed to you on completion. Try this:
nohup nice ./longjob job_options ; cat nohup.out | mail $USER &
First find the PID (process ID) with top or with ps. For example, I might list all jobs belonging to me with:
ps -u eric
To stop the job, send it a STOP signal:
kill -s STOP PID
(replace PID with the actual PID number). It is now no longer running, but it still occupies memory (although the memory it uses will probably be swapped out to disk). When you want to resume execution, give it a CONT signal:
kill -s CONT PID
Suppose you want to run ten jobs overnight. You could just start them all at once (using nohup as described above). However, it would be more efficient to run them sequentially. Besides being faster in total (less multitasking overhead), it is also much more considerate to other users to run your jobs in series instead of in parallel.
An easy way to run things in sequence is to use a script. For example:
#!/bin/bash # This script runs mycalc with arguments 10 through 100. ./mycalc 10 ./mycalc 20 ./mycalc 30 ./mycalc 40 ./mycalc 50 ./mycalc 60 ./mycalc 70 ./mycalc 80 ./mycalc 90 ./mycalc 100 |
If you save this script as docalc, you need to make it executable before you can run it.
chmod +x docalc
You could run it like this
nohup nice ~/docalc &
As an example of the things you can do with bash scripts, the example script in the previous section could also have been written like this:
#!/bin/bash
# This script runs mycalc with arguments 10 through 100.
ARGS="10 20 30 40 50 60 70 80 90 100"
for ARG in $ARGS
do
./mycalc $ARG
done
|
Obviously, if you have a lot of jobs to run, this is easier than writing a script with one line per job.
This just gives you a taste of the possibilities. If you find this kind of thing useful, then you should check out Heiner's SHELLdorado.