CHICAGO — With two outs in the seventh inning the statistic was staggering: The Giants hadn’t gotten a hit in 40 at-bats spanning two games.

Hunter Pence ended the futility with a home run, but it wasn’t enough to stop San Francisco from another defeat in its miserable second half of the season.

Jon Lester pitched 6 2/3 innings of no-hit ball and settled for his second complete game of the season, helping the sizzling Chicago Cubs shut down the Giants 2-1 on Friday.

“We were getting some things going but the boys needed a break and have one of those hard-hit balls fall in,” Giants manager Bruce Bochy said.

Pence ended Lester’s bid for a second no-hitter with a homer to left on a 1-2 changeup that cut over the plate. Brandon Crawford followed with a double and it looked like Eduardo Nunez was going to drive in the tying run, but his liner was caught by third baseman Kris Bryant.

“We ran into a very hot pitcher who’s throwing the ball very well and he was on top of his game,” Bochy said. “Still, you’ve got to generate some offense.

“I will say we hit some balls right at them, too. We could’ve used a break on Nunez’s ball. He just hit it right at him or right there the game is tied.”

Lester worked around Trevor Brown’s one-out double in the eighth. After a ninth-inning walk, he struck out Crawford — his fourth of the game — to end it with his 102nd pitch.

Lester allowed three hits and walked two. The Cubs have won five straight to improve to 87-47. The last time they were 40 games over .500 was the end of the 1945 season.

“It’s pretty incredible,” Cubs manager Joe Maddon said.

David Ross had an RBI double and Dexter Fowler a run-scoring single in the third off Albert Suarez (3-3) as the Giants fell to a major league-worst 15-29 since the All-Star break.

Lester walked Buster Posey with two outs in the first inning and followed by setting down 18 straight while getting some help.

Bryant made a diving, backhand grab of Crawford’s liner to lead off the fifth and Dexter Fowler made a sliding catch of pinch-hitter Kelby Tomlinson’s liner to center an inning later.

“We had some chances,” Pence said. “Crawford hit a double. Brownie hit the double. Nunez crushed that line drive right at him Dennard (Span) hit that line drive. They just happened to be there.”

The 32-year-old Lester threw a no-hitter on May 19, 2008, for Boston against Kansas City. He acknowledged he started thinking about a second one after the first inning and added, “Anybody that tells you different is lying to you.”

The rookie Suarez retired the first six batters. He allowed three hits with two walks and three strikeouts in five innings, settling down after his 34-pitch third.

“I thought he did a very nice job,” Bochy said. “He got in a jam there and could’ve let it get away from him but he made big pitches. He found a way to keep it close and gave us five solid innings.”

TRAINER’S ROOM

Giants: Brown (illness) started after being unavailable Thursday, allowing Posey to play 1B. Brown tested negative for strep throat. … RHP Matt Cain (back) was activated off the DL and became the 12th reliever thanks to expanded rosters.

Cubs: RHP John Lackey (shoulder) came out fine after a bullpen session and will start Sunday’s series finale.

LONG DROUGHT

The 12 consecutive full innings the Giants were held without a hit tied the longest streak for the team since at least 1974, according to STATS.

PENCE’S STREAK

Pence has hit in 17 straight games at Wrigley Field, the longest active streak in the majors.

CLOSE GAME WOES

The Giants fell to 4-12 in one-run games since the All-Star break.

UP NEXT

A showdown of stars Saturday as Cubs RHP Jake Arrieta (16-5, 2.84 ERA) faces LHP Madison Bumgarner (13-8, 2.49). “It’ll be an interesting day, no question,” Maddon said.