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
platform::shell(n)					       Tcl Bundled Packages						platform::shell(n)

__________________________________________________________________________________________________________________________________________________

NAME
platform::shell - System identification support code and utilities SYNOPSIS
package require platform::shell ?1.1.4? platform::shell::generic shell platform::shell::identify shell platform::shell::platform shell _________________________________________________________________ DESCRIPTION
The platform::shell package provides several utility commands useful for the identification of the architecture of a specific Tcl shell. This package allows the identification of the architecture of a specific Tcl shell different from the shell running the package. The only requirement is that the other shell (identified by its path), is actually executable on the current machine. While for most platform this means that the architecture of the interrogated shell is identical to the architecture of the running shell this is not generally true. A counter example are all platforms which have 32 and 64 bit variants and where a 64bit system is able to run 32bit code. For these running and interrogated shell may have different 32/64 bit settings and thus different identifiers. For applications like a code repository it is important to identify the architecture of the shell which will actually run the installed packages, versus the architecture of the shell running the repository software. COMMANDS
platform::shell::identify shell This command does the same identification as platform::identify, for the specified Tcl shell, in contrast to the running shell. platform::shell::generic shell This command does the same identification as platform::generic, for the specified Tcl shell, in contrast to the running shell. platform::shell::platform shell This command returns the contents of tcl_platform(platform) for the specified Tcl shell. KEYWORDS
operating system, cpu architecture, platform, architecture platform::shell 1.1.4 platform::shell(n)
All times are GMT -4. The time now is 08:18 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy