Display Process Information¶
The ‘ps’ command¶
The output of ps will indicate the name of the program, its
process I.D., and the amount of CPU time it has consumed so far.
To display information about a specific user’s processes:
ps -U USERNAMEwhere USERNAME is your username
To display information about a specific running program:
ps -ef | grep PROGRAMwhere PROGRAM is the program’s name
See man ps for additional information.
Killing Processes¶
To kill a running program, type:
kill PIDwhere PID is the process I.D. obtained by running ps.
If the first form does not kill your process, try
kill -9 PIDTo kill all processes in your current process group (including your shell session), execute:
kill 0Note: This is rarely needed and will terminate your shell session. To kill
only background jobs, use jobs -p | xargs kill instead.
Input and Output¶
Most programs initially inherit the current terminal or pseudo-terminal for doing input and output. This means that these programs have a ‘controlling terminal’. The ‘controlling terminal’, and therefore all output, is lost if you logout before your background job completes. To avoid this loss, you should either code your program to read all input and write all output to specific files, or your should redirect input and output for the program by using the shell operators: <, >, >>, and >&.
It is recommended that you redirect standard output and standard error to a log file for all programs run in the background. For example:
myprog > logfile 2>&1 &will run myprog in the background, redirecting all output to the file
logfile in the current directory. See man bash for details on shell
redirection operators.
Hints to Improve Efficiency¶
Some people run 2-hour CPU-time jobs only to discover afterwords that the program didn’t even do what they wanted. Avoid this. Debug your program using small test cases until you’re sure you’ve got it right. Only then should you run the big monster.
Timing Jobs¶
Two common questions when running big jobs are: How do I find out the running time? and How do I capture the program output which would normally go to the screen?. Here is one simple way to do both (as:
nice -n 19 /usr/bin/time program-name > output-filename 2>&1Where program-name is the name of your program and output-filename is the name of the file in which you want to capture output. The running time will be the last line of the output file, formatted like this:
60.0 real 10.0 user 0.5 sysIn this example, the cpu time used was 10.5 seconds (10.0 user + 0.5 sys) and the elapsed (wall-clock) time was 60.0 seconds. By division, your program used 10.5/60 or just over 1/6th of the available cpu time while it ran.
Running sequential jobs¶
If your programs are ‘prog1’, ‘prog2’ and ‘prog3’, you can run them in background via the shell command:
(prog1 ; prog2 ; prog3) > log 2>&1 &Another way is to use a semicolon:
run1 > run1.log 2>&1 ; run2 > run2.log 2>&1where run1 and run2 are the programs you wish to run and run1.log and run2.log are the logfiles.
Yet another way is to set up a shell script file, for example
run_all, containing:
#!/bin/bash
run1 > run1.log 2>&1
run2 > run2.log 2>&1The > file 2>&1 syntax redirects both standard output and standard error
to the specified file.
Then from the unix prompt:
chmod +x run_allto allow the script to be executable, and then type:
./run_allto run the script. You could also type:
./run_all &to have it run in the background.
Scheduling Jobs¶
The at and batch commands allow the system to queue up big
jobs and run them at a later time. at allows you to specify when
the commands should be executed, while jobs queued with batch will
execute as soon as the system load level permits. These commands provide
a mechanism for big jobs to run without slowing down interactive
response and interfering with other people trying to use the computer.
Queue Execution¶
To use at or batch, create a script file which contains the
unix commands you want to run. Suppose your script file is called
‘filename’. To run it in batch, type the command:
batch filenameTo run the script at a specific time, use:
at time date filenamewhere time is in the form 0815, 0815am, 8:15am, now, and 5 pm;
and date is in the form Jan 24, Friday, tomorrow, and today.
If you leave out the date field, the date will default to today.
The computer will respond:
job N at <full date>where ‘N’ is the job number it creates. When the job finishes, it will mail you the output of the script, unless output was redirected. (see below)
Shell Features¶
By default, /bin/bash is used as the shell interpreter for the commands in your script.
If the commands in your script file need any input, create separate input files which contain the necessary input and use the ‘<’ shell feature in the script file. To redirect the output of a particular command in your script, use the ‘>’ shell feature. For example, your script file might contain the line:
proga < inputa > outputaThis would cause the program ‘proga’ to take its input from the file ‘inputa’ and send output to ‘outputa’.
Controlling jobs¶
To find out the status of your jobs, type the command:
at -lThis will report both ‘batch’ and ‘at’ jobs. If ‘N’ is the job number reported by ‘at -l’ then the command:
at -r Nwill remove that job from the queue (whether or not it is already running) and interrupt it (if it is already running).