First, the good news: You’re closing in on retirement. After decades of long hours and loyal service, you only have another few years until you can kiss your job goodbye.

The bad news is you dread every minute. Even surviving another month will require Herculean effort.

Ideally, you’d quit now. But your financial plan dictates that you keep generating income into the early 2020s so that you can retire with a comfortable nest egg.

Perhaps you’re bored or exhausted in your job. Cynicism and negativity cloud your every thought. To make matters worse, an odious boss tears away at the thinning shreds of sanity you cling to.

So how do you persevere?

In rallying yourself to reach the finish line, you have a choice. You can either gripe at every available opportunity — lamenting your situation, lambasting higher-ups and regretting career moves you made years ago — or find a more productive way to cope.

Let’s focus on the latter.

Start by taking charge of your thoughts. Impose a time limit on how often you will stew in anger or self-pity. Endlessly telling yourself, “I can’t stand it here” or “I’ll never be able to make it through another month here, much less a year or two” won’t help.

“If you focus on how burned out you are or how much you hate your boss, then that’s the way you’ll think and feel,” said Dee Cascio, a life and retirement coach in Sterling, Va. “I suggest to clients that they replace those thoughts with recalling what brought them into this job and what aspects they enjoy.”

Another way to redirect your attitude is to visualize what you’ll gain. Filling your mind with pleasing images of retirement — what you’ll do, where you’ll reside — can divert attention from the daily grind.

Read: Why early retirement is all it’s cracked up to be

“Look at what positive purpose your work is serving, even if it’s just the positive financial purpose,” said John Grobe, president of Federal Career Experts, which provides preretirement training for federal employees. “Imagine that vacation you’ll take, the satisfaction you’ll have paying for your kid’s college or if you have a pension, that it’ll be bigger each year you work.”

Counting the days until retirement gets easier if you’re used to priming your brain to work to your advantage. Competitive athletes excel at this.

Joanne Waldman is director of training at Retirement Options, which certifies retirement coaches and offers retirement readiness assessments. She recalls a client, a marathoner, who said to himself at mile 20, “Keep going. I’m almost there. I can do this.”

“That’s the kind of message you want to tell yourself,” Waldman said. “Affirmations are great.”

The $80 billion fitness industry sees huge growth in this demographic

Meanwhile, explore how you can enrich your work life to make it more tolerable. Examples include delegating one of your least favorite duties, befriending a cheery colleague or learning a new skill.

Author of “Ready to Retire?,” Cascio adds that mentoring entrants into your field can unleash more positive energy. As long as you don’t dampen their enthusiasm by complaining, you can come away feeling gratified and recognizing the worthy aspects of your work.

At the same time, embrace wellness. Commit to a regular exercise routine, eat a balanced diet and seek support from friends outside the workplace.

“Don’t get into bad habits like drinking too much, smoking or any other unhealthy behavior,” Cascio said.

What happens if you try everything — from affirmative mantras to tweaking your job to make it more palatable — and you’re still in a constant state of despair?

“Some people who can’t stand it anymore may want to change careers,” Cascio suggested. “It may be worth three or four years of extending your working life to shift into a lower-paying field that you enjoy.”