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.
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.
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.
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.