He was born in 1762 in Dunbar, in East Lothian on the southeast coast of Scotland.
He served in India, as a lieutenant in the 73rd regiment in the war against Hyder Ali's forces. In 1780, he was wounded and captured. He was held prisoner for four years under bad conditions. On his release, he was promoted to Captain. However, he was still very ill and stayed with his brother in Bengal until 1786, when he was much recovered.
On his return to England in 1797, he was appointed the commander of an invalid company based on Guernsey, where he married Miss Elizabeth Dobree. He set up a school for the children of soldiers in his command.
He was then appointed Lieutenant-Governor of Pendennis Castle and served until 1811. He formed the Pendennis Volunteer Artillery, a local militia.
He married Elizabeth (1770–1845), youngest daughter of Peter Dobree of Beauregarde, Guernsey. They had nine children, including at least two daughters.
Their eldest son, John, was drowned, aged 19, off Madeira, in 1808.
Their second son died at the age of 12.
Their third son, James Cosmo (1792–1861), was Secretary of the East India Company.
Their fourth son, Philip (1796–1882), became Military Secretary to the East India Company in 1837.
Their fifth son, Henry (1798–1871), was a Church of England clergyman, who became principal of the East India Company College, Haileybury and then a canon residentiary of St Paul's Cathedral; he was also rector of Barnes, Surrey and a famous preacher.
Another son, Peter (1803 - 1895), became a Major General, KCB and was military and naval secretary to the government of Bombay.
In 1807, he founded the Falmouth Misericordia Society "for the relief of poor strangers and distressed persons of the town". He also helped found a Church Girls' School in 1802 and a Boys' School in 1805.
He died on 27 October 1811 at Pendennis Castle. Memoirs of him were published in 1812
The Falmouth Misericordia Society was still in operation in 1887.
"Melvill Road", connecting the A39 road with Falmouth Docks is named after him.