4

What is wrong with "do" in the following:

nohup while true; do ./mysync.sh; sleep 1800; done &

Keep getting the following syntax error:

sh: Syntax error: "do" unexpected

If I execute the command without the nohup, it works fine and doesn't generate errors.

2 Answers 2

3

Problem : the shell is seeing separate commands like "nohup while true" & "do ./mysync.sh" & "sleep 1800" & "done &".
Here, the shell sees a problem with unexpected "do".

Solution : you would have to group your commands into one unit, either with quoting or by making a small shell script. Give this unit to nohup and it will work.

(Solution A) Join the commands like this:
nohup bash -c "while true; do ./mysync.sh; sleep 1800; done &"
(Solution B) Put the commands while true; do ./mysync.sh; sleep 1800; done & into mywrapper.sh and run this:
nohup mywrapper.sh

1
  • @orlandolopez , Nice to know that. I assume you selected Solution A ?
    – Prem
    Commented Feb 26, 2017 at 15:05
1

I had the same problem and tried @Prem's solution. I got permission denied error. Also, if you want to run nohup with while loop using a variable from a file, double quotation marks dont work. It looks for the variable in shell environment, not from the file. Later, I run the same command with single quotation mark without error:

nohup bash -c 'while read fvar; do bash uniqReads.MT.sh $fvar; done < file.txt > file.log &'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .