Sponsored Content
Operating Systems Solaris How to check if a shell is already running ? Post 302597832 by agama on Sunday 12th of February 2012 11:35:47 AM
Old 02-12-2012
I assume you have a long running script and are wanting to prevent a second copy from starting if the first is running. What is probably happening is that your test is seeing 'itself' in the output from ps and is claiming that another copy is already running.


When I have to ensure that the invocation of a script is the only one running I use a marker directory in /tmp. Only one process can create a directory, so if two are started at the same time then only one will 'win' and continue running. Further, I add some information to the directory so that when another detects one already running, it can verify that it is still running and allows itself to start if it finds a marker, but doesn't find an associated process.


Here's an example:
Code:
#!/usr/bin/env ksh

trap "rm -fr \${solo_mark:-nosuchfile}" EXIT            # remove our marker stuff at exit

cmd=${0##*/}
solo_mark=/tmp/$cmd.solo
if ! mkdir $solo_mark 2>/dev/null       # if two started at the same time only one will be successful
then
    sleep 1                             # if there was a collision, allow winner to set the info file
    read rinfo <$solo_mark/info
    echo "verification that '$rinfo' is still running"
    if ps -e -o pid,command |awk -v rinfo="$rinfo" '
        BEGIN { split( rinfo, a, " " ); }
        $1 == a[1] {
            if( rinfo == $0 )
                exit( 1 );
            next;
          }'
    then
        echo "marker exists, but not running; we continue"
    else
        echo "already running"
        exit 1;
    fi
else
    echo "not running"
fi
ps -o pid,command | grep "^[ ]*$$" >$solo_mark/info     # save our info for others to verify
 


# ------- do intended work here -----------------

The bit of extra work to save the info, and verify it, makes things easier following a system crash, or someone killing the process in such a way that the marker doesn't get removed.


Hope this helps.

Last edited by agama; 02-12-2012 at 12:39 PM.. Reason: formatting
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

check my code?? running shell from c?

Hi mates, i am trying to run a shell command from my C program in this case let is say the "ls" command. It copiles it okay, and even creates the new fork too. But seems to nothing is happening i mean it is not showing the result of the "ls" command. I don't know wat i am doing wrong. Any... (1 Reply)
Discussion started by: abdul
1 Replies

2. Shell Programming and Scripting

check that script is not running twice

using ps -ef | fgrep "ld_data" how do i write a script to check that it didn't already run Thanks (2 Replies)
Discussion started by: Link_02
2 Replies

3. Shell Programming and Scripting

How to check for specific process running through shell

Hi I want to check whether specific process is running or not through a shell script. ps -ef | grep will tell me this, i tried this way in shell cnt =`ps -ef| grep xyz | awk '{ END {print NR}}` if it return two rows, job is running else not. Is there any better way of doing this. (3 Replies)
Discussion started by: agp
3 Replies

4. UNIX for Advanced & Expert Users

how to check if a process is running in a server from shell script.

I want to write a unix shell script that will check if a process (say debu) is running in the server or not. If no , then send a mail to the corresponding person to start the process??? (2 Replies)
Discussion started by: debu
2 Replies

5. Programming

How do I check if a process is running in C

How to I check if a process is running in C? I'm trying to use ps aux |grep "process name" but failing in doing that. How do I do that? Thanks, (1 Reply)
Discussion started by: cprogdude
1 Replies

6. Shell Programming and Scripting

Check what cron is running

How to check what cron is scheduled to run? I don't want to modify anything. Thanks (1 Reply)
Discussion started by: stevensw
1 Replies

7. Shell Programming and Scripting

need to check if the process is running

Hi, I check if the process is running or not using the below. /usr/ucb/ps auxww | grep 109 |grep rmi | awk '{print $2}' 9718 Thus we see 9718 is the PID. It return blank if the process is not running. I need to perform some action if the process is not running and leave it if... (8 Replies)
Discussion started by: shifahim
8 Replies

8. Shell Programming and Scripting

Bash shell script to check if script itself is running

hi guys we've had nagios spewing false alarm (for the umpteenth time) and finally the customer had enough so they're starting to question nagios. we had the check interval increased from 5 minutes to 2 minutes, but that's just temporary solution. I'm thinking of implementing a script on the... (8 Replies)
Discussion started by: hedkandi
8 Replies

9. Shell Programming and Scripting

Help with Check Syntax of Shell Script without Running

Hi Everyone, Is there any way ( generic) to check syntax of Shell Scripts without running it?? (4 Replies)
Discussion started by: roy121
4 Replies

10. Shell Programming and Scripting

Check status of long running multiple curl commands in shell script

I am working on script. it reads a file which contains multiple lines Ex; curl --write-out %{http_code} --silent --output /dev/null http://hostname:port/input=1 curl --write-out %{http_code} --silent --output /dev/null http://hostname:port/input=2 curl --write-out %{http_code} --silent ... (2 Replies)
Discussion started by: oraclermanpt
2 Replies
shell(1F)							   FMLI Commands							 shell(1F)

NAME
shell - run a command using shell SYNOPSIS
shell command [command] ... DESCRIPTION
The shell function concatenate its arguments, separating each by a space, and passes this string to the shell ($SHELL if set, otherwise /usr/bin/sh). EXAMPLES
Example 1: A sample output of shell command. Since the Form and Menu Language does not directly support background processing, the shell function can be used instead. `shell "build prog > /dev/null &"` If you want the user to continue to be able to interact with the application while the background job is running, the output of an exe- cutable run by shell in the background must be redirected: to a file if you want to save the output, or to /dev/null if you don't want to save it (or if there is no output), otherwise your application may appear to be hung until the background job finishes processing. shell can also be used to execute a command that has the same name as an FMLI built-in function. NOTES
The arguments to shell will be concatenate using spaces, which may or may not do what is expected. The variables set in local environments will not be expanded by the shell because "local" means "local to the current process." ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ SEE ALSO
sh(1), attributes(5) SunOS 5.10 5 Jul 1990 shell(1F)
All times are GMT -4. The time now is 07:46 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy