PuTTY or OTHER - Keep executing command even after exiting PuTTY

5,267

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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.

Share:
5,267

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    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.

  • Pitto
    Pitto over 9 years
    nohup definitely does the job
  • Eamonn Travers
    Eamonn Travers over 9 years
    Glad to be of help.
  • Brian Knoblauch
    Brian Knoblauch over 9 years
    nohup sends the output to a file where it can be processed.
  • Alberto Santini
    Alberto Santini over 9 years
    +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.! :)
  • Dan7119
    Dan7119 over 9 years
    For those who are new to nohup, it's basically "no hangup" - with nohup, the hangup (HUP) signal is ignored.