I am curious if this script needs to be sh?
If it were able to be bash (and I am not 100% sure which parts might not work in sh (as been a while)), here are a few suggestions or ideas / notes:
1. There is a space after LOG_FILE= which will cause some issue (again may just be a typo)
2. You have created variables for what seems to be all external commands, yet not for wc
3. You only appear to use cat / CAT once in the entire script, which would seem to be pointless creating this variable for one line. You could try:
4. You use echo and printf alternately throughout the script, but the printf is never really utilised (ie not using %s, %d, ...) so it would be cleaner to simply use echo
5. As an alternative to point 4 you might also wish to look at
HEREDOC'S
6. Why not use the same if test, as we did for kill, for the rest of your tests of command output, ie pgrep (although you may need to look at the logic)
7. As an alternative to 6, you can use
(()) for arithmetic tests
8. As yet another alternative for the pgrep lines, I would ditch the wc pipe altogether and simply test the output of pgrep, something like:
Code:
if $PGREP $PHP_FPM &> /dev/null
9. It is generally accepted (although can be a personal choice) to use $() over ``,
here is some information on it
10. Is the for loop even required, maybe also have a look at pkill
11. You run, what appears, to be the exact same code twice, first for PHP_FPM and then NGINX. I would suggest looking into creating a function or maybe loop to take the arguments and process
the data in a single portion of the script.
Well I hope some of that helps. As usual you can completely ignore it if you like