Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am using Ubuntu 12.04 server edition. I have access to the server via ssh.

My question is how can I run a program through SSH such that it wont be get closed even if I disconnect the ssh. It has to be run in the server as a service.

can you tell me how can I do that?

share|improve this question

closed as off-topic by Falcon Momot, SvW, Jenny D, rnxrx, mdpc Jan 3 at 20:49

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions must demonstrate a minimal understanding of the problem being solved. Try including attempted solutions, why they didn't work, and the expected results. See How can I ask better questions on Server Fault? for further guidance." – Falcon Momot, SvW, Jenny D, rnxrx, mdpc
If this question can be reworded to fit the rules in the help center, please edit the question.

6 Answers 6

up vote 2 down vote accepted

Invoke command with nohup

from nohup info:

'nohup' runs the given COMMAND with hangup signals ignored, so that the command can continue running in the background after you log out. Synopsis:

 nohup COMMAND [ARG]...
share|improve this answer
    
THANK YOU FOR YOUR ANSWER –  Hari Jan 2 at 11:55

To run program as service i suggest to use

start-stop-daemon --start --quiet --background --make-pidfile --pidfile $PIDFILE --exec $DAEMON --chuid $USER --chdir $DIR -- $DAEMON_ARGS 

take a look in /etc/init.d/skeleton and write init script based on this example.

share|improve this answer
    
Thank you for answering –  Hari Jan 3 at 2:53

nohup does not automatically put the command it runs in the background, you should end the command line with an & symbol.

nohup command &

share|improve this answer
    
also u can press ctrl+Z, then bg –  eri Jan 2 at 12:07

In addition to the nohup advice, consider using GNU Screen, it allows you to have mutliple terminal windows open with the ability to resume connections should your ssh session die for any reason.

share|improve this answer

screen is useful.

Run screen, then run your command, press ctrl+A, then d. You can return to your program. Run screen -ls to find Detached screen, connect to it with screen -r 20673.pts-0.srv.

share|improve this answer

Upstart! As you're using Ubuntu 12.04..

You create an upstart script in /etc/init/

Your script contains directives, which control the process, how it runs, where it runs, environment, respawn if fail, etc.

# my upstart script
# optional stuff
description "start and stop the THING"
version "1.0"
author "Tom"


# configuration variables.
# You'll want to change these as needed
env A_THING_TO_SET=blaaah

# tell upstart we're creating a daemon
# upstart manages PID creation for you.
expect fork
# of course, if it doesn't fork, take out the above line

script
# My startup script, plain old shell scripting here.
chdir /opt/thingy/bin
exec /opt/thingy/bin/thingy ARGUMENTS
# create a custom event in case we want to chain later
emit thingy_running
end script

Taken and adapted from: http://geeknme.wordpress.com/2009/10/15/getting-started-with-upstart-in-ubuntu/

I also recommend you read the Upstart Cookbook

If you're not using Ubuntu, then other options include supervisor(d), monit, daemontools all of which suck far less than running stuff in screen or with nohup.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.