ELLSWORTH, Maine — A former custodian at Ellsworth High School will serve three years behind bars for selling an undercover agent prescription pain killers at the school in October.

Frank Trundy, 53, pleaded guilty to one count of aggravated felony drug trafficking during a hearing in Superior Court in Ellsworth on Wednesday morning. As part of a plea agreement worked out with the state, Trundy was sentenced to eight years with all but three years suspended. He will be on probation for three years following his release.

Trundy was arrested on Oct. 28 after he sold 10 pills of hydrocodone — a prescription pain medication — to an undercover officer with the Maine Drug Enforcement Agency who was posing as a new custodian. Trundy also had purchased codeine from a student at the high school the day earlier.

Administrators at RSU 24 had contacted the MDEA after receiving information about possible drug activity at the school.

Appearing before Superior Court Justice Ann Murray on Wednesday, Trundy accepted full responsibility and expressed remorse for his crimes. He acknowledged past drug problems but explained that he was prescribed the pain medication to deal with broken bones that had never healed properly following a 2005 accident in which he was run over by a forklift.

Clearly nervous and upset, Trundy said he was “pleading guilty because I am guilty” and expressed concern about his wife, who was in the courtroom.

“This was a dumb move on my part and I ruined my career,” Trundy said.

Patrick Larson, the assistant attorney general handling the case, explained that one of the initial charges carried a minimum sentence of four years in prison but the state agreed to three years because Trundy has cooperated.

“This offense had a significant impact on the community by having occurred not only during school hours but also on school property,” Larson said. “Short of selling to students, this is about as serious as you can get.”

Murray agreed with Trundy’s attorney, Steven Juskewitch, that there was a significant and important difference between selling drugs to students and selling them to an adult.

“On the other hand, this offense happened during school hours on school property,” Murray said.

Trundy, a Maine native, had worked for 26 years for schools in New York before moving back to the Ellsworth area and landing a job with the local schools, where he worked for about six years.

Murray denied Juskewitch’s request to allow Trundy to remain free for an additional week to take care of his personal affairs with his wife. Among the restrictions during his probation, Trundy will have to refrain from alcohol and illegal drugs, submit to random drug and alcohol tests, and undergo substance abuse counseling.