Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6555435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:49:12+00:00 2026-05-25T12:49:12+00:00

How can a bash script ensure that not more than one copy of it

  • 0

How can a bash script ensure that not more than one copy of it is running?

I have tried:

ps -ef| grep /user/loca/shell/sh | wc -l

This shows me 2 because of the grep command. Change it to:

ps -ef | grep /user/loca/shell/sh | grep -v 'grep' wc -l 

then its shows 1. However, if I then vim /user/loca/shell/sh and then execute:

ps -ef| grep /user/loca/shell/sh | grep -v 'grep' wc -l

that shows 2. But there is only one process, I have started.

How can the bash script check whether the process is running?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:49:12+00:00Added an answer on May 25, 2026 at 12:49 pm

    The idiom often used in Unix Daemons is to create a file with the PID in it when they start.

    Then you can check the existence and/or content of the file when you start, if it’s there, you exit and leave that instance running.

    At the end of the script you delete the file, ready to run next time.

    The only problem comes when the script runs, but does not complete for some reason, leaving the PID file in existence. This can be taken care of by checking its timestamp, if it’s too long ago, you assume that it was an aborted run and continue with the current one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can i, in a bash script, execute a command when the user stops
how can one turn off the error messages of a bash script? I want
I want to write a sh/bash script that can determine whether a particular directory
Can you use the bash getopts function twice in the same script? I have
In bash one can escape arguments that contain whitespace. foo a string This also
It is not totally rigth that I can run a Bash function with 000
I have a strange problem with BASH script which I can't figure out. I
I want to write a bash script that will execute one command in the
How can I get the directory a bash script file is in, when that
I have the following Bash script. How can I make it pause on keypress

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.