Hello, On Tue, 16 Feb 2010, David C. Rankin wrote: [...] We're getting there ;)
#!/bin/bash # check for root and use sudo if not root [[ $UID -eq 0 ]] && prefix="" || prefix="sudo "
How about 'SUDO' instead of prefix? But anyway: using sudo that way is a bad idea. Any /etc/sudoers containing '.* ALL=(root) ALL' or equivalent, even with 'targetpw' set, is doomed to be r00ted or 0wnzrd (or whatever you may call it ;) And checking $UID is unsafe too, I'd guess (the environment is not to be trusted). Use the following instead: test $(uid -u) -eq 0 || sudo /our/script/with/fullpath and then add only /our/script/with/fullpath to sudoers, for those users that shall be able to start the script manually (i.e. from outside an initscript).
runfile=/var/run/chkdsl0.run
Ok, though I see more *.pid than *.run under /var/run ;) But extensions, shmektensions, we don't really care anyway. It's just a hint / convention as to what these files are supposed to contain. "Is dat fing wif der Pid in, no?"[1], so I prefer pidfile and foo.pid ;)
tmpfile=/tmp/chkdsl0
Unneccessary (see below). And dangerous. Generally: Never ever use a tempfile with a static name.
ckinterval=${1:-60}
You can't wait 60s for the connection? Ugh. I'd propose 300 or more ;)
cleanup() { ${prefix}rm -f $runfile }
Should clean out "$tmpfile" too. And quote the "$runfile". Iff you use a tempfile.
trap cleanup SIGINT SIGTERM EXIT
trap cleanup HUP INT TERM EXIT
## Change this test based on your 'ifconfig' output (this is temporary) dsl0test() { ping -q -c1 yahoo.com &>/dev/null return $? }
return is superfluous. A function returns $? of the last command. Experiment with: $ bash -c 'foo() { echo -n "foo: "; false; return $?; }; bar() { echo -n "bar: "; true; false; }; foo; echo $?; bar; echo $?;' foo: 1 bar: 1 (and add/change the last command around ;) Explicitly returning $? doesn't harm though. So: dsl0test() { ping -q -c 1 -s 1 yahoo.com >dev/null 2>&1; } (other domains/hostnames with big pools of machines behind it are alternatives. E.g. www.google.com, www.microsoft.com etc. There are a lot more IPs involved than you can get at with a single nslookup/'dig a' ;)
## if the runfile is still present check the PID to see if it is running [[ -f $runfile ]] && { ps ax | grep -q $(<$runfile) &>/dev/null PIDrunning=$? }
This is not robust. The PID may or may not appear in the output of 'ps' in other places, e.g. as part of a higher PID. And instead of parsing 'ps ax' correctly and robustly: ps ax | awk -vPID="$(<$runfile)" '$1 == PID { exit(0); } END{ exit 1;}' (and you'd then have to check if it's the script running with that pid. You could check that too with awk: ps -eo pid,cmd | awk -vPID="$(<$runfile)" \ '$1 == PID && $2 ~ /^[^[:space:]]scriptname[[:space:]]+/ { exit(0); } END{ exit 1;}' or something like that. Awk's quite nice for such stuff ;), I propose to use another easy but safe way: check_running() { if test -r "$runfile"; then ### whee, we got a pidfile PID="$(<$runfile)" ### what pid are we to check if test -e "/proc/${PID}/cmdline"; then ### ok, something's running ### with that pid if grep -q '[o]ur_scriptname' "/proc/${PID}/cmdline"; then ### yep, this should be already running with PID="$PID" return 0; fi fi fi return 1; }
## if not already running, start checking dsl0 connection if [[ ! -f $runfile ]] || [[ $PIDrunning != 0 ]]; then
if ! check_running; then
echo "$$" > $tmpfile && ${prefix}cp $tmpfile $runfile && rm $tmpfile
Forget about the tempfile. Just write to the pidfile. We're root at this point anyway (see above). echo "$$" > "$pidfile" BTW: using sudo without specifying _explicitly allowed commands in /etc/sudoers, then you could just as well run the whole script as root, login as root, run X as root, shoot yourself in the foot. See above for a possibility.
## keep runing until killed or $runfile deleted
That's a bit unusual ...
while [[ -f $runfile ]] && [[ $$ == $( <$runfile ) ]]; do
... and I don't really see a reason for this ... But I'm a bit tired.
## if still OK, sleep for $ckinterval seconds, if dls0 down restart network if dsl0test; then sleep $ckinterval else ${prefix}rcnetwork restart fi done fi
exit 0
HTH, -dnh, just providing bits and ideas, not a full script, as I may have messed up at this time of night. [1] tell me if I'm channeling Detritus too much -- "What, you don't think "insmod emacs" is a good idea?" -- Joe Moore -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org