Just a few days before he was shot and killed, a US police officer in Baton Rouge had posted a Facebook message sharing his emotional fatigue and expressing how difficult it was to be both a policeman and a black man.

Montrell Jackson, 32, died on Sunday along with two colleagues when a gunman opened fire on them, nearly two weeks after white police in the Louisiana city shot and killed Alton Sterling, a 37-year-old black man and father of five.

Baton Rouge shooting: 3 US police officers killed

Sterling's shooting triggered nationwide protests, including one in Dallas where five officers were shot dead.

"These last three days have tested me to the core,"  an emotional Jackson wrote on his Facebook page, in an entry posted the day after the Dallas shootings, and just over a week before he was killed.

"I love this city, but I wonder if this city loves me. In uniform, I get nasty hateful looks, and out of uniform some consider me a threat," said Jackson, who had served in the Baton Rouge force for 10 years and whose partner recently gave birth.

Describing himself as "tired physically and emotionally", he added: "These are trying times. Please don't let hate infect your heart."

Police officers killed

Louisiana State Police Superintendent Colonel Mike Edmonson told reporters the gunman - identified by US media as Gavin Long, 29 - was killed in a gunfight and there were no suspects at large.

The motive was not immediately clear.

Also among the dead officers was Matthew Gerald, 41, a rookie with a military background, the police department said on Facebook.

The third officer killed was Sheriff's Deputy Brad Garafola, 45, a father of four, local media reported, citing Sheriff Sid Gautreaux and Garafola's wife, Tonja.

Inside Story - US police killings: What role does prejudice play?

A fourth policeman, aged 41, was fighting for his life on Monday, Gautreaux said.

Call for unity 

President Barack Obama condemned the "cowardly" Baton Rouge shooting and demanded an end to such violence.

"It is so important that everyone ... right now focus on words and actions that can unite this country rather than divide it further," Obama told reporters at the White House.

READ MORE: There are two Americas that really need to meet each other

"We don't need inflammatory rhetoric. We don't need careless accusations thrown around to score political points or to advance an agenda. We need to temper our words and open our hearts, all of us."

Obama has repeatedly called for racial unity. "Nothing justifies violence against law enforcement," he said.

Widespread protests

Baton Rouge became the scene of large protests against police brutality after the killing of Sterling on July 5.

Police officers killed Sterling outside a supermarket, claiming he had a gun. Sterling had been selling CDs.

Footage of the moment the father of five was killed was captured on a mobile phone and circulated online.

READ MORE: Second US police shooting in two days sparks outrage

His killing was followed the next day with another police shooting. An officer killed a 32-year-old black man, Philando Castile, at a traffic stop in the midwestern US state of Minnesota. The aftermath of the shooting was also captured on video and streamed live by Castile's girlfriend on Facebook.

The deaths sparked outrage and protests in many cities across the US.

The Guardian has documented at least 587 people killed by police across the US so far this year. From that total, 145 - nearly 25 percent - were black, although black Americans constitute only around 13 percent of the country's total population.

Source: Al Jazeera and agencies