Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if script is running or not from script itself?

Tags:

linux

bash

Having below sample script sample.sh

#!/bin/bash

if ps aux | grep -o "sample.sh" >/dev/null
then
    echo "Already script running"
    exit 0
fi

echo "start script"

while true
do
    echo "script running"
    sleep 5
done

In above script i want to check if this script previously running or not if running then not run it again.

problem is check condition always become true (because to check the condition require to run script) and it always show me "Already script running" message.

Any idea how to solve it?

like image 470
Jayesh Bhoi Avatar asked Apr 09 '14 09:04

Jayesh Bhoi


People also ask

How do you check if a script is running or not?

Create PID file sh. pid # Add check for existence of mypidfile if [ -f $mypidfile ]; then echo "script is already running" exit fi # 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.

How do you check if a script is running or not in Unix?

Bash commands to check running process: pgrep command – Looks through the currently running bash processes on Linux and lists the process IDs (PID) on screen. pidof command – Find the process ID of a running program on Linux or Unix-like system.

How do you check if a shell script is running in the background?

You can use the ps command to list all background process in Linux. Other Linux commands to obtain what processes are running in the background on Linux. top command – Display your Linux server's resource usage and see the processes that are eating up most system resources such as memory, CPU, disk and more.

What shell script is running?

How to check which shell am I using: Use the following Linux or Unix commands: ps -p $$ – Display your current shell name reliably. echo "$SHELL" – Print the shell for the current user but not necessarily the shell that is running at the movement.

Why can't I see if a script is running?

I see your script is checking if the process is running, using logger to log the status and terminating. This is the reason why you are not seeing it running. You should put this in an infinite while loop with a sleep time of your choice for periodically checking and logging the status.

Why is it important to check if a shell script is running?

When you run shell script it is important to ensure that only a single instance of your script is running. If you run the command to start your shell script when another instance of it is already running, it can lead to unpredictable result. So before you run your shell script it is important to check if it is already running.

How do I know if a process is running in Linux?

Every running process has a PID. We use pidof command to determine the PID of our shell script. If the PID exists, it means the shell script is already running. In such cases, the above script will display a message saying that the process is already running.

How can I tell which VBScript or JScript file is running?

If a VBScript or JScript is running, the process wscript.exe or cscript.exe would appear in the list. Right-click on the column header and enable "Command Line". This should tell you which script file is being executed.


1 Answers

You need a proper lock. I'd do using flock like this:

exec 201> /tmp/lock.$(basename $0).file
if ! flock -n 201  ; then
  echo "another instance of $0 is running";
  exit 1
fi

# cmds 

exec 201>&- 
rm -rf  /tmp/lock.$(basename $0).file

This basically creates lock for script using a temporary file. The temporary file has particular significance other than it's used to tell whether your script has acquired a lock. When there's an instance of this program running, the next run of the same program can't run as the lock will prevent it.

like image 139
P.P Avatar answered Nov 15 '22 06:11

P.P