How To Keep Running Commands After SSH Session Disconnection

Some time back i got an requirement that i have to run few commands/scripts which execute more than 8 hours to complete. But my SSH connection can’t wait for 8 hours. Either i can’t keep my Laptop at office for security reasons then how can i run command which should run continuously for 8 hours without interrupt. Here is an Few tips you can use To keep Running Commands After SSH Session Disconnection.

How To Keep Running Commands After SSH Session Disconnection

The Very first tip is to send your command to background and exit from command line command will execute still it completes in background. What i have to do for that.?

nohup sh scriptname.sh &

above command will create nohup.out file with command output and command will run in background

# jobs
[1]+  Running                 nohup sleep 300 &

OR

simply run command by adding & to last

# sleep 300 &

Option-2 Screen Command to Run background Jobs

# screen
bash: screen: command not found...

screen command by default not exists, you have install using below yum command

# yum install screen

Screen  is  a  full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells).  Each virtual terminal provides the functions of a DEC VT100 terminal and, in addition, several control functions from the ISO 6429 and ISO 2022 standards.

# screen -t sh scriptname.sh 

Or

# screen -L CommandName

It opens new virtual terminal within terminal session and it run scripts/commands to if your terminal closes no problem virtual terminal will still exists until your executed scripts completes. There are many options in screen command for more details read

# man screen

Option-3 tmux command to run scripts continuously

 tmux is a terminal multiplexer: it enables a number of terminals to be created, accessed, and controlled from a single screen. Install tmux using below command

# yum install -y tmux

Each session is persistent and will survive accidental disconnection, you re-attach using below command

# tmux attach

Open new session and run scripts as you want run for more duration

# tmux new-session <Command/Script>

Option-4 disown command keep running job after you exit from a shell prompt in background

When you exit from shell prompt hangup signal will be sent to all current running jobs to avoid to send hangup signal to your current running jobs just execute below command with disown 

# disown -h JOBID

Option-5 byobu for Ubuntu Linux

Byobu is an open-source Linux project to run multiple screens. Powerful then screen command and have more control over terminals

# apt-get install byobu

run commands using byobu

# byobu -S SessionTittle

Option-6 VNC Server/Viewer

Install VNC server in your Linux server and start vnc services connect from your desktop using vnc viewer and run the job close the viewer still your VNC session keep running in background

# yum install -y vnc*

Start VNC server services and

[root@ArkItServ ~]# vncserver

You will require a password to access your desktops.

Password:
Verify:

Warning: ArkItServ.local:1 is taken because of /tmp/.X11-unix/X1
Remove this file if there is no X server ArkItServ.local:1


New 'ArkItServ.local:2 (root)' desktop is ArkItServ.local:2

Creating default startup script /root/.vnc/xstartup
Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/ArkItServ.local:2.log

Connect using VNCVIEWER to VNC server session:2

Last but not least NoMachine RDP Tool

you can also run more background jobs and manage remote servers using NoMachine Free Linux Virtual Desktop Tool

See More Details Here

Conclusion

These tools will come handy when you want to run jobs/commands/scripts which runs for more duration/time.

Related Articles

PNP4Nagios Installation and Configuration Step by Step Guide

NRPE (Nagios Remote Process Executor)

KickStart Server

Thanks for your wonderful Support and Encouragement