Different ways to keep a process running even after ssh logout or session/terminal is closed in linux ?

In this article, we will look at different ways to let our process running in background even after we close our terminal, or our session timeout, or due to any network connectivity issues.

This tip will help you to run the processes in background and keep them running even if your session is terminated due to any issue(s).

1. nohup and &

[root@nglinux ~]# nohup iostat -c 5 &
[1] 4498
[root@nglinux ~]# nohup: ignoring input and appending output to `nohup.out'

[root@nglinux ~]# ps -ef | grep -i iostat
root      4498  4176  0 23:19 pts/1    00:00:00 iostat -c 5
root      4502  4176  0 23:19 pts/1    00:00:00 grep -i iostat

[root@nglinux ~]# cat nohup.out 
	top: failed tty get

	top: failed tty get

Linux 2.6.32-754.el6.i686 (nglinux) 	04/23/2019 	_i686_	(1 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.06    0.00    0.07    2.07    0.00   97.81

Linux 2.6.32-754.el6.i686 (nglinux) 	04/23/2019 	_i686_	(1 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.06    0.00    0.07    2.06    0.00   97.81

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.00    0.00    0.00    0.00    0.00  100.00

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.00    0.00    0.20    1.20    0.00   98.60

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.00    0.00    0.00    0.40    0.00   99.60

[root@nglinux ~]# 

 

2. disown command
It runs the command in background, however the command is killed when session is terminated/closed.

[root@nglinux ~]# top &
[1] 4755
[root@nglinux ~]#

[1]+ Stopped top
[root@nglinux ~]# disown -h %1
[root@nglinux ~]#
[root@nglinux ~]# ps -ef | grep -i top
root 5 2 0 18:21 ? 00:00:00 [stopper/0]
root 4755 4390 0 23:37 pts/2 00:00:00 top
root 4760 4390 0 23:37 pts/2 00:00:00 grep -i top

 

3. Setsid Command
Setsid runs in background, gets killed once session is terminated/closed.

[root@nglinux ~]# setsid iostat -c 5
[root@nglinux ~]# Linux 2.6.32-754.el6.i686 (nglinux) 	04/23/2019 	_i686_	(1 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.06    0.00    0.07    1.93    0.00   97.94


[root@nglinux ~]# ps -ef | grep -i avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.20    0.00    0.00    0.00    0.00   99.80


[root@nglinux ~]# ps -ef | grep -i iostat
root      4959     1  0 23:44 ?        00:00:00 iostat -c 5
root      4965  4918  0 23:44 pts/1    00:00:00 grep -i iostat
[root@nglinux ~]# avg-cpu:  %user   %nice %system %iowait  %steal   %idle

 

4. Screen Command

ngelinux:~ saket$ screen
bash-3.2$ top
Processes: 391 total, 4 running, 387 sleeping, 1603 threads                                                                               00:43:39
Load Avg: 1.83, 1.86, 1.79  CPU usage: 7.67% user, 15.34% sys, 76.97% idle   SharedLibs: 208M resident, 47M data, 59M linkedit.
MemRegions: 81093 total, 2553M resident, 67M private, 720M shared. PhysMem: 8109M used (1791M wired), 82M unused.
VM: 1716G vsize, 1089M framework vsize, 3646(0) swapins, 4414(0) swapouts. Networks: packets: 14740498/18G in, 4783755/622M out.
Disks: 772104/15G read, 772984/24G written.

PID   COMMAND      %CPU TIME     #TH   #WQ  #PORTS MEM    PURG   CMPRS  PGRP PPID STATE    BOOSTS          %CPU_ME %CPU_OTHRS UID  FAULTS   COW
5390  top          3.1  00:00.47 1/1   0    23     5704K  0B     0B     5390 5344 running  *0[1]           0.00000 0.00000    0    4165+    104 
5381  auditd       0.0  00:00.01 3     2    31-    644K-  0B     0B     5381 1    sleeping *0[1]           0.00000 0.00000    0    715+     200 
5344  bash         0.0  00:00.00 1     0    19     636K   0B     0B     5344 5343 sleeping *0[1]           0.00000 0.00000    505  633      177 
5343  login        0.0  00:00.03 2     1    30     1100K  0B     0B     5343 5342 sleeping *0[9]           0.00000 0.00000    0    1578     178 

### Detach your screen session by pressing Ctrl+d
### Now your process is running in background on the screen session
### you can reattach with detached screen by using command "screen -r ID" 
### where ID can be grepped from "screen -ls" command.

 

5. tmux command
tmux works similar to screen command.
a. Run “tmx” commmand.
b. A new shell session is started. Run the required command, say, “top”
c. Detach the session by pressing Ctrl+b and then d.
d. Check the running sessions with command “tmux list-sessions”
e. Attach to a session using command “tmux attach-session -t 0”.

In the end, we can conclude the effective ways to achieve this are:
1. nohup and & (Most widely used)
2. Screen command
3. tmx command
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments