为什么这个脚本不能用nohup工作但是没有用?

时间:2016-05-12 07:10:31

标签: linux bash nohup

我有一个bash脚本如下。当我只运行'./my 2'时,它会成功启动并每10秒打印一次预期的输出。但是我想在没有挂断的情况下运行它,所以我做'nohup ./my 2',但是这不起作用并产生如下所示的错误(在这篇文章的最后)

脚本:

echo "Time now is $(date), first task will begin after $1 seconds, then at the hours of [6, 11, 16, 19]";
sleep $1s;

array=(5 10 15 18)

while :
do
    startReadable=$(date);
    start=$(date +%s);
    currentHour=$(date +"%H");
    nextGap=11;
    currentWindow=5;

    for i in ${array[*]}
    do
        diff=$(expr $i - $currentHour)
        if [ $diff -gt 1 ]
        then
            nextGap=$diff
            currentWindow=$i
            break;
        fi      
    done

    prevGap=0
    if [ $currentWindow -eq 5 ]
    then 
        prevGap=11
    else
        if [ $currentWindow -eq 18 ]
        then
            prevGap=3
        else
            prevGap=5
        fi
    fi

    echo ">> Now start computing the current task ($startReadable), previous scoring window is $prevGap hours";
    echo ">> (The next task after this will be in $nextGap hours ...)"
    sleep 10    
done

预期产出:

Time now is Thu May 12 07:07:16 UTC 2016, first task will begin after 2 seconds, then at the hours of [6, 11, 16, 19]
>> Now start computing the current task (Thu May 12 07:07:18 UTC 2016), previous scoring window is 5 hours
>> (The next task after this will be in 3 hours ...)
>> Now start computing the current task (Thu May 12 07:07:28 UTC 2016), previous scoring window is 5 hours
>> (The next task after this will be in 3 hours ...)
>> Now start computing the current task (Thu May 12 07:07:38 UTC 2016), previous scoring window is 5 hours
>> (The next task after this will be in 3 hours ...)

nohup错误,它似乎抱怨第4行的数组创建:

Time now is Thu May 12 07:02:23 UTC 2016, first task will begin after 2 seconds, then at the hours of [6, 11, 16, 19]
./my.sh: 4: ./my.sh: Syntax error: "(" unexpected

请问任何想法?

1 个答案:

答案 0 :(得分:1)

在脚本开头没有shebang !#/bin/bash,很可能它可以从标准shell /bin/sh运行,这可能导致问题,因为它是设计的仅适用于标准功能。在看到行4时,shell将其视为语法错误,暗示括号在其上下文中对其没有任何意义。

由于您使用bash功能,因此文件的第一行必须始终为#!/bin/bash