为什么这个脚本不能与 nohup 一起使用但没有?
why does this script not work with nohup but ok without?
我有一个 bash 脚本如下。当我简单地 运行 './my 2' 时,它成功启动并每 10 秒打印一次预期的输出。但是我想 运行 它没有挂断所以我做了 'nohup ./my 2',但是这不起作用并产生如下所示的错误(在这个 post 的最后)
脚本:
echo "Time now is $(date), first task will begin after seconds, then at the hours of [6, 11, 16, 19]";
sleep s;
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
我有一个 bash 脚本如下。当我简单地 运行 './my 2' 时,它成功启动并每 10 秒打印一次预期的输出。但是我想 运行 它没有挂断所以我做了 'nohup ./my 2',但是这不起作用并产生如下所示的错误(在这个 post 的最后)
脚本:
echo "Time now is $(date), first task will begin after seconds, then at the hours of [6, 11, 16, 19]";
sleep s;
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