1

I'm having some problems with a bit of script I'm using for making sure a script isn't running twice.

I've been using this for quite some time without much problems, but it is giving me trouble on a server I just upgraded from Ubuntu 15.04 to Ubuntu 15.10, so I think there might be something there.

The code I'm using looks like this:

# locking; make sure we are alone
lockfile-create /var/lock/mylockfile --retry 3 --quiet
result=$?
if [ ${result} -gt 0 ]
then
    # allready running; bye!
    exit 2
fi
lockfile-touch /var/lock/mylockfile &
lockfiletouch="$!"
trap "{ kill ${lockfiletouch}; lockfile-remove /var/lock/mylockfile; }" EXIT

However, this now results sometimes in the following error:

/usr/local/scripts/myscript.sh: line 1:  8173 Terminated              lockfile-touch /var/lock/mylockfile

It happens less than 1% of the times the script is run, but it is run quite often.

Can anyone give me any clue on what might be causing this and how I can prevent this error?

DoppyNL
  • 111
  • 1

1 Answers1

0

According to this, the error is just lockfile-touch being killed by your script.

# bash -x  lock.sh
+ lockfile-create /var/lock/mylockfile --retry 3 --quiet
+ result=0
+ '[' 0 -gt 0 ']'
+ lockfiletouch=3902
+ trap '{ kill 3902; lockfile-remove /var/lock/mylockfile; }' EXIT
+ kill 3902
+ lockfile-remove /var/lock/mylockfile
lock.sh: line 1:  3902 Terminated              lockfile-touch /var/lock/mylockfile
Ryan Babchishin
  • 6,260
  • 2
  • 17
  • 37
  • Then why isn't it giving this message everytime the script is run? – DoppyNL Oct 24 '15 at 10:04
  • @DoppyNL You've removed the contents of your script from here right? It's hard to tell because this one does nothing except exit immediately. Though it does seem to do what it's supposed to do. Does your script normally stay running and rarely/never exit? Or does it run, kill lockfile-touch and exit on a regular basis? – Ryan Babchishin Oct 24 '15 at 10:08
  • The script is quite a bit longer obviously. I did not post the code unrelated to the lockfile. The script should normally be done withing 1 or 2 secs. It almost always exits normally at the end of the script. There are only 2 other exit points using `exit`, but those are logged just before that happens (and nothing is in the log). It might be occuring just in the cases where an earlier call of the script is still busy (because of slow network, timeout...). – DoppyNL Oct 24 '15 at 10:14
  • @DoppyNL Perhaps try running it with `bash -x` to see whether or not it's trying to kill `lockfile-touch` or maybe exiting early without doing that. – Ryan Babchishin Oct 24 '15 at 10:16
  • the trap-line should actually do just that when the script stops. So it appears this is not run in some cases for some reason. I just have no idea why. – DoppyNL Oct 24 '15 at 10:29
  • @DoppyNL Running it as `bash -x myscript.sh` will very very likely tell you what you need to know – Ryan Babchishin Oct 24 '15 at 10:36
  • Yeah, I'm looking at that. Didn't know you could use that, it is very useful. The problem is it does not happen on every call and it is in a crontab (adjusting the crontab now to use that and log results to a file). I've also added some extensive logging now which I should be able to match when the error occurs again and perhaps see what is happening. – DoppyNL Oct 24 '15 at 10:39
  • I've not run into the problem since I last looked at it. I removed my debug-code to see if that was the issue, but it did not come back. Only thing I can think of is it was caused by higher load on the server, which is something I resolved causing the server to have quite a bit less load. – DoppyNL Nov 02 '15 at 08:47