2012-05-04 78 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

相关问题