user198729
user198729

Reputation: 63686

Need help understanding a bash command

It's actually a combination of php and bash:

exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $outputfile, $pidfile));

I don't understand what 2>&1 & echo $! is there for?

Upvotes: 2

Views: 192

Answers (1)

Mark Rushakoff
Mark Rushakoff

Reputation: 258358

2>&1 redirects stderr to stdout, and $! "Expands to the process ID of the most recently executed background (asynchronous) command".

So, here's what happens:

  1. You send both stderr and stdout of $cmd to a file named $outputfile. If you didn't do 2>&1, you wouldn't be able to read the stderr output in the file.
  2. The following & means that process runs in the background.
  3. Then you append the PID of $cmd (obtained through $!) to the end of $pidfile.

Upvotes: 7

Related Questions