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.

    CLAMD_Running=$(pgrep -c clamd)
    if  "$CLAMD_Running" -eq 0 ];then
        printf "loading clamd
"
        clamd &
        
        #pause for clamd to load
        while (  "$(pgrep -c clamd)" -eq 0 ] )
        do
             CLAMD_Running=''; printf "
"
        done
        
    else
        printf "clamd running
"
    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.

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

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.

Show how it can be done.

Hi,

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

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

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

if ! pgrep -x foobar >/dev/null; then
  printf 'foobar is not running >&2
fi

Note that pgrep has more options.

pgrep --help

Or if you can start the process yourself

clamd & pid=$!
while kill -0 "$pid"; do
  printf 'clamd is running%s
'
  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.

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.

    if ! pgrep -x clamd >/dev/null; then
        printf "loading clamd
"
        
        clamd & pid=$!    #
        while kill -0 "$pid"; do
           printf 'clamd is running%s
'
           sleep 5
        done    #
        
    else
        printf "clamd running
"
    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.

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

PAGER='less +/^:space:]]\+Special\ Parameters' man bash

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

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