Skip to main content
Code Review

Return to Answer

replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
Source Link

As you and I both know, what follows is the answer I posted to your question at Unix and Linux Unix and Linux. I'm not sure if this might be considered in bad taste or not - and if so, I'll happily delete it - but at the other site the question was apparently closed as a cross-post in favor of this. And so I thought - maybe it belonged? Anyway, here goes:

As you and I both know, what follows is the answer I posted to your question at Unix and Linux. I'm not sure if this might be considered in bad taste or not - and if so, I'll happily delete it - but at the other site the question was apparently closed as a cross-post in favor of this. And so I thought - maybe it belonged? Anyway, here goes:

As you and I both know, what follows is the answer I posted to your question at Unix and Linux. I'm not sure if this might be considered in bad taste or not - and if so, I'll happily delete it - but at the other site the question was apparently closed as a cross-post in favor of this. And so I thought - maybe it belonged? Anyway, here goes:

Source Link
mikeserv
  • 221
  • 1
  • 5

As you and I both know, what follows is the answer I posted to your question at Unix and Linux. I'm not sure if this might be considered in bad taste or not - and if so, I'll happily delete it - but at the other site the question was apparently closed as a cross-post in favor of this. And so I thought - maybe it belonged? Anyway, here goes:


What you're really doing wrong is duplicating your effort - basically every hardcoded occurrence of _agent or _server appears to be completely redundant.

For example, if this is being run on a linux system, you can completely drop the grep_...() functions, and consolidate both check_...s into a single entity which might work like:

email(){ 
 mutt -s "Zabbix Auto-restart Script Just Ran" \<user email\>
}
prlog(){ 
 date +"%x %X:%tservice 1ドル${2+%n************************}"
}
chk_run()
 while [ "$#" -gt 0 ]
 do if ps -C zabbix_"1ドル"
 then : "$((1ドル=1))"
 else set zabbix_"$@"
 service "1ドル" start || eval >&2 '
 prlog "1ドル restart failed." +; exit '"$?"
 prlog "1ドル restarted." + >&2
 prlog "1ドル restarted from 0ドル." |email
 fi; shift
 done
 

The key to that is you would just call chk_run with an argument list each member of which would indicate to it what it should be checking each iteration.

loop()
 until [ "$((1ドル&&2ドル))" -eq 1 ]
 do chk_run "$@"
 sleep 2
 done >/dev/null 2>>"$log"
agentd=0 server=0 loop agentd server

POSIXly the only thing that should need altering there is the ps command - because POSIX doesn't specify the -C switch. And so you could just change the if line to look like:

if ps -eocomm= |
 grep -xqF zabbix_"1ドル"

Aside from mutt, service, and the ps optimization, it should all be standard command language. At least one advantage to that is the #!/bin/bash hash-bang is completely unnecessary - there is no anchor here to some shell-specific extension, and so it should work pretty much exactly the same in all shells which strive for POSIX-compliance. That means that #!/bin/dash is a very simple optimization in this case.

lang-bash

AltStyle によって変換されたページ (->オリジナル) /