Netgate Discussion Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Search
    • Register
    • Login

    How to secure only one running instance of Linux script?

    Scheduled Pinned Locked Moved General pfSense Questions
    3 Posts 2 Posters 268 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.
        Edit : and where are the logs ??

        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
          • First post
            Last post
          Copyright 2025 Rubicon Communications LLC (Netgate). All rights reserved.