Skip to main content
Fix and-or list with proper if construct. errors should go on stderr, echo can't be used for arbitrary data, missing quotes and --
Source Link
Stéphane Chazelas
  • 584.7k
  • 96
  • 1.1k
  • 1.7k

One other way to make sure a single instance of bash script runs:

#! /bin/bash -

# Check if another instance of script is running
if pidof -o %PPID -x $0-- "$0" >/dev/nullnull; &&then
 echo printf >&2 '%s\n' "ERROR: Script $0 already running" 
 && exit 1
fi

...

pidof -o %PPID -x $0-- "$0" gets the PID of the existing scriptscript¹ if itsit's already running or exits with error code 1 if no other script is running


¹ Well, any process with the same name...

One other way to make sure a single instance of bash script runs:

#!/bin/bash

# Check if another instance of script is running
pidof -o %PPID -x $0 >/dev/null && echo "ERROR: Script $0 already running" && exit 1

...

pidof -o %PPID -x $0 gets the PID of the existing script if its already running or exits with error code 1 if no other script is running

One other way to make sure a single instance of bash script runs:

#! /bin/bash -

# Check if another instance of script is running
if pidof -o %PPID -x -- "$0" >/dev/null; then
  printf >&2 '%s\n' "ERROR: Script $0 already running" 
  exit 1
fi

...

pidof -o %PPID -x -- "$0" gets the PID of the existing script¹ if it's already running or exits with error code 1 if no other script is running


¹ Well, any process with the same name...

added 74 characters in body
Source Link
Sethu
  • 281
  • 2
  • 3

One other way to make sure a single instance of bash script runs:

#!/bin/bash

# Check if another instance of script is running
pidof -o %PPID -x $0 >/dev/null && echo "ERROR: Script $0 already running" && exit 1

...

pidof -o %PPID -x $0 gets the PID of the existing script if its already running or exits with error code 1 if no other script is running

One other way to make sure a single instance of bash script runs:

#!/bin/bash

# Check if another instance of script is running
pidof -x $0 >/dev/null && echo "ERROR: Script $0 already running" && exit 1

...

pidof -x $0 gets the PID of the existing script if its already running

One other way to make sure a single instance of bash script runs:

#!/bin/bash

# Check if another instance of script is running
pidof -o %PPID -x $0 >/dev/null && echo "ERROR: Script $0 already running" && exit 1

...

pidof -o %PPID -x $0 gets the PID of the existing script if its already running or exits with error code 1 if no other script is running

Source Link
Sethu
  • 281
  • 2
  • 3

One other way to make sure a single instance of bash script runs:

#!/bin/bash

# Check if another instance of script is running
pidof -x $0 >/dev/null && echo "ERROR: Script $0 already running" && exit 1

...

pidof -x $0 gets the PID of the existing script if its already running