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
# 1  
Old 03-01-2010
Script executable only for one person at same time

Hi

My question:
Is there a way to lock a script if its already running for other users?

Like if i want to start a script, that is running by another user, there will be a message: Hey, you cant start the script because its running by another user, try it later.



my idea was that the script creates a tmp-file, what will be deleted at the end of the script. If everyone starts the script, there will be a scan: does the tmp-file exist, if yes: error message, if no: you can start the script.

The problem of my idea is that if the script has a crash, the tmp-file still exist and nobody can start the script anymore.

Do you have another, probably a better idea how i can resolve this problem?

Best regards
Roger M.

Last edited by pludi; 03-01-2010 at 09:06 AM.. Reason: removed email
# 2  
Old 03-01-2010
2 possibilities:
trap various signals (eg, EOF, TERM, KILL and INT) and if they occur remove the lock file
or
Write your current PID to the lock file. On start, check if the file exists and if there is a process with that PID and if it's named the same as the script. If not, run, exit otherwise.
# 3  
Old 03-01-2010
Here is one way to do that without temporary files.
Code:
ps -eaf | grep test.sh | grep -v grep

if [ $? = 0 ]
then
echo "Script already running"
fi

# 4  
Old 03-01-2010
A better way to do that is at the start of the script do the following.

- Check whether your script runs by using ps command.

For example,if your script name is script.sh,then check the following.

Code:
ps ajx | egrep "[0-9]+:[0-9]+[ ]*sh script.sh"|rev|cut -d ' ' -f 1 | rev

Store the above output in the variable and checks whether it is equal to script.sh.If it matched,don't run the process.Else,run the process.

Last edited by vivekraj; 03-01-2010 at 09:21 AM..
# 5  
Old 03-01-2010
Thank u all! Sounds good!
I'll try it!

Greez Roger

---------- Post updated at 02:40 PM ---------- Previous update was at 02:17 PM ----------

Quote:
Originally Posted by vivekraj
A better way to do that is at the start of the script do the following.

- Check whether your script runs by using ps command.

For example,if your script name is script.sh,then check the following.

Code:
ps ajx | egrep "[0-9]+:[0-9]+[ ]*sh script.sh"|rev|cut -d ' ' -f 1 | rev

Store the above output in the variable and checks whether it is equal to script.sh.If it matched,don't run the process.Else,run the process.
Are you sure that youre code is right? theres an error: rev: command not found

Do you know the mistake?

Thx Greez Roger
# 6  
Old 03-01-2010
Hi DarkSwiss:

rev isn't required by posix, so it may not be on all systems.

By the way, just so you know, if it is very important that one and only one instance of the script be running at any instant, none of the above solutions will guarantee that. If the script is seldom run, you may never have a problem with them, but it is still possible for more than one instance to run.

Quote:
Originally Posted by bsnithin
Here is one way to do that without temporary files.
Code:
ps -eaf | grep test.sh | grep -v grep

# Between the last grep above and the exit status check that follows, there is
# no guarantee that the operating system won't schedule another process which
# will start up this script.  At that point, this instance mistakenly thinks that it
# is the only one.

if [ $? = 0 ]
then
echo "Script already running"
fi

vivekraj's solution is similarly affected.

Regards,
Alister
# 7  
Old 03-01-2010
Hi alister

thank you for the information.
is there no way to guarantee that nobody starts the script while it is running?
its a cloning script for databases, so it must not be startet twice.
the script has to be locked or something like that.

Greez & Thx Roger
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