Cops shot a man in Brooklyn on Sunday night as he was attacking another individual with a knife, sources said.

Officers had been responding to a call of a dispute at 110 Shepherd Avenue in Cypress Hills when they found the pair fighting in the backyard shortly before 9 p.m., according to police sources.

The knife-wielding man, identified as Francis DeJesus, 31, had an ongoing beef with the person he was allegedly attacking — and had just gotten into an altercation with him earlier in the day, the sources said.

Jeffrey Maddrey, head of the NYPD’s Patrol Borough Brooklyn North, later explained at a press conference that the two men were roommates and that they both lived at the residence.

They decided to reignite their feud on Sunday night, as residents were outside enjoying the summer weather.

After exchanging words in the backyard, the pair began trading punches and a knife was eventually pulled by DeJesus, sources said.

He was allegedly stabbing the other individual in both of his arms — and chasing him around the house — when cops showed up on Sunday.

“Our officers gave commands to the male to drop the knife and fired one time as the roommate was being stabbed,” Maddrey said. “The man immediately fell.”

DeJesus was shot once and transported to Brookdale Hospital, where he was said to be in serious condition. The man he was stabbing suffered injuries to both of his arms, but was expected to survive.

Police are still investigating.

Additional reporting by Tina Moore