PuTTY or OTHER - Keep executing command even after exiting PuTTY

13

5

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.

user3331952

Posted 2014-11-18T09:12:46.283

Reputation: 133

Question was closed 2014-11-30T20:12:45.400

Is there any reason why you're not running these services as daemons? – SevenSidedDie – 2014-11-18T21:16:16.253

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 – 2014-11-19T17:13:47.277

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 – 2014-11-19T20:09:32.987

Answers

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.

Eamonn Travers

Posted 2014-11-18T09:12:46.283

Reputation: 496

nohup definitely does the job – Pitto – 2014-11-18T10:31:52.437

Glad to be of help. – Eamonn Travers – 2014-11-18T10:38:54.363

15screen 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 – 2014-11-18T12:23:12.093

1nohup sends the output to a file where it can be processed. – Brian Knoblauch – 2014-11-18T21:24:05.433

For those who are new to nohup, it's basically "no hangup" - with nohup, the hangup (HUP) signal is ignored. – Dan7119 – 2014-11-20T19:29:32.980

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.

Mark

Posted 2014-11-18T09:12:46.283

Reputation: 1 304

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 – 2014-11-19T09:18:00.160

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.

user391035

Posted 2014-11-18T09:12:46.283

Reputation: 336

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.

1k5

Posted 2014-11-18T09:12:46.283

Reputation: 131

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

nulldev

Posted 2014-11-18T09:12:46.283

Reputation: 15