Register
It is currently Mon Nov 24, 2014 3:32 pm

Lock file usage


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Sun Nov 13, 2011 1:46 pm   

Joined: Thu Nov 10, 2011 7:31 pm
Posts: 21
Can someone make a program that makes simple use of a lock file? I need it to be so 1. people cannot run the script more than once at a time and 2. so people have to run it as root


Top
 Profile  
 PostPosted: Mon Nov 14, 2011 4:27 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
Hi

2 very easy things to do, and also very easy to find out how with a quick google. If this is homework you're so gonna fail +:)

Code:
#!/bin/bash
# a place to stash the lock file
lockfile=/tmp/mylockfile

# $UID=0 for root
if [ "$UID" != 0 ]; then
  echo "Need root!"
  exit
fi

# if the lockfile already exists then quit
if [ -e "$lockfile" ]; then
  echo "Lockfile exists!"
  exit
else
  touch "$lockfile"
fi

# my code
ls -l "$lockfile"

# remove the lockfile when done
rm "$lockfile"


I hope this helps.
DW


Top
 Profile  
 PostPosted: Mon Nov 14, 2011 10:18 am   

Joined: Thu Nov 10, 2011 7:31 pm
Posts: 21
I haven't tried it, but it looks good! I just did not know how to check for existence of a file.


Top
 Profile  
 PostPosted: Mon Nov 14, 2011 10:34 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
blendmaster345 wrote:
I haven't tried it, but it looks good! I just did not know how to check for existence of a file.


That's part of the basics.

Read the output of the following command:

Code:
help test


Top
 Profile  
 PostPosted: Mon Nov 14, 2011 5:09 pm   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
The code shown here is not safe. If two processes try to create the lockfile at the same time, they both might find the file not yet existing, but only one should be able to create the lock. Using flock or lockfile should be safer.


Top
 Profile  
 PostPosted: Tue Nov 15, 2011 3:40 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
choroba wrote:
The code shown here is not safe. If two processes try to create the lockfile at the same time, they both might find the file not yet existing, but only one should be able to create the lock. Using flock or lockfile should be safer.


indeed :)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP