2012-05-04 77 views
1

我有一個小腳本,連續檢查某些條件,只要條件滿足,程序應該執行。這可以做到嗎?我想使用crontab腳本每5分鐘運行一次,但現在我希望不用crontab完成如何在不使用crontab的情況下在後臺連續運行腳本

+0

看到這個接受的答案http://stackoverflow.com/questions/525247/how-do-i-daemonize-an-arbitrary-script-in-unix – tommasop

+0

可能的重複[如何在不使用crontab的情況下連續運行unix腳本。](http://stackoverflow.com/questions/10445201/how-can-i-continously-run-a-unix-script-in -background-without-using-crontab) – WrightsCS

回答

1

您可能想要先創建一個無限循環,然後在該循​​環內您可能想要驗證您的條件或等待一下。由於您沒有提到您想使用哪種腳本語言,因此我將爲該示例編寫僞代碼。給我們更多關於腳本語言的信息,也許還有條件。

實例僞代碼:在你的輸入代碼SH

# Defining a timeout of 1 sec, so checking the condition every second 
timeout = 1 

# Running in background infinitely 
while true do 
    # Let's check the condition 
    if condition then 
    # I got work to do 
    ... 
    else 
    # Let's wait a specified timeout, not to block the system 
    sleep timeout 
    endif 
endwhile 

#!/bin/sh 
PATH=/bin:/usr/bin:/sbin:/usr/sbin 

# Defining a timeout of 1 hour 
timeout=3600 

while true 
do 
    case 'df /tmp' in 
    " "[5-9]?%" ") rm -f /tmp/af.* 
     ;; 
    *) 
     sleep $timeout 
     ;; 
    esac 
done 

然後,您可以使用 'nohup的' 從外殼程序運行此腳本:

nohup yourscript & 
+0

實際上,我在unix中開發了一個用於檢查目錄空間的腳本,如果超過了某個限制,它將刪除該磁盤的文件。現在我想讓腳本在連續運行或每1小時後運行。可以請你幫助我,因爲我不能使用crontab – Atul

+0

你使用bash腳本嗎? ksh腳本? Perl腳本? python腳本?等等你是什麼意思「連續跑步」?你想讓它成爲守護進程嗎?要在啓動時運行?在關閉shell後繼續運行? – Huygens

+0

這裏是腳本#!/ bin/sh PATH =/bin:/ usr/bin:/ sbin:/ usr/sbin case'df/tmp'in *「」[5-9]?%「」 *)rm -f /tmp/af.* ;; esac ...這需要跑每個說1小時 – Atul

相關問題