我有一个 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
有什么想法吗?
最佳答案
脚本开头没有 shebang !#/bin/bash
,很可能它可以从标准 shell /bin/sh
这可能会导致问题,因为它被设计为仅适用于标准功能。在看到行 4
时,shell 将其视为语法错误,暗示括号在其上下文中没有任何意义。
由于您使用 bash 功能,文件的第一行必须始终是 #!/bin/bash
关于linux - 为什么这个脚本不能与 nohup 一起使用但没有?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37179740/