0

I have the following but I'm worried about $! being overwritten prior to the echo command taking place. How do you guys recommend going about this?

sleep 100 & >/dev/null ; echo $! >sleep.pid
7
  • Why are you backgrounding a sleep command?
    – MadHatter
    Jul 18, 2013 at 9:53
  • This is just an example here.
    – Abdo
    Jul 18, 2013 at 9:54
  • It will be difficult to answer such an abstract question. I strongly advise you to be more concrete about what you're trying to do.
    – MadHatter
    Jul 18, 2013 at 9:55
  • I'm trying to write the process id of a command I'm forking in a file while avoiding any possibility of concurrency.
    – Abdo
    Jul 18, 2013 at 10:01
  • 1
    After running a brief test, i've found that i can fork a process under one instance of bash, and it has no effect on the value of $! under other instances of bash. I'm quite certain that the value of $! in one session is the pid of the last backgrounded process in that particular session.
    – Rob M
    Jul 18, 2013 at 10:11

1 Answer 1

2

Bash is designed such that it cannot happen as you've described it. The value of $! in one session is the pid of the last backgrounded process in that particular session.

2
  • This works granted the first command does not fork any commands (check discussion on stackoverflow about it at stackoverflow.com/questions/17720131/…). I'll accept this answer.
    – Abdo
    Jul 18, 2013 at 12:04
  • My console disagrees.
    – Rob M
    Jul 18, 2013 at 12:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .