How to Put Current Running Program in Background without Being T

  • 时间:2020-09-11 08:23:45
  • 分类:网络文摘
  • 阅读:87 次

As you may know that we can use screen command to start a session when you SSH that allows you to run a few long commands that won’t terminate when the session is disconnected.

However, if you forget to do so, and you have a long-running application running, and we can follow the steps to put it in the background, which won’t be killed when the session is ended.

Let’s prepare a long-running script/application in BASH:

1
2
3
4
5
6
7
8
$ cat someApplication
#!/bin/bash
while :
do
    echo print some output
    sleep 2
done
$
$ cat someApplication
#!/bin/bash
while :
do
    echo print some output
    sleep 2
done
$

Pause the Current Running Program

You can press Ctrl + Z – which will pause the current running program:

1
2
3
4
5
6
7
8
9
$ someApplication
print some output
print some output
print some output
..
..
Ctrl + Z <- Keyboard interrupt
[1]+  Stopped                 ./someApplication
$
$ someApplication
print some output
print some output
print some output
..
..
Ctrl + Z <- Keyboard interrupt
[1]+  Stopped                 ./someApplication
$

Put the Suspended Program in Background

Then, we need to run bg to put the suspened program in the background. You will see the script printing output and you will have access to the prompt.

1
2
3
4
$ bg
[1]+ ./someApplication &
$ print some output
$ print some output
$ bg
[1]+ ./someApplication &
$ print some output
$ print some output

Then Disown the Program

Then, we need to run disown command to return the ownership of the program, which should make the program keep running in the background until being killed or host rebooted.

1
2
$ disown
$
$ disown
$

Close the current SSH session, then login back:

1
2
$ ps augx | grep someApplication
helloacm   29695  0.0  0.0  15244  3192 ?        S    19:31   0:00 /bin/bash ./someApplication
$ ps augx | grep someApplication
helloacm   29695  0.0  0.0  15244  3192 ?        S    19:31   0:00 /bin/bash ./someApplication

As you can see, the application is still alive. However, we cannot bring the application back to foreground as we already lost the ownership by issuing the disown command. We can kill it to terminate it although.

1
$ kill 29695
$ kill 29695

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
萝卜的3种吃法可以防治冬季常见病  食品之辟谣系列:喝豆浆易患乳腺癌  食品之辟谣系列:忽悠美容丰胸减肥的食物  冬季预防食物中毒及中毒后急救措施  四种常见的食物可以排出身体毒素  男人多吃一些香蕉对身体大有好处  冬至时节常吃6种传统食物可补阳防寒  南方黑芝麻糊大肠菌群超标 5批次产品不合格  烹饪技巧之烹调鸡蛋过程中的常见错误  全新认识冬季当家菜大白菜的营养价值 
评论列表
添加评论