LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-11-2009, 08:59 AM   #1
scriptblues
LQ Newbie
 
Registered: Jun 2009
Posts: 13

Rep: Reputation: 0
Is there any locking mechanism in linux


I have a problem as mentioned below. Would like to know for some suitable fix for this.

In my directory structure (with some test scripts in that directory), as soon you enter some directory, one is supposed to run some pre-run script and then run the test scripts in that directory.

When they move to another directory, similarly they have to run respective pre-run script of that directory.

My problem is, people are running the pre-run script , running their scripts in that directory and are supposed to run post-run script of that directory (Which basically reset the flags set in pre-run).
But people are just moving to another directory without running post-run script. How can I tackle this problem.

Is there any lock mechanism in Linux, which basically locks the running of pre-run scripts back-to-back.

Any solution to this problem is welcome
 
Old 06-11-2009, 09:07 AM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
How about running pre-run, scripts and post-run from one additional script?
 
Old 06-11-2009, 09:14 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
You can use a function to substitute the built-in cd:
Code:
function cd() {
  <post-run>
  builtin cd "$@"
  <pre-run>
}
 
Old 06-11-2009, 09:22 AM   #4
john test
Member
 
Registered: Jul 2008
Distribution: ubuntu 9.10
Posts: 527
Blog Entries: 1

Rep: Reputation: 35
Can you prepend the funnctionality of the Post-Run script to the beginning of the Pre-run script?

if the last user fails to run the post-run script, the following user will do so prior to setting the flags for the new run.

Not elegant, but might suffice.

Last edited by john test; 06-11-2009 at 09:25 AM.
 
Old 06-11-2009, 09:39 AM   #5
scriptblues
LQ Newbie
 
Registered: Jun 2009
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks colucix for your suggestion. Even I thought of this overloading of cd at first instance, but this may irk other users of this system.

Thanks john test for your suggestion. But this may not suffice, as because I would not know which post-run script to run, if i just prepend post-run to pre-run. Because, user may come to this directory from any directory, And I need only respective directories post run to be run.

I was thinking of some lock in /tmp directory, Which I can check in every pre-run file and if not present then, I would proceed running pre-run or else, get the name of the lock (which I will create like pre-run-<directory name>, run the post-run of this directory name, then create lock for this pre-run and go on like that ...
Any improved suggestions ...
 
Old 06-11-2009, 09:42 AM   #6
scriptblues
LQ Newbie
 
Registered: Jun 2009
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks Wim Sturkenboom for your suggestion, but user may wish to run only a single script in the directory .
If I write a script to run pre-run, run scripts, post-run script, then he is always forced to run all scripts in the directory.
I want to give the user freedom to run his intended script.
But for this he had to run pre-run script, run his intended script and mandatorily run post-run script.
The users are running pre-run script to make their intended script. But they are not running post-run script (Which is not making the flags unset which were set in pre-run script.)
 
Old 06-11-2009, 10:45 AM   #7
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
What you want to accomplish is usually handled by "flags." In you case, it would be simple to create a "per-user" directory in /tmp and do a touch /tmp/<user>/<directory name> when run the "start" script, and a rm -f /tmp/<user>/<directory name> when they run the "exit" script.

All the "start" scripts could check for the existence of any "unclosed" directories for the user.

The "test" scripts could also, individually, check that the "start" script had been run (and run it) before they run.

Basically, just use the existence of some (zero length) file to flag various states.

Here's some (untested) code:
Code:
#!/bin/bash
function at_start()
{
  here=$(basename pwd)
  [ -d "/tmp/locks/$USER" ] || mkdir "/tmp/locks/$USER"
  if [ -e "/tmp/locks/$USER/$here" ]; then
     echo Already started in this ("$here") directory.
  else
     touch "/tmp/locks/$USER/$here"
  fi
  for dir in "/tmp/locks/$USER/*";do
    [ "$dir" = "$here" ] && continue
    read -n 1 -p "You have not finished directory $dir. Do you want to do that now? (Y/n)" ans
    case "$ans" in
      n|N) break;;
      *) ../$dir/finalization;;
    esac
  done
}
function while_running()
{
  here=$(basename pwd)
  [ -e "/tmp/locks/$USER" ] || ./initialization
}
function at_end()
{
  here=$(baseneme pwd)
  if [ -e "/tmp/locks/$USER/$here ]
  then
    rm -f "/tmp/locks/$USER/$here ]
  else
    echo You can't run the finalization script unless the initialization script has been run.
  fi
}
 
Old 06-12-2009, 06:57 AM   #8
scriptblues
LQ Newbie
 
Registered: Jun 2009
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks PTrenholme, With the mentioned approach I was able to develop the script.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux Traditional File Access Mechanism dolphine Linux - General 2 09-27-2006 03:43 PM
secure mechanism for encryption/decryption on linux Synesthesia Linux - Security 10 09-04-2006 03:09 PM
Application Error Logging mechanism in Linux kidskc Programming 2 11-03-2005 03:11 PM
Linux locking up, what should I do? M$ISBS Linux - General 3 06-25-2005 07:43 PM
Linux keeps locking up!!!! yzrider210 Linux - Newbie 12 11-18-2003 08:44 PM


All times are GMT -5. The time now is 02:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration