Results 1 to 10 of 10

Thread: clamd checking for running process

  1. #1

    Default clamd checking for running process

    I need to check to see if clamd is running. When clamd is not running to start it. Wait for it to start up and execute command.

    Code:
        CLAMD_Running=$(pgrep -c clamd)
        if [ "$CLAMD_Running" -eq 0 ];then
            printf "loading clamd\n"
            clamd &
            
            #pause for clamd to load
            while ( [ "$(pgrep -c clamd)" -eq 0 ] )
            do
                 CLAMD_Running=''; printf "\n"
            done
            
        else
            printf "clamd running\n"
        fi
    
        freshclam
    WARNING: Clamd was NOT notified: Can't connect to clamd through /var/run/clamav/clamd-socket: No such file or directory

    If run it again, It will say clamd running. freshclam will work. What is wrong with it? Thanks.

  2. #2
    Join Date
    Jun 2008
    Location
    San Diego, Ca, USA
    Posts
    13,295
    Blog Entries
    2

    Default Re: clamd checking for running process

    I've seen this done by assigning a specific PID to your app when it runs, so that at any time you can check for the existence of that PID when your app is running.

    BTW - If you do this, then it also supports another feature you may be interested in... modifying nice to adjust the priority of your app so that it can run as a low priority in the background or as a high priority foreground process if you're in a hurry.

    TSU
    Beginner Wiki Quickstart - https://en.opensuse.org/User:Tsu2/Quickstart_Wiki
    Solved a problem recently? Create a wiki page for future personal reference!
    Learn something new?
    Attended a computing event?
    Post and Share!

  3. #3
    Join Date
    Sep 2012
    Posts
    7,481

    Default Re: clamd checking for running process

    Quote Originally Posted by lord_valarian View Post
    If run it again, It will say clamd running. freshclam will work. What is wrong with it?
    clamd may need arbitrary long time to finish initialization; the mere existence of process (that you check) does not mean it is ready to serve requests. You need to ask on clamd support forum how to determine it is ready (the best would be if it supported socket activation - that eliminates any races) - or simply add some delay after starting it and hope it will usually be enough.

  4. #4
    Join Date
    Sep 2012
    Posts
    7,481

    Default Re: clamd checking for running process

    Quote Originally Posted by tsu2 View Post
    I've seen this done by assigning a specific PID to your app when it runs,
    Show how it can be done.

  5. #5

    Default Re: clamd checking for running process

    Hi,

    You don't need to use a variable just check if a process is running.

    Code:
    if pgrep -x bash >/dev/null; then
      printf 'bash is running%s\n'
    fi

    Or if you just want to check if a process is not running and act upon it.

    Code:
    if ! pgrep -x foobar >/dev/null; then
      printf 'foobar is not running >&2
    fi
    Note that pgrep has more options.

    Code:
    pgrep --help

    Or if you can start the process yourself

    Code:
    clamd & pid=$!
    Code:
    while kill -0 "$pid"; do
      printf 'clamd is running%s\n'
      sleep 5
    done
    Now, someone already mention that clamd might need more time to be able to serve whatever request process is need after it has started so it yeah you must read the docs for clamd or ask question in the proper forum.
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

  6. #6

    Default Re: clamd checking for running process

    Quote Originally Posted by tsu2 View Post
    I've seen this done by assigning a specific PID to your app when it runs, so that at any time you can check for the existence of that PID when your app is running.

    BTW - If you do this, then it also supports another feature you may be interested in... modifying nice to adjust the priority of your app so that it can run as a low priority in the background or as a high priority foreground process if you're in a hurry.

    TSU
    Hmmm, that's a good idea. I'd like the ability have it not drag (drain speed) in firefox. FYI, I added a 'p' shutdown command for overnight scans. My took 6 hours+. This will speed that up. A question for another topic.

  7. #7

    Default Re: clamd checking for running process

    Code:
        if ! pgrep -x clamd >/dev/null; then
            printf "loading clamd\n"
            
            clamd & pid=$!    #
            while kill -0 "$pid"; do
               printf 'clamd is running%s\n'
               sleep 5
            done    #
            
        else
            printf "clamd running\n"
        fi
    I have to wait for another update to see if will work. Sending data to clamd if new updates. The code looped three times. This looks positive. I'll post again after I test it.

    For now, i'm not quite clear what this is doing(##). Can you explain? Saving the Process ID.

  8. #8

    Default Re: clamd checking for running process

    Quote Originally Posted by lord_valarian View Post
    Code:
        if ! pgrep -x clamd >/dev/null; then
            printf "loading clamd\n"
            
            clamd & pid=$!    #
            while kill -0 "$pid"; do
               printf 'clamd is running%s\n'
               sleep 5
            done    #
            
        else
            printf "clamd running\n"
        fi
    I have to wait for another update to see if will work. Sending data to clamd if new updates. The code looped three times. This looks positive. I'll post again after I test it.

    For now, i'm not quite clear what this is doing(##). Can you explain? Saving the Process ID.
    Hi,

    Yes the pgrep part is a bit easy to understand. The while loop is only looping to check if the process is still running by checking the pid of the running process which is save in the variable "$pid". kill -0 only checks if the process id is still present/alive.
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

  9. #9

    Default Re: clamd checking for running process

    Quote Originally Posted by jetchisel View Post
    Hi,

    Yes the pgrep part is a bit easy to understand. The while loop is only looping to check if the process is still running by checking the pid of the running process which is save in the variable "$pid". kill -0 only checks if the process id is still present/alive.
    Hi,

    I forgot to mention the $! special bash parameter holds the PID of the most recently executed background job.

    You can check it out in the bash man page by running

    Code:
    PAGER='less +/^[[:space:]]\+Special\ Parameters' man bash
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

  10. #10

    Default Re: clamd checking for running process

    Thanks for the assistance to everyone who helped. The clamd now is delayed with enough time to so that freshclam received the data.

    Now to figure out what information clamd has and how to view it.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •