Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How can I investigates whether already running a program called /root/dodo/dodo? But I need to run a scan directly from the program, which tests. Thank you...

file /root/todo/todo

#!/bin/bash
if [ check run program /root/todo/todo ]
then
  exit
else
  #continued
fi
share|improve this question

migrated from stackoverflow.com Mar 11 '13 at 10:54

This question came from our site for professional and enthusiast programmers.

    
Can you give some more information? Do you need to check programmatically whether process /root/dodo/dodo is running? If yes, then what language are you using? –  Devendra D. Chavan Mar 11 '13 at 1:50
    
Yes, I'm sorry! Language is bash/shell. Thank –  petr Mar 11 '13 at 2:09

5 Answers 5

you can check the process and see if there is a name like you indicated:

$ ps aux | grep "/root/dodo/dodo"

if you need to do test inside a bash script,

ps aux | grep -v "grep" | grep -q "/root/dodo/dodo"
if [ $? -eq 0 ]; then
   # do stuff if it is running
else
   # do stuff if it is not running
fi
share|improve this answer
    
but the control is started from the / root / todo / todo. ps aux ... I always give the program runs –  petr Mar 11 '13 at 2:23
    
@petr I cannot understand your comments, but anyhow, above is an example code to do test –  zzk Mar 11 '13 at 2:33
    
Thank, the result is still do stuff if it is running :-( –  petr Mar 11 '13 at 2:39

If you want to know whether a program called /root/dodo/dodo is already running or not at the moment, use the following:

ps eaf | grep /root/dodo/dodo
share|improve this answer

It seems that you want to write a shell script which ensures that there is only one single instance (process) running it.

Then see this question and consider using lockfile(1)

share|improve this answer

The appropriate way to check whether one or more running processes match a string is pgrep. Never use ps | grep.

if pgrep bash >/dev/null; then 
   echo "found a bash process"
else 
   echo "didn't find a bash process"
fi
share|improve this answer

Actually you may need to scan all running bash processes and check what script they run (at least in my bash this is always filedescriptor 255)..

the following may be a bit fragile in point of portability and longtime availability but it may fix your problem for now..

#!/bin/bash

declare -i running

cmd=$(readlink -f "${0}")

while true ; do
    running=0
    pids=( $(pidof bash) )

    for p in "${pids[@]}" ; do
        script=$(readlink -f /proc/${p}/fd/255 2>/dev/null)
        if [ "${script}" = "${cmd}" ] ; then
            running+=1
        fi
    done

    echo "running ${running} times.. this is $$.."
    sleep 1
done

Exit logic is not implemented.. it just counts number of running processes in a loop.. 8) should be easy to adopt..

This introduces some race conditions (as every solution may do) since if you get 2 running instances they both may be checking and deciding to exit because they are already running.. 8)

the readlinks are required to resolve any symlinks.. this may be needed if the script is started by another name (symlink with another name to itself)..

Actually I would solve this issue by file locking and/or by creating a pid file..

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.