How to check if another instance of my shell script is running

bash check if script is already running
shell script check if process is running
bash check if process is running by pid
how to check script running in background
python script to check if process is running linux
bash check if child process is running
script to check if application is running
php check if process is running

GNU bash, version 1.14.7(1)

I have a script is called "" I have to check this from script only... inside it I have written following statement

status=`ps -efww | grep -w "" | grep -v grep | grep -v $$ | awk '{ print $2 }'`
if [ ! -z "$status" ]; then
        echo "[`date`] : : Process is already running"
        exit 1;

I know it's wrong because every time it exits as it found its own process in 'ps' how to solve it? how can I check that script is already running or not from that script only ?

An easier way to check for a process already executing is the pidof command.

if pidof -x "" >/dev/null; then
    echo "Process already running"

Alternatively, have your script create a PID file when it executes. It's then a simple exercise of checking for the presence of the PID file to determine if the process is already running.



# Could add check for existence of mypidfile here if interlock is
# needed in the shell script itself.

# Ensure PID file is removed on program exit.
trap "rm -f -- '$mypidfile'" EXIT

# Create a file with current PID to indicate that process is running.
echo $$ > "$mypidfile"


Update: The question has now changed to check from the script itself. In this case, we would expect to always see at least one running. If there is more than one, then we know that process is still running. I'd still suggest use of the pidof command which would return 2 PIDs if the process was already running. You could use grep to filter out the current PID, loop in the shell or even revert to just counting PIDs with wc to detect multiple processes.

Here's an example:


for pid in $(pidof -x; do
    if [ $pid != $$ ]; then
        echo "[$(date)] : : Process is already running with PID $pid"
        exit 1

How to check if process is already running in bash (shell script) Linux, if ps -Ao comm | grep -q "^$0\$" then echo "$0 is already running" 2>&1 exit 1 fi In the interval between those two operations, another instance of the script may  Hi Vino, I tried using a temporary file with pids and creating a lock directory to check for another process instance. But in my situation, there is one more complication. The script which is invoking the C++ program exits even before the other process is complete

I you want the "pidof" method, here is the trick:

    if pidof -o %PPID -x "">/dev/null; then
        echo "Process already running"

Where the -o %PPID parameter tells to omit the pid of the calling shell or shell script. More info in the pidof man page.

Check for Process if Same is Running, I am using a CentOS 7 Linux and Ubuntu 18.04 LTS server in the cloud. How do I check if a process is running or not on Linux? How do I A Linux process is nothing but an executing (i.e., running) instance of a program. What is the linux command to find if a process say is running or not. ps command does not seem to work and it does not show the shell script names. Please advise.

Here's one trick you'll see in various places:

status=`ps -efww | grep -w "[a]" | awk -vpid=$$ '$2 != pid { print $2 }'`
if [ ! -z "$status" ]; then
    echo "[`date`] : : Process is already running"
    exit 1;

The brackets around the [a] (or pick a different letter) prevent grep from finding itself. This makes the grep -v grep bit unnecessary. I also removed the grep -v $$ and fixed the awk part to accomplish the same thing.

Bash check if process is running or not on Linux / Unix, #!/bin/bash # check if we are the only local instance if [[ "`pidof -x $(basename $0) -o %PPID`" ]]; then echo "This script is already running with  I want a shell script which check whether a certain program is running or not, if not, then start it. There are two programs for which I need this. I have made this script and setup it in cron to execute it every hour for checking/starting of my programs:

Someone please shoot me down if I'm wrong here

I understand that the mkdir operation is atomic, so you could create a lock directory

mkdir $lockdir  || {
    echo "lock directory exists. exiting"
    exit 1
# take pains to remove lock directory when script terminates
trap "rmdir $lockdir" EXIT INT KILL TERM

# rest of script here

Bash: How to check if your script is already running – Jay's IT blog, This script is just checking to see if the program "gedit" is running. Or you can only I need to make sure only one instance of my script is running. In this case, use a echo "Not starting another instance." exit 5 else nohup  It stores the pid of the script to a file. Then when another instance of the script is started, the new script tries to send signal 0 (=do nothing) to the pid stored in the file (i.e. previous instance of the script) using the kill command. If the signal gets send it means that the script is running and the new script will exit _cleanly_.

Here's how I do it in a bash script:

if ps ax | grep $0 | grep -v $$ | grep bash | grep -v grep
    echo "The script is already running."
    exit 1

This allows me to use this snippet for any bash script. I needed to grep bash because when using with cron, it creates another process that executes it using /bin/sh.

bash, Execute your script from the command line. Open another terminal session and try running the script again and it should say it's already running. I don't believe the  so i have a script that takes a while to complete and its cpu intensive. this script is being used by several users. i want to make sure only 1 user can run this script at any given time. i originally | The UNIX and Linux Forums

Bash script to check if a script is already running. · GitHub, Hi, I am writing a shell script to invoke a C++ program. Before I start the C++ program (oi7loadbalancer), I am checking if the process is already running. I want to know how from Unix Shell Script I can check if another instance of a java program is running. Update : 'ps -aef | grep MyJavaProg' is not working as my command is pretty long

How to check if another instance of the process is running, I am concerned of the fact that another instance of the same script run by cron will "overwhelm" the older instance (due to my code) if it is still  Within this script, I need to write some logic that checks if any other instances of this same script is running on the host. If there are no other instances, then the script can continue to execute. However, if another instance is running, then the current session should send an email and terminate immediately.

Check if a script is already running?, This is an example of a bash script which checks for some running process then use grep(filter) command to check if our process name is in that list, for example: Here is a script wich checks if SERVICE (variable at the beginning of script