A man walked into a school in southern California Monday and shot dead his estranged wife in front of her students, killing an eight-year-old boy in the crossfire before turning his gun on himself.

Police said local resident Cedric Anderson had checked into the office as a visitor after entering the campus in the city of San Bernardino and going to the special needs classroom, where he opened fire on Karen Elaine Smith as she was teaching.

Source: b'The school where the shooting took place | Source: AFP'

Officers said Anderson had only targeted Smith but two students were caught in the crossfire and one of them, Jonathan Martinez, died later in hospital.

"This does appear to have been a murder-suicide with both male adult and female adult victim succumbing to injuries, with the male succumbing to a self-inflicted gunshot wound," Lieutenant Mike Madden of the San Bernardino Police Department told a news conference.

Police said initially the two wounded students had been listed as critical but later confirmed Martinez's death.

Source: b'Parents with their children after the shooting | Source: Reuters'

Students at North Park Elementary School -- which has around 500 students between kindergarten and sixth grade -- were transported to a nearby campus, where they were "having snacks, playing games and watching a Disney movie," the police department tweeted.

Many panicked parents and relatives who rushed to the school had to endure waits of several agonizing hours before they could be certain that their child was not among the victims.

"I saw blood splashing on the wall. I ran as fast as I can. I lost a shoe," one student told AFP.

An 11-year-old boy told AFP he was in the middle of a math test when he heard the gunfire.

"I started feeling really scared from the SWAT team, and people from the police station came through the door to walk us out we had to walk with our hands up," he said.

Restarting the gun debate

San Bernardino, about an hour's drive east of Los Angeles, was the scene of horrific violence when Syed Rizwan Farook and his wife Tashfeen Malik gunned down 14 people and wounded 22 others in December 2015 before being shot dead by police.

Monday's shooting will likely reignite the debate on gun violence in the US, where attempts to put in place tougher gun control measures have failed, despite a series of mass killings.

In one of the most notorious school shootings in modern US history, 20 children and six staff were massacred in 2012 at Sandy Hook Elementary in Newtown, Connecticut.

In June last year, 49 people were killed in a shooting rampage at Pulse, a gay nightclub in Orlando, Florida.

"School campuses are sacred spaces where children should be free to learn, play and grow without threat of violence," Los Angeles Mayor Eric Garcetti said in a statement.

"Today's shooting... is a reminder that deadly weapons can shatter that sense of security, mercilessly and without warning -- leaving parents and educators struggling to address the questions, fears and anxieties that gun violence creates for our young people."