13

This question already has an answer here:

I have a couple of commands that I need to run. These commands needs to continually run even after I exit my interface. When I have access to the Linux GUI, I execute something like:

cd /home/testuser/Scripts*
xterm -hold -T "Background_Script1" -e  "service1-start.sh" &
sleep 10
xterm -hold -T "Background_Script2" -e "service2-start.sh" &

Any help will be much appreciated.

marked as duplicate by Ƭᴇcʜιᴇ007, Kevin Panko, Matthew Williams, Nifle, bwDraco Nov 30 '14 at 20:12

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
  • Is there any reason why you're not running these services as daemons? – SevenSidedDie Nov 18 '14 at 21:16
  • Are the commands meant to be services that should run at near-100% uptime "forever", or a long-running processing task that's expected to finish after some time? All answers will "work" for both cases, but the proper recommendation will be different. – Peteris Nov 19 '14 at 17:13
  • Which specific distribution of Linux are you using? I found that running set -m; makes it so that my scripts continue running after I log out on CentOS and RHEL. – ArtOfWarfare Nov 19 '14 at 20:09
33

Try

nohup Background_ScriptX &

nohup ensures that a process is not terminated when a terminal is closed. The & symbol pushes the process into the background.

Hope this was helpful.

  • nohup definitely does the job – Pitto Nov 18 '14 at 10:31
  • Glad to be of help. – Eamonn Travers Nov 18 '14 at 10:38
  • 15
    screen has one clear advantage over nohup: nohup does not allow you to return to a running command (e.g. next morning to read its stdout, then choose to kill it, etc). screen allows you to do exactly that. – Andreas F Nov 18 '14 at 12:23
  • 1
    nohup sends the output to a file where it can be processed. – Brian Knoblauch Nov 18 '14 at 21:24
  • For those who are new to nohup, it's basically "no hangup" - with nohup, the hangup (HUP) signal is ignored. – Dan7119 Nov 20 '14 at 19:29
20

If you want to read the output at a later date, you can use screen:

screen -d -m my_command

This gives you a detached terminal you can connect to later (screen -r) to read the stdout/stderr output.

  • 2
    +1. For one, if you close your terminal (e.g. putty) and then you open it again and reattach to screen (screen -r) you will also have all the output your program produced and you'd have otherwise "missed". Plus, screen can do so many amazing things, like split screens, etc.! :) – Alberto Santini Nov 19 '14 at 9:18
6

Another option if nohup command is not available is to use the disown command.

First you can view your background tasks by running jobs in terminal. You can get the job number from there.

Now just run disown %[job number] and the process will be detached from the current terminal and stay alive when you log out.

Side note: Make sure that you are asking the correct question. If you are running actual services as you hint in your sample you might want to look into how to create a daemon in the OS of your choice. This is to make sure that the process still runs after a reboot and make it more consistent with other services.

Hope this helps.

3

Since screen has been mentioned: there are other terminal multiplexers out there. I like tmux very much, see e.g. this other superuser question, tmux vs screen for a comparison.

0

You can also try: setsid Setsid also works to background programs and allows them to keep running after the terminal/ssh session is disconnected.

Use it like this:

setsid program-to-background

Manpage: http://man7.org/linux/man-pages/man2/setsid.2.html

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