How to run application from terminal forever?

22,115

Solution 1

The simplest and most direct method is nohup java -jar jenkins.war &. "nohup" means "no hangup", which is vintage terminology for not ending a session when the terminal disconnects. "&" just starts the process in the background, similar to what would happen if you press CTRL+Z (and then type bg 1) while the process is running in the foreground.

WARNING: nohup, by default, sends output of the process to a text file. If you are not careful, that file can become dangerously large in some cases. Treat it like any other ever-growing log file.

Solution 2

Install screen. In the terminal type screen and then run you application. Press CTRL+A,CTRL+D to deattache the screen and type the terminal screen -r to reattache. You can reach you screen through ssh etc...

apt-get install screen

Solution 3

Use nohup

No Hang Up. Run a command immune to hangups, runs the given command with hangup signals ignored, so that the command can continue running in the background after you log out.ManPage

nohup Command &

In you case it would be

nohup java -jar jenkins.war &

Solution 4

If you are frequently starting a service like Jenkins that you want to have run in the background and not close when you end your session, you should consider implementing an init script for it. Look at the scripts in /etc/init.d/ for examples. These are short shell scripts that allow you to do things like service httpd start and service httpd stop and even service httpd status to find out if httpd is running, or if it's supposed to be running but isn't.

There is a great example script for the Hudson service, which functions very similarly to Jenkins (they are sibling forks of the same codebase) here: https://wiki.jenkins-ci.org/display/JENKINS/HudsonUbuntuLinuxStartupScript

Share:
22,115

Related videos on Youtube

UAdapter
Author by

UAdapter

Updated on September 18, 2022

Comments

  • UAdapter
    UAdapter over 1 year

    I start jenkins web server using command java -jar jenkins.war. It works great. When I close terminal the application stops.

    How to make it run even if I close terminal session ?

  • mx7
    mx7 over 10 years
    may I know why downvote ?
  • Rmano
    Rmano over 10 years
    Probably because in modern shells, if you do not do nohup (or disown the process afterward) it will be killed when the shell exits.
  • Rmano
    Rmano over 10 years
    forgot & to background the process.
  • mx7
    mx7 over 10 years
    I have tested firefox from terminal before I have posted my answer here.
  • Sparr
    Sparr over 10 years
    @rajagenupula When you type firefox from a console, you're running a script that later launches the firefox binary detached from your console. That step is not automatic for most commands.
  • JohnZaj
    JohnZaj over 4 years
    Curious why you chose nohup rather than setsid since setsid disconnects the process from the controlling terminal? I switched to using setsid after nohup just wasn't reliable with terminal session; since setsid makes a process its own “session leader” which isn’t controlled by any terminal. If it isn’t controlled by a terminal it won’t get a SIGHUP when the terminal closes. Maybe I'm missing something?