I'm no expert, but my personal experience has been to use cabinet screw lengths that, when I start from the inside, the screw will be just short of the outside of the cabinet. I would think you'd be able to find varying lengths of screws that will allow you to do this. Since 2x4s are actually 1 1/2 inches by 3 1/2 inches, you'd have to take these measurements into account. I've found that 4x8 by 3/4 sheet goods vary in thickness from 5/8 to an actual 3/4.
However, and Drew and confirm or deny, if the 2x4s are going to bear some kind of load, you wouldn't want to stop short. Theoretically, I'd think you'd want to go from the outside-in to gain maximum holding power. Counter sink your holes on the outside for a smooth finish and dremel or hack-saw the residual screw that protrudes into the cabinet.
Just my inexperienced $0.02