<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Locking files from a shell script in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/locking-files-from-a-shell-script/m-p/5024698#M97367</link>
    <description>Thanks all,&lt;BR /&gt;&lt;BR /&gt;Well the file does not have to be locked the whole time.&lt;BR /&gt;Even its existance could be enough.&lt;BR /&gt;&lt;BR /&gt;Is there someting like:&lt;BR /&gt;&lt;BR /&gt;Command : create_file&lt;BR /&gt;errorlevel: 0 = file could be created&lt;BR /&gt;errorlevel: 1 = file allready exists&lt;BR /&gt;&lt;BR /&gt;then the construction could be:&lt;BR /&gt;&lt;BR /&gt;# check if if some process is working at this moment&lt;BR /&gt;&lt;BR /&gt;create_file file&lt;BR /&gt;if [ $? -eq 0 ]&lt;BR /&gt;then&lt;BR /&gt;      do_my_thing&lt;BR /&gt;      rm file&lt;BR /&gt;else&lt;BR /&gt;      wait_for_other_process&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;Kl@@s&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
    <pubDate>Thu, 25 Jan 2007 05:37:06 GMT</pubDate>
    <dc:creator>Klaas D. Eenkhoorn</dc:creator>
    <dc:date>2007-01-25T05:37:06Z</dc:date>
    <item>
      <title>Locking files from a shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/locking-files-from-a-shell-script/m-p/5024691#M97360</link>
      <description>Dear all,&lt;BR /&gt;&lt;BR /&gt;Who can help . . .&lt;BR /&gt;What command can create a file from a shell script, run as root, and gives no error if the creation succeeds but generates an errorlevel if the file already exists ??&lt;BR /&gt;&lt;BR /&gt;touch does'nt&lt;BR /&gt;cat doesn't &lt;BR /&gt;&lt;BR /&gt;What does . . .&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Klaas Eenkhoorn&lt;BR /&gt;</description>
      <pubDate>Thu, 25 Jan 2007 04:34:27 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/locking-files-from-a-shell-script/m-p/5024691#M97360</guid>
      <dc:creator>Klaas D. Eenkhoorn</dc:creator>
      <dc:date>2007-01-25T04:34:27Z</dc:date>
    </item>
    <item>
      <title>Re: Locking files from a shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/locking-files-from-a-shell-script/m-p/5024692#M97361</link>
      <description>Sorry the subject is a bit misleading . . .&lt;BR /&gt;The command shoud be used for a sort of locking mechanism based on the exsistance of a file.&lt;BR /&gt;&lt;BR /&gt;a construction of &lt;BR /&gt;if [ -f file]&lt;BR /&gt;then &lt;BR /&gt;        wait_for_compleation&lt;BR /&gt;else&lt;BR /&gt;        touch file&lt;BR /&gt;        do_something&lt;BR /&gt;        rm file&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;is to slow, during the time needed to get from the if -f to the touch statment some other proces, doning the same commandsequence, does the same action 'do_smomething' instead of waiting.&lt;BR /&gt;&lt;BR /&gt;Kl@@s</description>
      <pubDate>Thu, 25 Jan 2007 04:39:53 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/locking-files-from-a-shell-script/m-p/5024692#M97361</guid>
      <dc:creator>Klaas D. Eenkhoorn</dc:creator>
      <dc:date>2007-01-25T04:39:53Z</dc:date>
    </item>
    <item>
      <title>Re: Locking files from a shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/locking-files-from-a-shell-script/m-p/5024693#M97362</link>
      <description>Klaas,&lt;BR /&gt;why don't you test for esistance of the file first ?&lt;BR /&gt;&lt;BR /&gt;if [ -f filename ]&lt;BR /&gt;then&lt;BR /&gt;echo "file found"&lt;BR /&gt;else&lt;BR /&gt;echo "file not found"&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;See "man test"&lt;BR /&gt;&lt;BR /&gt;You could also try "mv -i", which will ask for permission, before overwriting an existing file.&lt;BR /&gt;</description>
      <pubDate>Thu, 25 Jan 2007 04:39:56 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/locking-files-from-a-shell-script/m-p/5024693#M97362</guid>
      <dc:creator>Peter Godron</dc:creator>
      <dc:date>2007-01-25T04:39:56Z</dc:date>
    </item>
    <item>
      <title>Re: Locking files from a shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/locking-files-from-a-shell-script/m-p/5024694#M97363</link>
      <description>Hy&lt;BR /&gt;&lt;BR /&gt;I would also try something like:&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/ksh&lt;BR /&gt;&lt;BR /&gt;filename=blah&lt;BR /&gt;&lt;BR /&gt;if [ -a ${filename} ]&lt;BR /&gt;then&lt;BR /&gt;        echo "file already exists"&lt;BR /&gt;else&lt;BR /&gt;        touch ${filename}&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;Regards&lt;BR /&gt;</description>
      <pubDate>Thu, 25 Jan 2007 04:44:53 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/locking-files-from-a-shell-script/m-p/5024694#M97363</guid>
      <dc:creator>Oviwan</dc:creator>
      <dc:date>2007-01-25T04:44:53Z</dc:date>
    </item>
    <item>
      <title>Re: Locking files from a shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/locking-files-from-a-shell-script/m-p/5024695#M97364</link>
      <description>Klass,&lt;BR /&gt;missed your additional post by 3 seconds, so my first post was based on initial description.&lt;BR /&gt;&lt;BR /&gt;Unix places the detailed control over access to files on the programmer, so I think in your case, your application will have to open the file with read/write exclusive.&lt;BR /&gt;&lt;BR /&gt;For example, root can vi a file and another session can remove the currently edited file, without warning. So if you quit your editor without saving, you have lost the whole file !</description>
      <pubDate>Thu, 25 Jan 2007 04:52:44 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/locking-files-from-a-shell-script/m-p/5024695#M97364</guid>
      <dc:creator>Peter Godron</dc:creator>
      <dc:date>2007-01-25T04:52:44Z</dc:date>
    </item>
    <item>
      <title>Re: Locking files from a shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/locking-files-from-a-shell-script/m-p/5024696#M97365</link>
      <description>Hi Klaas,&lt;BR /&gt; &lt;BR /&gt;afaik, there is no shell command to exercise a file locking regime similar to the fcntl() syscall.&lt;BR /&gt;But such a thing possibly may exist, and simply  has escaped my notice so far.&lt;BR /&gt;Anyway, even use of fcntl() would only implement "advisory locking" if I recall correctly, meaning that only another program that was equally written to be locking considerate would do so.&lt;BR /&gt;For shell scripts I think you will have to prescribe a simple logic that if a certain lock file exists the execution will be discontinued for a contending process.&lt;BR /&gt;You could look at init scripts for inspirations.&lt;BR /&gt;For instance many daemons store their pid in a file like /var/run/daemon_name.pid&lt;BR /&gt;whose existence prevents a second start of that process.&lt;BR /&gt;On linux they e.g. use the /var/lock/subsystem directory in a similar manner.&lt;BR /&gt;</description>
      <pubDate>Thu, 25 Jan 2007 04:54:08 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/locking-files-from-a-shell-script/m-p/5024696#M97365</guid>
      <dc:creator>Ralph Grothe</dc:creator>
      <dc:date>2007-01-25T04:54:08Z</dc:date>
    </item>
    <item>
      <title>Re: Locking files from a shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/locking-files-from-a-shell-script/m-p/5024697#M97366</link>
      <description>I don't see anything better with Peter's and  Oviwan's fragments.  Perhaps they were answering the initial question?&lt;BR /&gt;&lt;BR /&gt;You could do rm and check for errors.  That's pretty atomic.  ;-)&lt;BR /&gt;&lt;BR /&gt;And perhaps a mkdir on another directory if the first test passes?&lt;BR /&gt;&lt;BR /&gt;Perhaps for your initial question about root, using mkdir/rmdir will work consistently.</description>
      <pubDate>Thu, 25 Jan 2007 04:57:06 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/locking-files-from-a-shell-script/m-p/5024697#M97366</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2007-01-25T04:57:06Z</dc:date>
    </item>
    <item>
      <title>Re: Locking files from a shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/locking-files-from-a-shell-script/m-p/5024698#M97367</link>
      <description>Thanks all,&lt;BR /&gt;&lt;BR /&gt;Well the file does not have to be locked the whole time.&lt;BR /&gt;Even its existance could be enough.&lt;BR /&gt;&lt;BR /&gt;Is there someting like:&lt;BR /&gt;&lt;BR /&gt;Command : create_file&lt;BR /&gt;errorlevel: 0 = file could be created&lt;BR /&gt;errorlevel: 1 = file allready exists&lt;BR /&gt;&lt;BR /&gt;then the construction could be:&lt;BR /&gt;&lt;BR /&gt;# check if if some process is working at this moment&lt;BR /&gt;&lt;BR /&gt;create_file file&lt;BR /&gt;if [ $? -eq 0 ]&lt;BR /&gt;then&lt;BR /&gt;      do_my_thing&lt;BR /&gt;      rm file&lt;BR /&gt;else&lt;BR /&gt;      wait_for_other_process&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;Kl@@s&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 25 Jan 2007 05:37:06 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/locking-files-from-a-shell-script/m-p/5024698#M97367</guid>
      <dc:creator>Klaas D. Eenkhoorn</dc:creator>
      <dc:date>2007-01-25T05:37:06Z</dc:date>
    </item>
    <item>
      <title>Re: Locking files from a shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/locking-files-from-a-shell-script/m-p/5024699#M97368</link>
      <description>My suggestion is to use 'ln -s'&lt;BR /&gt;&lt;BR /&gt;The command is exclusive (e.g. if the file already exists, a failure occurs) and is done in a single step (e.g. there is no if else statement). This should eliminate your contention problem.&lt;BR /&gt;&lt;BR /&gt;Cheers,&lt;BR /&gt;BYG.</description>
      <pubDate>Thu, 25 Jan 2007 05:44:55 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/locking-files-from-a-shell-script/m-p/5024699#M97368</guid>
      <dc:creator>Goh Boon Yeow</dc:creator>
      <dc:date>2007-01-25T05:44:55Z</dc:date>
    </item>
    <item>
      <title>Re: Locking files from a shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/locking-files-from-a-shell-script/m-p/5024700#M97369</link>
      <description>Sorry. Found myself being ambiguous. The following example should make things clear:&lt;BR /&gt;&lt;BR /&gt;ln -s whatever lockfile&lt;BR /&gt;if [ $? -eq 0 ]; then&lt;BR /&gt;  do what you need to do&lt;BR /&gt;else&lt;BR /&gt;  throw some error&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;Or you could wrap the whole thing within a function and test the result of the function to do what needs to be done:&lt;BR /&gt;file_lock () {&lt;BR /&gt;  dowhat="$1"&lt;BR /&gt;  lockfile="$2"&lt;BR /&gt;  case "$dowhat" in&lt;BR /&gt;  lock)&lt;BR /&gt;    ln -s whatever $lockfile&lt;BR /&gt;    return $?&lt;BR /&gt;    ;;&lt;BR /&gt;  unlock)&lt;BR /&gt;    rm -f $lockfile&lt;BR /&gt;    ;;&lt;BR /&gt;  *)&lt;BR /&gt;    echo "What's this?"&lt;BR /&gt;    return 1&lt;BR /&gt;    ;;&lt;BR /&gt;  esac&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;Allows for more flexibility in that you can lock/unlock with the same function and establish the name of the file you want to lock with.</description>
      <pubDate>Thu, 25 Jan 2007 05:58:11 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/locking-files-from-a-shell-script/m-p/5024700#M97369</guid>
      <dc:creator>Goh Boon Yeow</dc:creator>
      <dc:date>2007-01-25T05:58:11Z</dc:date>
    </item>
    <item>
      <title>Re: Locking files from a shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/locking-files-from-a-shell-script/m-p/5024701#M97370</link>
      <description>Thanks,&lt;BR /&gt;&lt;BR /&gt;The ln solution works for me !&lt;BR /&gt;Good thinking.&lt;BR /&gt;&lt;BR /&gt;Thanks for the replies.&lt;BR /&gt;&lt;BR /&gt;Kl@@s</description>
      <pubDate>Thu, 25 Jan 2007 06:51:28 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/locking-files-from-a-shell-script/m-p/5024701#M97370</guid>
      <dc:creator>Klaas D. Eenkhoorn</dc:creator>
      <dc:date>2007-01-25T06:51:28Z</dc:date>
    </item>
    <item>
      <title>Re: Locking files from a shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/locking-files-from-a-shell-script/m-p/5024702#M97371</link>
      <description>just for completion, it seems you are creating a script that can be called while the previous execution might still be running, but they cannot run at the same time.&lt;BR /&gt;&lt;BR /&gt;the clean unix way is to use .pid files in the /var/run directory. this file contains the pid of the active running process.&lt;BR /&gt;&lt;BR /&gt;this has several interesting advantages;&lt;BR /&gt;- you know there is (still) something running&lt;BR /&gt;- it is easy to kill, by just cating the pid file.&lt;BR /&gt;- all files are at one location&lt;BR /&gt;etc.</description>
      <pubDate>Mon, 29 Jan 2007 03:42:12 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/locking-files-from-a-shell-script/m-p/5024702#M97371</guid>
      <dc:creator>dirk dierickx</dc:creator>
      <dc:date>2007-01-29T03:42:12Z</dc:date>
    </item>
  </channel>
</rss>

