Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • Ok, thanks. I'd also like to send the pid of my command to a file and used to append the command with & echo $! >> /path/to/pidfile. I can still do this but will the pid be correct, or that of tee etc? Commented May 10, 2017 at 14:41
  • @LukeCousins, yes in cmd1 | cmd2 &, $! is the pid of cmd2. You can use the process substitution approach in that case. Or you can use sh -c 'echo "$$" > pidfile; exec your-cmd' in place of your-cmd. Commented May 10, 2017 at 14:47
  • Ok, in this particular use case the exit status is not important. Could you give an example of using bash with the pid file getting the right id using process substitution as I'm not quite sure. Thanks for all the help :) Commented May 10, 2017 at 14:50
  • @Luke, see edit. In the process substitution case, in the case of bash or zsh (as opposed ksh93), if you add a &, because of the command group, you'll get an extra subshell process, and $! will be the pid of that subshell. Hence the extra exec. Commented May 10, 2017 at 15:04
  • @StéphaneChazelas In your bash command { your-cmd 2>&1 >&3 3>&- | tee stderr.log 3>&-; } > stderr+stdout.log 3>&1 , what does 3>&- do and why do you need curly braces around the first 2 parts? Commented Mar 3, 2024 at 12:31