Need to execute a script by one user at a time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to execute a script by one user at a time
# 1  
Old 10-08-2012
Need to execute a script by one user at a time

Hi ,
I have a script which everyone have access but I need that the script should be ran by one user at a time. The second user who is trying to execute the script should get a message stating that the user is already executing the script. Is there any way to achieve this.?

Thanks in advance.
rogerben
# 2  
Old 10-08-2012
Use one file to maintain the script status.

It is just hint.
Cons - If you use ctrl c in middle of script then next time you can't run the script even if script is not running.
Solution - Get PID at the start of the script and then use that to check if the script is still running or not.( this i left for your exercise)

try something this..

Code:
if [[ ! -f test_script_check ]]
then
echo "test2.sh" > test_script_check
echo "script is running"
sleep 100
rm test_script_check
else
echo "script is already running"
fi


Last edited by pamu; 10-08-2012 at 01:17 PM..
This User Gave Thanks to pamu For This Post:
# 3  
Old 10-08-2012
You can overcome the "con" above by setting a trap so the trap will remove the check file upon exit (no matter how you exit):

Code:
trap 'rm test_script_check' 0 2 3 9 15

More info on signals that can be caught:
Unix - Signals and Traps

Also you could write the current user into the check file, and if someone else tries to run it, the error message could print who is currently running it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Script without execute permissions will work for a user?

Please help me to understand the issue: Issue: There are shell scripts in a user home directory (/home/user_1) without execute permissions (rw-r--r--) to owner,group and world These shell scripts were able to execute/work previously but its not working now and it says permission denied or... (2 Replies)
Discussion started by: MSK_1990
2 Replies

2. UNIX for Advanced & Expert Users

Allow user without dir write permission to execute a script that creates files

In our project we have several unix scripts that trigger different processes. These scripts write logs to a particular folder 'sesslogs', create output data files in a separate directory called 'datafiles' etc. Usually L1 support team re-run these scripts . We donot want L1 support team to have... (14 Replies)
Discussion started by: waavman
14 Replies

3. Shell Programming and Scripting

Script for login to servers with user name and password and execute commands

I am having the 15 servers which need to do the monitoring Hi I need a shell script, By which i can log in to multiple servers and execute the commands.. I need to specify the username and password in the scripts. Please help me to write the script so that it can login with username and... (5 Replies)
Discussion started by: nandan8a
5 Replies

4. Shell Programming and Scripting

root user command in shell script execute as normal user

Hi All I have written one shell script for GPRS route add is given below named GPRSRouteSet.sh URL="www.google.com" VBURL="10.5.2.211" echo "Setting route for $URL for GPRS" URL_Address=`nslookup $URL|grep Address:|grep -v "#"|awk -F " " '{print $2}'|head -1` echo "Executing ... (3 Replies)
Discussion started by: mnmonu
3 Replies

5. Shell Programming and Scripting

Change user on remote machine and execute script!

Hi, I need to login into remote server and execute a shell script over there. As of now i am making use of ssh command ssh primUser@135.254.242.2 sh /poll.sh I am logging in as primUser but unless i change the user to root the script execution on the remote machine is not possible. ... (5 Replies)
Discussion started by: goutham4u
5 Replies

6. Shell Programming and Scripting

login into root from user and execute command through script

i have logged in as user. I want to write a script to login into root and execute commands for eg. ifconfig or other command. kindly help me out. (6 Replies)
Discussion started by: pradeepreddy
6 Replies

7. Shell Programming and Scripting

shell script to execute user command

I don't know why the following shell script doesn't work. Could you please help me out? #!/usr/bin/ksh test="cal > /tmp/tmp.txt 2>&1" $test I know it will work for the following format: #!/usr/bin/ksh cal > /tmp/tmp.txt 2>&1 However, I need to get the command from the user in... (1 Reply)
Discussion started by: redtiger
1 Replies

8. Shell Programming and Scripting

Execute internal script as different user

I have a script that I must run as user X and need to send the results to a different server as user Y (sftp). User Y has been set up to not require password authentication between the 2 servers. I would prefer to keep these in a single script, as our operations might have to run it from time to... (6 Replies)
Discussion started by: aubtc
6 Replies

9. UNIX for Advanced & Expert Users

i want a script to execute if the time is 12:00 automatically

i write a script for displaying a xmessage.But i want it to run automatically for every one hour.can any one help me. (2 Replies)
Discussion started by: lakshmananindia
2 Replies

10. Shell Programming and Scripting

Execute a part of shell script only after particular date and time

I have created a simple shell script... say test.sh Contents of test.sh ================ service named restart cp /etc/imp.conf /backup/test/ #-- if date > 15 July 2007 11:23 pm , then only issue the commans below, else exit --- cp /etc/secondimp.conf /backup/test/ rm -f... (2 Replies)
Discussion started by: fed.linuxgossip
2 Replies
Login or Register to Ask a Question