Deuteronomy 6:2:

That thou mightest fear the LORD thy God, to keep all his statutes and his commandments, which I command thee, thou, and thy son, and they son’s son, all the days of thy life; and that thy days may be prolonged.

Notes on Deuteronomy 6:2:

This is one of many passages linking obedience to God to long life.

See also:

Some of God’s Promises Have Conditions
Is There an Appointed Time for Each Person to Die?