1. 程式人生 > >Linux 後臺執行命令

Linux 後臺執行命令

下面的示例統一使用這個每秒列印一次時間的簡單指令碼:

[[email protected]_139_74_centos shell]# cat 10s.sh
#!/bin/bash

for ((i = 0; i < 10; i++)); do
    echo $(date)
    sleep 1
done

正常執行命令時,會阻塞終端,同時將所有輸出資訊列印到終端:

[root@VM_139_74_centos shell]# ./10s.sh
Wed May 30 11:32:13 CST 2018
Wed May
30 11:32:14 CST 2018 Wed May 30 11:32:15 CST 2018 Wed May 30 11:32:16 CST 2018 Wed May 30 11:32:17 CST 2018 Wed May 30 11:32:18 CST 2018 Wed May 30 11:32:19 CST 2018 Wed May 30 11:32:20 CST 2018 Wed May 30 11:32:21 CST 2018 Wed May 30 11:32:22 CST 2018 [root@VM_139_74_centos shell]#

注意,Linux 終端中執行的命令的輸出是可以有快取的。有時候命令執行完了,但是輸出的資訊並不完整,這時候再需要敲其他命令(例如 Ctrl + c)清空快取。

&

命令後面有 & 時,這個命令將會在後臺執行。但是輸出資訊仍會列印到終端,但是此時可以接受其他命令:

[root@VM_139_74_centos shell]# ./10s.sh &
[1] 17257
[root@VM_139_74_centos shell]# Wed May 30 11:32:30 CST 2018
Wed May 30 11:32:31 CST 2018
Wed May 30 11:32:32 CST 2018
Wed May 30 11:32:33 CST 2018
Wed May 30 11:32:34 CST 2018
Wed May 30 11:
32:35 CST 2018 Wed May 30 11:32:36 CST 2018 Wed May 30 11:32:37 CST 2018 Wed May 30 11:32:38 CST 2018 Wed May 30 11:32:39 CST 2018 nohup ./10s.sh & [2] 17290 [1] Done ./10s.sh [root@VM_139_74_centos shell]# nohup: ignoring input and appending output to ‘nohup.out’

& 特點如下:

  • 終端關閉或退出賬戶時,後臺執行的命令會終止
  • 後臺執行的作業一樣會將結果輸出到螢幕上

對於第一個問題,可以在命令前新增 nohup(no hang up)解決:

nohup your-command &

第二個問題可以使用輸出重定向將輸出重定向到檔案:

your-command > out.file 2>&1  & 

其中,your-command > out.file 將命令的標準輸出流 STDOUT 重定向到 out.file 檔案,不再列印到螢幕上。2>&1 將標準錯誤流 STDERR 重定向到標準輸出流(2 與 > 結合表示重定向標準錯誤流 STDERR,&1 表示標準輸出流 STDOUT),而標準輸出流已經重定向到了 out.file 檔案,所以標準錯誤流也會輸出到這個檔案中。最後的 & 表示該命令在後臺執行。

nohup

使用 nohup 命令時,如果沒有指定輸出檔案,此時命令的所有輸出都被重定向到一個名為 nohup.out 的檔案中。

[root@VM_139_74_centos shell]# nohup ./10s.sh &
[1] 17816
[root@VM_139_74_centos shell]# nohup: ignoring input and appending output to ‘nohup.out’
nohup ./60s.sh^C
[1]+  Done                    nohup ./10s.sh

也可以自己指定輸出重定向到的檔案:

[root@VM_139_74_centos shell]# nohup ./10s.sh > myout.file 2>&1 &
[1] 17944
[root@VM_139_74_centos shell]#
[root@VM_139_74_centos shell]# cat myout.file 
nohup: ignoring input
Wed May 30 11:38:36 CST 2018
Wed May 30 11:38:37 CST 2018
Wed May 30 11:38:38 CST 2018
Wed May 30 11:38:39 CST 2018
Wed May 30 11:38:40 CST 2018
Wed May 30 11:38:41 CST 2018
Wed May 30 11:38:42 CST 2018

注意,使用 nohup 後臺執行命令時,需要使用 exit 正常退出當前賬戶,這樣才能保證命令一直在後臺執行。

ctrl + z

掛起前臺程序。將正在前臺執行的命令放到後臺,並且處於暫停狀態。

[[email protected]_139_74_centos shell]# ./10s.sh 
Wed May 30 11:44:52 CST 2018
Wed May 30 11:44:53 CST 2018
^Z
[1]+  Stopped                 ./10s.sh
[[email protected]_139_74_centos shell]# jobs
[1]+  Stopped                 ./10s.sh

jobs

檢視所有後臺執行的命令。

[[email protected]_139_74_centos shell]# nohup ./10s.sh > myout.file 2>&1 &
[2] 18573
[1]   Done                    nohup ./10s.sh > myout.file 2>&1
[[email protected]_139_74_centos shell]# jobs
[2]+  Running                 nohup ./10s.sh > myout.file 2>&1 &

fg

將後臺命令調至前臺,並恢復執行。

如果後臺中有多個命令,可以用 fg %jobnumber 將選中的命令調出,%jobnumber 是通過 jobs 命令查到的後臺正在執行的命令的序號(不是 pid)。

[[email protected]_139_74_centos shell]# ./10s.sh 
Wed May 30 11:44:52 CST 2018
Wed May 30 11:44:53 CST 2018
^Z
[1]+  Stopped                 ./10s.sh
[[email protected]_139_74_centos shell]# jobs
[1]+  Stopped                 ./10s.sh
[[email protected]_139_74_centos shell]# fg 1
./10s.sh
Wed May 30 11:51:31 CST 2018
Wed May 30 11:51:32 CST 2018
^Z
[1]+  Stopped                 ./10s.sh
[[email protected]_139_74_centos shell]#

bg

恢復執行後臺暫停的命令。

如果後臺中有多個命令,可以用 bg %jobnumber 將選中的命令調出,%jobnumber 是通過 jobs 命令查到的後臺正在執行的命令的序號(不是 pid)。

[[email protected]_139_74_centos shell]# jobs
[1]+  Stopped                 ./10s.sh
[[email protected]_139_74_centos shell]# bg
[1]+ ./10s.sh &
[[email protected]_139_74_centos shell]# Wed May 30 11:53:39 CST 2018
Wed May 30 11:53:40 CST 2018
jobs
[1]+  Running                 ./10s.sh &
[[email protected]_139_74_centos shell]# Wed May 30 11:53:41 CST 2018
Wed May 30 11:53:42 CST 2018
Wed May 30 11:53:43 CST 2018
Wed May 30 11:53:44 CST 2018
^C
[1]+  Done                    ./10s.sh
[[email protected]_139_74_centos shell]# 

kill

殺死程序。需要使用通過 ps 命令看到的程序 PID。

[[email protected]_139_74_centos shell]# nohup ./10s.sh &
[1] 20062
[[email protected]_139_74_centos shell]# nohup: ignoring input and appending output to ‘nohup.out’
ps
  PID TTY          TIME CMD
17113 pts/0    00:00:00 bash
20062 pts/0    00:00:00 10s.sh
20064 pts/0    00:00:00 sleep
20074 pts/0    00:00:00 ps
[[email protected]_139_74_centos shell]# kill 20062
[[email protected]_139_74_centos shell]# ps
  PID TTY          TIME CMD
17113 pts/0    00:00:00 bash
20087 pts/0    00:00:00 ps
[1]+  Terminated              nohup ./10s.sh

相關推薦

linux後臺執行命令:&和nohup詳解

&    當在前臺執行某個作業時,終端被該作業佔據;可以在命令後面加上& 實現後臺執行。例如:sh test.sh &  適合在後臺執行的命令有f i n d、費時的排序及一些s h e l l指令碼。在後臺執行作業時要當心:需要使用者

windows/linux後臺執行命令

後臺執行是經常要用到的命令 ,收集了常用的程序管理命令,記錄下來便於以後使用 Windows: 啟動後臺執行 start /b + 命令 如 start /b python test.py 檢視程序 tasklist 殺程序 taskkill /im python.exe

linux後臺執行命令:&和nohup

當我們在終端或控制檯工作時,可能不希望由於執行一個作業而佔住了螢幕,因為可能還有更重要的事情要做,比如閱讀電子郵件。對於密集訪問磁碟的程序,我們更希望它能夠在每天的非負荷高峰時間段執行(例如凌晨)。為了使這些程序能夠在後臺執行,也就是說不在終端螢幕上執行,有幾種

linux 後臺執行命令:&和nohup

1.&  當在前臺執行某個作業時,終端被該作業佔據;可以在命令後面加上& 實現後臺執行。例如:sh test.sh & 適合在後臺執行的命令有f i n d、費時的排序及一些s h e l l指令碼。在後臺執行作業時要當心:需要使用者互動的命令不要放在後臺執

linux後臺執行命令

後臺執行指令碼  monitor.sh &            --shell終端斷掉指令碼就停止運行了 nohup command &         --不掛斷的執行程式 setsid command &        --關閉shell終端

linux後臺執行命令nohup啟動nodejs專案

由於要在後臺執行nodejs命令,故需要用到nohup命令。 原本執行nodejs專案是這樣啟動的: node .\log.io-server.js log.io-server.js是我用於專案啟動的js。但是這樣的啟動,命令視窗一關閉,程序就關閉

Linux 後臺執行命令

& nohup jobs fg bg kill 下面的示例統一使用這個每秒列印一次時間的簡單指令碼: [[email protected]_139_74_centos shell]# cat 10s.sh #!/bin/bash

Linux後臺執行命令或腳本

使用 linu 方法 amp 刪除 執行 安裝 時間 lin 在日常的工作中,我們會遇到執行備份、恢復數據等執行過程時間很長的命令或者腳本程序運行,此時,我們為防止執行過程被打斷,需要將命令或腳本在後臺執行。1)方法1nohup sh reload.sh &2)方法

linux後臺執行程式,不掛斷的執行,注意並沒有後臺執行的功能 &是指在後臺執行,但當用戶推出(掛起)的時候,命令自動也跟著退出

** linux後臺執行程式,不掛斷的執行,注意並沒有後臺執行的功能 &是指在後臺執行,但當用戶推出(掛起)的時候,命令自動也跟著退出 ** 正文 回到頂部 nohup nohup 命令執行由 Command引數和任何相關的 Arg引數指定的命令,忽略所有結束通話(SIG

Linux 後臺執行Java 程式命令

方式一:java -jar shareniu.jar 特點:當前ssh視窗被鎖定,可按CTRL + C打斷程式執行,或直接關閉視窗,程式退出 那如何讓視窗不鎖定? 方式二:java -jar shareniu.jar & &代表在後臺執行。 特定

linux後臺執行&符號、nohup命令、輸出重定向等使用方法

列出若干種情形,便於這幾個命令及引數的理解: 在命令最後加上&符號,表示讓這個程序到後臺去執行,這樣立刻返回到提示符狀態,我們可以接著做下面的事。如:command & 但此時如果這個程序有輸出,還是會顯示出來,這又會干擾到我們的shell視窗。所以可以考慮

Linux 後臺執行指令碼或命令 nohup &

nohup command > myout.file 2>&1 & 命令解析: 使用nohup命令後臺執行命令之後,需要使用exit正常退出當前賬戶,這樣才能保證命令一直在後臺執行。 command>out.file是將comma

linux後臺啟動命令-的原因

hang tails bsp n-n evel 忽略 art 用途 KS linux後臺啟動命令-的原因 學習了:https://blog.csdn.net/saife/article/details/78276014 學習了:https://www.ibm.com/dev

Linux後臺執行的方法 - 關閉、退出不影響

所有 mat data ack job tid scree 快捷 -c 難免會遇到在Linux,後臺執行任務。下面,總結了一下關閉窗口任然在後臺執行的方法,例如 #執行一個打包命令 tar czf /data/backup.tgz /data/backu

plink和pscp命令使用(登錄linux執行命令/linux和Windows服務器文件互拷)

htm putty -s ofo cin bat文件 windows 是把 use 1. 使用Putty提供的plink.exe來自動登陸一個機器並執行命令,完成後自己關閉 bat文件內容如下: D:\Download\Software\Putty-0.60\pli

python nohup linux 後臺執行輸出

遇到問題 nohup python flush.py & 這樣執行,生成了nohup.out檔案,但是內容始終是空的,試了半天也不行。浪費了不少時間。原因    python的輸出又緩衝,導致out.log並不能夠馬上看到輸出。  &nb

linux後臺執行總結

可參考:https://www.ibm.com/developerworks/cn/linux/l-cn-screen/ 1.python xxx.py &> log 然後在另一個終端裡就可以 tail -f log 實時看輸出 2.screen -S 名字 #起什麼名

Linux後臺執行Jar方法

在linux伺服器上執行Jar檔案時通常的方法是: $ java -jar test.jar 1 這種方式特點是ssh視窗關閉時,程式中止執行.或者是執行時沒法切出去執行其他任務,有沒有辦法讓Jar在後臺執行呢: 方法一: $ nohup java -jar

linux後臺執行python程式 nohup

nohup python -u test.py > out.log 2>&1 &nohup sh **.sh > /dev/null 2>&1 &   如上所示:nohup 和 &  組合了後臺執行程式。 可以輸出到 o

nohup 命令後臺執行命令、指令碼

轉載出處:AllenW nohup 命令 用途:不掛斷地執行命令。 語法:nohup Command [ Arg … ] [ & ] 描述:nohup 命令執行由 Command 引數和任何相關的 Arg 引數指定的命令,忽略所有結束通話(SIGHUP)訊號。在登出後使用 n