Netgate Discussion Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Search
    • Register
    • Login
    Introducing Netgate Nexus: Multi-Instance Management at Your Fingertips.

    How to secure only one running instance of Linux script?

    Scheduled Pinned Locked Moved General pfSense Questions
    3 Posts 2 Posters 456 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B
      Bamsefar
      last edited by

      Hi,

      So I am trying to get one of my scripts in pfSense to only start once so to speak, just as a precaution. Normal mode would be it starts itself automatic, and well that's just enough. However I like to double check that it is not already running. So I though I code it like:

      running=$(ps aux | grep CheckIfHome.sh | grep -v grep | grep -o CheckIfHome.sh | wc -l);
      
      if [ $running > 1 ];
      then
        exit
      fi
      

      But this will fail with "invalid variable" and for some reason my head seems to be on vacation…? Anyone that might help out?

      1 Reply Last reply
      Reply Quote 0
      • GertjanG
        Gertjan
        last edited by

        Hi,

        No need for anyone. Just one will and you'll be fine.
        What about this one : shell script run one instance  ;)

        No "help me" PM's please. Use the forum, the community will thank you.

        1 Reply Last reply
        Reply Quote 0
        • B
          Bamsefar
          last edited by

          I ended up with something like this:

          
          if [ `pgrep script.sh | wc -l` -gt 1 ];
          then
            exit
          fi
          
          1 Reply Last reply
          Reply Quote 0

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • First post
            Last post
          Copyright 2026 Rubicon Communications LLC (Netgate). All rights reserved.
          Privacy Policy · Cookie Policy