zig
zig

Reputation: 47

why does this script not work with nohup but ok without?

I have a bash script as below. When I simply run './my 2', it successfully starts and prints the expected output every 10 seconds. But I want to run it with no hang up so I do 'nohup ./my 2', however this does not work and generates an error shown below (at the very end of this post)

Script:

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

Expected output:

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 error, it seems it complains about the array creation at line 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

any ideas please?

Upvotes: 1

Views: 2190

Answers (1)

Inian
Inian

Reputation: 85550

You don't have a shebang !#/bin/bash at the beginning of the script, in all probability, it could be run from a standard shell /bin/sh which could have caused the problem, since it was designed to work only with standard features. On seeing the line 4, the shell sees it as a syntax error implying that parenthesis doesn't mean anything to it in its context.

Since you use bash features, the first line of the file must always be #!/bin/bash

Upvotes: 2

Related Questions