sleep 7 subsequent commands will not be processed until 7 seconds elapses
Waiting for a process to finish is referred to as executing a command in the foreground. The solution is to execute the command in the background using ampersand (&):
sleep 50 & Ampersand tells the sleep program to run in the background, returning you with a fresh Unix prompt to enter more commands into the same shell.
[1] 48143 output specifies this is your 1st job with process ID (PID) 48143
You are free to enter other commands while your job runs in the background:
ls
jobs lists your running jobs (job 1)
[1] + Running sleep 50
ps lists PIDs of running processes or shells
27229 ttys002 0:00.08 -csh
43716 ttys003 0:00.09 -csh
48172 ttys003 0:00.00 sleep 50
The shell will return a message when your background job encounters an error or terminates normally. The following line will print to the screen either in real time or the next time you enter a command:
[1] Done sleep 50
(Then hit return once or twice to get a Unix prompt on a new line.)
sleep 300 If you forget an ampersand..
Control-C ^c is the Unix kill signal for foreground processes.
sleep 3000 & How do we kill a given background process?
[1] 48192
sleep 4000 &
[2] 48193
ps
PID TTY TIME CMD
27228 ttys000 0:00.03 -csh
48192 ttys003 0:00.00 sleep 3000
48193 ttys003 0:00.00 sleep 4000
Note how jobs 1 and 2 are calculations running in parallel. For multiple processes on a single CPU, it alternates clock cycles for each process with equal priority.
kill %2 kill will usually terminate a process (job 2).
[2] Terminated sleep 4000
kill -9 <PID> the -9 flag using your PID is more forceful if the above doesn't work.
[1] Killed sleep 3000
ps
On a Mac, top shows which process are currently using the most CPU or memory:
top what's at the top of your list?!
Control-C ^c when you're ready to exit to the shell prompt.
The kill command is equivalent to the Force Quit.. function on Mac. To see just how many background processes your OS uses, enter:
ps -ef
It is good practice to perform a complete Shutdown or Restart on your computer once a month so that any hanging processes get terminated and your working memory is cleared. This is different than simply closing the lid of your laptop, which just puts the OS to sleep and holds all your programs open in RAM memory.
______________________________________________________________________________
A serial calculation can be made with a single command line by stringing subsequent commands together using semicolon (;) to perform them one after another:
sleep 2; abc; sleep 5; echo def & (your syntax may require a space after each semicolon)
The abc command does not exist. The fact that it takes 3 seconds before the error message is printed confirms that the shell performed the calculation in serial rather than in parallel.
Unix is popular with multi-user computer systems (remote client) because one can run in a job in the background, log out of the machine, and the job will still run to completion. In contrast, a process run in the foreground will cease the moment you close your terminal or log out of your account.
Scripting is writing a useful sequence of commands to automate operations on a set of input files. It is standard practice to write output into a file rather than printing to a terminal because you don't want random output popping up on the screen while working on other things. The greater than symbol (>) allows for standard output (stdout) redirection into a text file:
echo job done > job.out
l
cat job.out confirms the output of the echo command as written to a file
If your job encounters any errors they will be sent as standard error (stderr) rather than stdout, meaning errors normally get printed to the screen rather than your output file. If you want the errors stored in your file, the syntax to redirect stdout and stderr to a file the syntax is >&:
abc >& err.out
What if we need to rerun a job?
echo job redone > job.out
job.out: File exists.
cat job.out
l
The shell aborts the command and complains because > redirection expects to create a new file. There are two ways around this. One is to tell the shell to force the overwrite of the existing file using >!:
echo REDONE >! job.out
cat job.out
The last alternative is to append the output (>>) to whatever lines are already contained in the file:
echo thrice >> job.out
cat job.out
It is best practice to name separate input and output files each time you run a program:
echo run2.inp > run2.out
QUESTION 6: Of the output from the following command sequence, which lines are output into s.out by the time exactly 14 seconds has elapsed? Explain the meaning of each punctuation mark to the shell interpreter.
(sleep 10; date; sleep 3; date; sleep 3; date) > s.out & <enter>
tail -f s.out enter this immediately after the first command
Tail is like the cat command but follows data as it is appended to the file. Note that the parentheses above were needed to group the output from all commands into a single file. When you're done viewing all the output hit:
ctrl-c to exit the tail command and return to the Unix prompt use the universal kill signal (^c)