Tutorial :How to check in a bash script if something is running and exit if it is



Question:

I have a script that runs every 15 minutes but sometimes if the box is busy it hangs and the next process will start before the first one is finished creating a snowball effect. How can I add a couple lines to the bash script to check to see if something is running first before starting?


Solution:1

In lieu of pidfiles, as long as your script has a uniquely identifiable name you can do something like this:

#!/bin/bash  COMMAND=$0  # exit if I am already running  RUNNING=`ps --no-headers -C${COMMAND} | wc -l`  if [ ${RUNNING} -gt 1 ]; then    echo "Previous ${COMMAND} is still running."    exit 1  fi  ... rest of script ...  


Solution:2

You can use pidof -x if you know the process name, or kill -0 if you know the PID.

Example:

if pidof -x vim > /dev/null  then      echo "Vim already running"      exit 1  fi  


Solution:3

Why don't set a lock file ?

Something like

yourapp.lock

Just remove it when you process is finished, and check for it before to launch it.

It could be done using

if [ -f yourapp.lock ]; then  echo "The process is already launched, please wait..."  fi  


Solution:4

pgrep -f yourscript >/dev/null && exit  


Solution:5

This is how I do it in one of my cron jobs

lockfile=~/myproc.lock  minutes=60  if [ -f "$lockfile" ]  then    filestr=`find $lockfile -mmin +$minutes -print`    if [ "$filestr" = "" ]; then      echo "Lockfile is not older than $minutes minutes! Another $0 running. Exiting ..."      exit 1    else      echo "Lockfile is older than $minutes minutes, ignoring it!"      rm $lockfile    fi  fi    echo "Creating lockfile $lockfile"  touch $lockfile  

and delete the lock file at the end of the script

echo "Removing lock $lockfile ..."  rm $lockfile  


Solution:6

For a method that does not suffer from parsing bugs and race conditions, check out:


Solution:7

I had recently the same question and found from above that kill -0 is best for my case:

echo "Starting process..."  run-process > $OUTPUT &  pid=$!  echo "Process started pid=$pid"  while true; do      kill -0 $pid 2> /dev/null || { echo "Process exit detected"; break; }      sleep 1  done  echo "Done."  


Solution:8

To expand on what @bgy says, the safe atomic way to create a lock file if it doesn't exist yet, and fail if it doesn't, is to create a temp file, then hard link it to the standard lock file. This protects against another process creating the file in between you testing for it and you creating it.

Here is the lock file code from my hourly backup script:

echo $$ > /tmp/lock.$$  if ! ln /tmp/lock.$$ /tmp/lock ; then           echo "previous backup in process"          rm /tmp/lock.$$          exit  fi  

Don't forget to delete both the lock file and the temp file when you're done, even if you exit early through an error.


Solution:9

Use this script:

FILE="/tmp/my_file"  if [ -f "$FILE" ]; then     echo "Still running"     exit  fi  trap EXIT "rm -f $FILE"  touch $FILE    ...script here...  

This script will create a file and remove it on exit.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »