Kill all Java processes which have been running for more than two hours

On linux OS, how can I kill all Java processes that have been running on a server for more than 2 hours? Currently I'm using pkill -f 'java -jar' once every twenty four hours to kill processes but this also kills any current running process.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

That sounds like what you're asking is not the real problem. Why are there processes around that shouldn't be?
SheppardDigitalAuthor Commented:
That's something we need to look at, but in the meantime we need to apply a temporary solution until we have free resources to investigate the main cause of the issue.
I could possibly give you a free consultation on this.
Gerwin Jansen, EE MVETopic Advisor Commented:
Try this:

ps -eo pid,lstart,command | grep java | grep -v grep

It will give you a date/time sorted list of java processes, change 'java' to something else if you need a different filter.

Don't know how long your list typically is but you could add something like '| head -5' to get a list of the 5 oldest processes that you can kill this way:

kill $(ps -eo pid,lstart,command | grep java | grep -v grep | head -5 | awk '{print $1}')

You could also filter on the 2nd column (with awk maybe) and search for the 2 hour processes.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.