A few months after the start of the Civil War, Cunningham assisted with the recruitment of a company of volunteer infantry in Gloucester. The company was formed in November 1861 under the command of Captain James P. Draper and became Company D of the 1st Battalion Massachusetts Infantry. Cunningham was second in command of the company, receiving the commission of 1st lieutenant on December 2, 1861. The company was made up almost entirely of Gloucester fishermen and, early in its service, had a reputation for unruliness. The battalion was stationed during the winter of 1861-1862 at Fort Warren, the largest fortification in Boston harbor. There, the unit trained in infantry and artillery drill and also guarded the approximately 1,000 Confederate prisoners held at the fort. On March 6, 1862, Cunningham was promoted captain and placed in command of Company F of the battalion. In May 1862, the battalion was transferred to the battle front in northern Virginia. That summer, four more companies were added to the battalion, amounting to ten companies as required for a full regiment. The unit was designated, on September 3, 1862, the 32nd Regiment Massachusetts Volunteer Infantry. The regiment became part of the V Corps in the Army of the Potomac. Over the course of 1862, Cunningham commanded Company F in combat during the battles of Second Bull Run, Antietam and Fredericksburg. In 1863, he participated in the battles of Chancellorsville, Gettysburg and Mine Run. In 1864, still in command of a company in the 32nd Massachusetts, Cunningham saw action in the Overland Campaign, a series of battles aimed at grinding down the Confederate Army of Northern Virginia and taking the Confederate capital of Richmond. The Overland Campaign ended in a stalemate and resulted in the long Siege of Petersburg.
Regimental command
Because several of the senior officers of the 32nd Massachusetts were killed or wounded in the course of the Overland Campaign and the Siege of Petersburg, Cunningham was promoted to lieutenant colonelin the summer of 1864 and took command of the regiment at the end of September. He would command the regiment for the remainder of its service. In late March 1865, the Confederate army abandoned its siege lines at Petersburg and retreated towards western Virginia, bringing about the Appomattox Campaign and, ultimately, the end of the war. It was during this campaign, on April 2, 1865, that Cunningham distinguished himself in the wake of the Battle of Five Forks. The battle, having taken place on April 1, 1865, forced the Confederates to begin their retreat from Petersburg and Richmond. That night, Cunningham was placed in command of a brigade of skirmishers and ordered to lead the pursuit of the retreating Confederate army. Deploying his brigade under the eye of Major General Philip Sheridan, Cunningham pushed his men rapidly westward, covering many miles of ground and overtaking hundreds of Confederates in the course of their retreat as well as significant amounts of Confederate supplies. On January 18, 1867, President Andrew Johnson nominated Cunningham for the award of the honorary grade of brevet brigadier general, United States Volunteers, to rank from April 1, 1865, for distinguished services during the campaign against Richmond, and for especial gallantry at the battle of Five Forks, Virginia, and the U.S. Senate confirmed the award on February 21, 1867. Over the next week, the Union cavalry and V Corps pursued the Confederates, forcing their surrender at the Battle of AppomattoxCourt House on April 9, 1865. Near the close of the battle, Cunningham spotted through his field glasses a white flag of truce. Riding forward, Cunningham ascertained that the flag was born by a Confederate staff officer who wished to communicate a message to Lieutenant General Ulysses S. Grant that Confederate General Robert E. Lee wished to discuss terms of surrender. After the close of the war, Cunningham was mustered out of service with the rest of the 32nd Massachusetts on June 29, 1865.
Post-war life
Immediately after the war, Brevet Brig. Gen. Cunningham dabbled in politics, serving briefly as a representative to the Massachusetts General Court in 1865. At the close of 1866, he accepted an appointment as Adjutant General of Massachusetts, becoming the commanding general of the Massachusetts Volunteer Militia. He succeeded the wartime adjutant general, William Schouler and served in that capacity until 1879. In 1882, Cunningham became the superintendent of the Massachusetts Soldier's Home in Chelsea, a position he occupied for ten years until his death in Chelsea on July 17, 1892. He was buried in Oak Grove Cemetery, Gloucester, Massachusetts.