0
#!/bin/bash
( 
  flock -n 200 || exit 1
  # commands executed under lock 
  sleep 3
  echo "TEST"
) 200 > /home/nis/Scripts/lock.txt

Running this scrip gets me this error:

lock.sh: 7: lock.sh: Syntax error: word unexpected

I don't get why this happens. It works on my QNAP (Busybox)

  • xhienne's answer addresses your main problem, but looking at this I wonder if it's doing what you want. If all you want is to ensure the process only runs one at a time, that will work, but it would be almost useless for anything else. – Julie Pelletier Jan 14 '17 at 18:31
1

Suppress the space after 200:

...
) 200> /home/nis/Scripts/lock.txt

What 200> does is to redirect file descriptor 200 to the given file. You then use that file descriptor with flock to lock the file.

  • thanks.but no change - samme error also tried Sudo. Same error. :/ – Nis Jan 15 '17 at 7:44
  • @Nis Start over with a brand new file: copy-paste your script except the last line, then copy-paste the last line from my answer. It should work (works for me) – xhienne Jan 15 '17 at 9:45
  • I was so hoping for this to work. I wrote the whole thing in a new file. However, I wrote the script on a mac (EditRocket) and saved on samba network drive, then changed permissions (777 just to be sure) but..let me try to do it from scratch locally.. – Nis Jan 15 '17 at 21:08
  • No. Same thing. Really frustrating. Could i have installed the "wrong" flock? its: flock -V : flock from util-linux 2.27.1 – Nis Jan 15 '17 at 21:21
  • ok, this adds to my confusion. If I run this as "bash 'scriptname.sh" then there are no troubles.. anyne can explain this? (and whats the difference between sh and bash calling the script?) Thanks – Nis Jan 16 '17 at 6:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.