How to run a java program in backgroud (unix / Linux)

Oftentimes, we use SSH to remote access into the server to run a Java program. The problem is, we can’t type anything After the Java program is executed like this :


$ java -jar example.jar

In addition, when the remote access session is expired or terminated, the executed Java program will be killed.

Solution

To fix it, append a & symbol to the end of the command, it executes the Java program in the background, and continue until it finished.


$ java -jar example.jar &

If the above command get killed when you leave the SSH session, try to add nohup in front.


$ nohup java -jar example.jar &

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

21 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Anonymus
5 years ago

How to run it using from jsp

Vikas
8 years ago

Thanks a ton !!!…Really helpful

Rakh
12 years ago

I know this was posted long ago, sorry about commenting on it.
I’ve got an issue related to this, driving me crazy. When I run my jar on linux prompt with a command like this:
java -jar MyJar.jar &
the process starts running in background as expected, but this process always gets killed some time later, say 2 hours for a reason I can’t figure out; Not memory issues because I’ve tried VM option (Xmx) and never getting the exception. Also no logs found in kernel about it.
But if I start app without the & sign and keep the console open, it will continue running as long as I have the console opened.
Does this mean anything to you? Any suggestion please.

mkyong
11 years ago
Reply to  Rakh

Try nohup

nohup java -jar example.jar &

snrat
9 years ago
Reply to  mkyong

It still does not work for me.
pid gets created and it runs once .then when i do ps-fp ,it does not give any process

arche
12 years ago
Reply to  Rakh

Even though the post is old. Maybe you run it as a different user and then you exit, or if not, you need to do the following nohup java -jar MyJar.jar &

Lindsay
13 years ago

Very nice mkyong. It worked for me.

Freakygeek
13 years ago

If i close my SSH like Putty, will it stop the process?

Giang
13 years ago
Reply to  mkyong

I think it not right. I tried and close Putty, the pricess also be closed.

mkyong
11 years ago
Reply to  Giang

Try nohup
nohup java -jar example.jar &

Cesarla
13 years ago

Really useful, you save my life!

cracker
15 years ago

any body tell me how to do it in windows xp……….

mkyong
11 years ago
Reply to  cracker

On Windows, try task scheduler

suhana
14 years ago
Reply to  cracker

>javac filename.java
>java filename

Linh Locklier
15 years ago

I think I will give this a try, thanks for the post.

Mangara
16 years ago

Thanks! Exactly what I was looking for.

jeff
16 years ago

tnx.. i found 1 of my assignments…
can you help me some times if i have my other assignments?…

software development company
16 years ago

Interesting,

Although linux is not so widely used ,

Keep up the good work