If I use Terminal to initiate a time-consuming process (in this case, deleting a large .sparsebundle file on my Time Capsule, which could take hours) and if I then quit the Terminal app itself before that process has completed, does that ongoing process (the deletion) also quit? Or does Terminal just act as the 'trigger' and the process runs autonomously regardless of whether Terminal.app is active or not?
For the command to continue after closing the Terminal application window, you need to do the following.
- Precede the command with
nohup. This will allow the command to continue after the Terminal application window is closed. This also allows the command to continue after the quitting the Terminal application.
- Redirect standard out and standard error to the same or different files. This gives the output someplace to go, otherwise the default will be use. (See the man page below).
- Append a
&to the end of the command line. This runs the command in the background, otherwise you will not get a command prompt until the command finishes.
For example, if I wanted to use the
find command to search for the file
readgpt and print the results to a Terminal application window, I would execute the command given below. Note: Here, I do not care about the error messages, so I redirected standard error to
find / -name readgpt -print 2>/dev/null
Now, if I wanted to close the Terminal application window and view the results later, I would execute the command below.
nohup find / -name readgpt -print >~/Documents/results.txt 2>/dev/null &
After entering the above, you will get a job number in brackets followed by a process number. An example is given below.
You can get the status of this process by entering the
ps command, as shown below.
Marlin:~ davidanderson$ ps 1456 PID TT STAT TIME COMMAND 1456 s001 U 0:06.83 find / -name readgpt -print
You can terminate this process by entering the
kill command, as shown below.
Marlin:~ davidanderson$ kill 1456 + Terminated: 15 nohup find / -name readgpt -print > ~/Documents/results.txt 2> /dev/null
Note: If you enter the
killcommand in a window that differs from one used to create the process, then there probably will be no output.
The man page for the
nohup command is given below.
NOHUP(1) BSD General Commands Manual NOHUP(1) NAME nohup -- invoke a utility immune to hangups SYNOPSIS nohup [--] utility [arguments] DESCRIPTION The nohup utility invokes utility with its arguments and at this time sets the signal SIGHUP to be ignored. If the standard output is a terminal, the standard output is appended to the file nohup.out in the current directory. If standard error is a terminal, it is directed to the same place as the standard output. Some shells may provide a builtin nohup command which is similar or identical to this utility. Consult the builtin(1) manual page. ENVIRONMENT The following variables are utilized by nohup: HOME If the output file nohup.out cannot be created in the current directory, the nohup utility uses the directory named by HOME to create the file. PATH Used to locate the requested utility if the name contains no `/' characters. EXIT STATUS The nohup utility exits with one of the following values: 126 The utility was found, but could not be invoked. 127 The utility could not be found or an error occurred in nohup. Otherwise, the exit status of nohup will be that of utility. SEE ALSO builtin(1), csh(1), signal(3) STANDARDS The nohup utility is expected to be IEEE Std 1003.2 (``POSIX.2'') compatible. BUGS Two or more instances of nohup can append to the same file, which makes for a confusing output. BSD July 19, 2001 BSD