Script executable only for one person at same time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script executable only for one person at same time
# 8  
Old 03-01-2010
A quite safe mechanism for locking in a shell script is to create a locking directory. Call mkdir without the -p switch and if you succedd (exit code = 0) then your script can go on, or has to terminate otherwise.

But there still is the problem to remove the locking directory at program termination. If you are using ksh you can define a trap handler for the exit event like:

Code:
LOCKDIR=/tmp/mylock.dir
trap 'rm -rf $LOCKDIR' 0
if mkdir $LOCKDIR
then
    # go on
else
    print -u2 already running.
    exit 1
fi

# 9  
Old 03-04-2010
Hi hergp!

Thank you for your help!
The script works, if i start the script while the script is running, there is the right message "script is running". But if i start it a second time, the mylock.dir directory is removed-> so i can start it though the script is already running. how can i prevent that the script removes the mylock.dir?

Do I have to copy my whole code instead of the "go on" comment?

Now, my code is under the if ... fi

Thank you

Greez Roger
# 10  
Old 03-04-2010
Ah, I see. This issue can be corrected by undefining the trap in the else part of the if statement, like:

Code:
#!/bin/ksh
LOCKDIR=/tmp/mylock.dir
trap 'rm -rf $LOCKDIR' 0
if mkdir $LOCKDIR
then
    # go on
else
    print -u2 already running.
    trap : 0
    exit 1
fi

It's also possible, to move the first trap statement into the "go-on" part:

Code:
#!/bin/ksh
LOCKDIR=/tmp/mylock.dir
if mkdir $LOCKDIR
then
    trap 'rm -rf $LOCKDIR' 0
    # go on
else
    print -u2 already running.
    exit 1
fi

# 11  
Old 03-04-2010
Hey hergp!

I have chosen the second solution. It seems to work correctly =D
Thank you for your help!

Who can mark this theard as resolved? or isnt it necessary?

Greez Roger

Last edited by DarkSwiss; 03-04-2010 at 08:49 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Making any script executable

Hi all, I'm new to Unix so just wanted some help. I've been self learning and came accross a question online that I was trying. It is to make any shell script executable, the name of the file is to be made executable. I would use nano and type in something like #! /bin/bash Chmod +x... (4 Replies)
Discussion started by: HelenaR
4 Replies

2. Shell Programming and Scripting

How to produce a executable Oracle script from bash script?

Hi here's my code ${ORACLE_HOME}/bin/sqlplus /nolog <<!EOF --step 5 create db script start set feedback off set heading off set echo off conn / as sysdba spool ${ORACLE_SID}_db_link.sql SELECT 'CREATE '||DECODE(U.NAME,'PUBLIC','public ')||'DATABASE LINK '||CHR(10)... (2 Replies)
Discussion started by: jediwannabe
2 Replies

3. Shell Programming and Scripting

Set nice value in an executable in a script?

Is it possible to set a nice value for an executable in a script so that every time the executable runs it has this nice value? I'm trying to set aerender (After Effects terminal renderer) to run at +18 by replacing the original aerender script with a bash script with something like this in it: ... (3 Replies)
Discussion started by: scribling
3 Replies

4. Shell Programming and Scripting

Creating executable script--please help

Hi group, I am very beginner in shell scripting and self learning. I am trying to create and executable script to run awk from user defined variables. e.g. suppose from a given file I want to delete some rows or some columns We need to repeat this process for many files. Thus I was... (4 Replies)
Discussion started by: smitra
4 Replies

5. Shell Programming and Scripting

sh script to get unix username of person executing it

Hi, I am writing a script, and I need to incorporate some logic where I can find out the unix username of the person who is executing the script. The issue is , a particular user could have "sesu" ed into a group id. for eg. root, and then executed the script. In that case, instead of root,... (5 Replies)
Discussion started by: neil.k
5 Replies

6. Shell Programming and Scripting

Unable to make script executable

Hello everybody, I'm unable to make my shell script an executable file. The details are as follows: PATH includes my $HOME/bin i.e. /rchome/rc1/bin HOME directory is /rchome/rc1 script name is prep_mig.sh permissions set are 755 It's executing if I give below command sh prep_mig.sh but... (4 Replies)
Discussion started by: jitu.keshwani
4 Replies

7. Shell Programming and Scripting

script run when executable is launched

I need to know how to have a BASH script run every time Firefox is launched, what is the simplest way to do this? (1 Reply)
Discussion started by: glev2005
1 Replies

8. Shell Programming and Scripting

Unable to call executable from script

Now I am using the HP-UX11.11 version. The scripts are runninh in KSH shell. While I wan to call one executable of any Pro*C file, I have got the following error, however the executable is running fine directly. testpri Started at 10.05.200923:40 /usr/lib/dld.sl: Bad magic number for... (0 Replies)
Discussion started by: priyankak
0 Replies

9. UNIX for Dummies Questions & Answers

need help making a script executable

making a script in vi to create a shell script called wherearethey by entering the following script: echo -n "Who are you looking for: "read userif then list=`w | grep $user | cut -c19-30` if then echo "The user $user is logged in from $list" else echo "The user $user is not logged in... (3 Replies)
Discussion started by: curtner
3 Replies

10. Solaris

Executable takes indefinite time to search for shared library

Hi, I have an executable that takes indefinitely long time to search for the libsendfile.so in particular solaris machines only. The library is actually in the /lib folder. Where as it works fine on few of the machines, it takes long time in few others. I have checked crle options. Its... (1 Reply)
Discussion started by: kk2202
1 Replies
Login or Register to Ask a Question