1. 使用&符號在后台執行命令
你可以在Linux命令或者腳本后面增加&符號,從而使命令或腳本在后台執行,例如:.
$ ./my-shell-script.sh &
2. 使用nohup在后台執行命令
使用&符號在后台執行命令或腳本后,如果你退出登錄,這個命令就會被自動終止掉。要避免這種情況,你可以使用nohup命令,如下所示:
$ nohup ./my-shell-script.sh &
3. 使用screen執行命令
通過nohup和&符號在后台執行命令后,即使你退出登錄,這個命令也會一直執行。但是,你無法重新連接到這個會話,要想重新連接到這個會話,你可以使用screen命令。.
Linux的screen命令提供了分離和重新連接一個會話的功能。當你重新連接這個會話的時候,你的終端和你分離的時候一模一樣。
4. 使用at將一個命令作為批處理執行
使用at命令,你可以讓一個命令在指定的日期和時間運行,例如要在明天上午10點在后台執行備份腳本,執行下面的命令:
$ at -f backup.sh 10 am tomorrow
在批處理模式下執行某些任務需要啟用一些選項。下面的文章會給出詳細解釋:.
- How To Capture Unix Top Command Output to a File in Readable Format
- Unix bc Command Line Calculator in Batch Mode
- How To Execute SSH and SCP in Batch Mode (Only when Passwordless login is enabled)
5. 使用watch連續地執行一個命令
要想按一個固定的間隔不停地執行一個命令,可以使用watch命令,如下所示:
$ watch df -h
原文鏈接:http://www.cnblogs.com/Javame/p/3582885.html
測試:
[root@rusky ~]# nohup ping 192.168.1.100 > /tmp/test_nohup_ping.txt & [1] 2619 [root@rusky ~]# nohup: ignoring input and redirecting stderr to stdout [root@rusky ~]# nohup ping 192.168.1.202 > /tmp/test_nohup_ping2.txt & [2] 2628 [root@rusky ~]# nohup: ignoring input and redirecting stderr to stdout [root@rusky ~]# nohup ping 127.0.0.1 > /tmp/test_nohup_ping3.txt & [3] 2629 [root@rusky ~]# nohup: ignoring input and redirecting stderr to stdout [root@rusky ~]# jobs [1] Running nohup ping 192.168.1.100 > /tmp/test_nohup_ping.txt & [2]- Running nohup ping 192.168.1.202 > /tmp/test_nohup_ping2.txt & [3]+ Running nohup ping 127.0.0.1 > /tmp/test_nohup_ping3.txt & [root@rusky ~]#
ctrl+z 停止進程 ctrl+c 終止進程
[root@rusky ~]# fg nohup ping 127.0.0.1 > /tmp/test_nohup_ping3.txt ^Z [3]+ Stopped nohup ping 127.0.0.1 > /tmp/test_nohup_ping3.txt [root@rusky ~]# jobs [1] Running nohup ping 192.168.1.100 > /tmp/test_nohup_ping.txt & [2]- Running nohup ping 192.168.1.202 > /tmp/test_nohup_ping2.txt & [3]+ Stopped nohup ping 127.0.0.1 > /tmp/test_nohup_ping3.txt
bg:Resume each suspended job jobspec in the background, as if it had been started with &.
fg: Resume jobspec in the foreground, and make it the current job.
這個'+'就是表示在當前窗口下后台默認調用的任務。如下,輸入fg,調用的是[3]job,也就是帶+號的job。
job job編號,調用指定的job
[root@rusky ~]# fg nohup ping 127.0.0.1 > /tmp/test_nohup_ping3.txt ^Z [3]+ Stopped nohup ping 127.0.0.1 > /tmp/test_nohup_ping3.txt
[root@rusky ~]# job 1 bash: job: command not found... [root@rusky ~]# fg 1 nohup ping 192.168.1.100 > /tmp/test_nohup_ping.txt ^C[root@rusky ~]# jobs [2]- Running nohup ping 192.168.1.202 > /tmp/test_nohup_ping2.txt & [3]+ Stopped nohup ping 127.0.0.1 > /tmp/test_nohup_ping3.txt
bg %jobnumber(%可省略)命令激活job3
[root@rusky ~]# jobs [2]- Running nohup ping 192.168.1.202 > /tmp/test_nohup_ping2.txt & [3]+ Stopped nohup ping 127.0.0.1 > /tmp/test_nohup_ping3.txt [root@rusky ~]# [root@rusky ~]# bg 3 [3]+ nohup ping 127.0.0.1 > /tmp/test_nohup_ping3.txt & [root@rusky ~]# jobs [2]- Running nohup ping 192.168.1.202 > /tmp/test_nohup_ping2.txt & [3]+ Running nohup ping 127.0.0.1 > /tmp/test_nohup_ping3.txt &
jobs -l : List process IDs in addition to the normal information.
[root@rusky ~]# jobs -l [1] 2619 Running nohup ping 192.168.1.100 > /tmp/test_nohup_ping.txt & [2]- 2628 Running nohup ping 192.168.1.202 > /tmp/test_nohup_ping2.txt & [3]+ 2629 Stopped nohup ping 127.0.0.1 > /tmp/test_nohup_ping3.txt [root@rusky ~]#
這樣,我們可以用Kill命令加jobs的ID號殺死進程
