dongjk
dongjk

Reputation: 343

how to redirect the '&' command's log?

here is the command:

 # ls >log 2>&1 &
 [1] 24274

now I want this line [1] 24274 to redirect a another file. any ideas?

Upvotes: 3

Views: 91

Answers (2)

Todd A. Jacobs
Todd A. Jacobs

Reputation: 84423

Unless you're trying to get the job number, the easiest way to do this is to use $!, which will give you the PID of the most recent background job. For example:

$ sleep 30 & bg_pid=$!; echo $bg_pid
[2] 10100
10100

If you really want to get the job number back from the stored PID, perhaps to use Bash's job control instead of sending signals to the process with kill, you can do this:

jobs -l | fgrep $! | perl -ne 'print "$1\n" if /\[(\d+)\]/'

Upvotes: 2

Anton Tykhyy
Anton Tykhyy

Reputation: 20086

Use $!:

ls >log 2>&1 &
echo ls: pid=$!

Upvotes: 5

Related Questions