Correct use of "&" to release con, 2 number terminal answer?

michaelsanford

Translator, Web Developer
I've got an alias that backs up my web design folder (scripts and stuff) to my little USB stick. This is the line from .cshrc

alias vault 'cp -r /Library/WebServer/Documents/design /Volumes/VAULT/ &'

When I run it from the command line I get the answer

[gwailo:~]% vault
[1] 937


I know that ampersand releases the process from the console, but what's the business with the two numbers? Is there a way to suppress them?

It looks about right that the second number is a PID, and the first number is uuh :rolleyes:

This command runs in the background so that I don't have to wait until it's finished copying to do other stuff.
 
Could the first number be your your history number?

Was that the first command you entered?

Look at how your prompt is configured in your .tcshrc, ,cshrc, or wherever you have your environment settings defined. That's where you'll find the answer.
 
It's not (by far) the first command entered, but any way the command history is preserved across logins so it wouldn't be that (good guess tho).

My prompt's set in .login and is set to [%n:%~]%#

What makes you think I'll find the answer there (no sarcasm intended, I'm genuinely curious)? It doesn't seem to have anything to do with the command prompt.

The other idea I had was that it's a tty indicator (and that just happens to be running at /dev/ttyp1) but that doesn't hold water since I can open another shell at /dev/ttyp2 and it will still say [1]...
 
it's the job number for the session. If you launch something else in the background, you'll see a [2]. This is so that you can bring the job back into the foreground, if you want, by issuing a 'fg %jobnumber'. So, if you wanted to bring your vault process to the foreground, you would issue a 'fg %1'. To return it to the background, you would issue a ctrl-z (stopping it), then a 'bg' to put it in the background. Throughout all this, it will reference it by its job number and, optionally, its pid. You can see a list of jobs by simply issuing the 'jobs' command.

For more info, see 'man tcsh'..
 
Oh, one thing to be aware of Michael, in case you weren't already - & does not fully detach the process from the terminal it's running on. That means, if you close the window before the job's done running, it will kill the job. If you want the job to keep running even if the window closes, you would need

alias vault 'nohup cp -r /Library/WebServer/Documents/design /Volumes/VAULT/ &'
 
First: The ampersand does not "release the process from the console" (correct: detach it from the controlling terminal). It just creates a background job which in fact stays attached to your terminal. You can easily see this in case the background job would output something which would just asynchronously appear in your terminal window.

Second: Each job which you put into background has a number. The first one is job #1. The numbers you get are 1) the local background job number in brackets and 2) the global process id. You can put a running background job into foreground with fg %1 (if the local job number is 1). There is an analogous bg command as well.

EDIT:
Sorry, I just overlooked that everything was already answered...
 
Back
Top