Re: /bin/sh Madness
Dan Nelson wrote: In the last episode (Feb 16), Tim Daneliuk said: Here is a shell function that behaves quite strangely: #!/bin/sh # # Execute A Command, Noting Start/Stop Time, Logging Output # Args: # $1 Command Name # $2 Log Directory # $3 Command String To Execute # runupd() { log=$2/$1.log timestamp $log touch $2/.$1-begin eval $3 21 $log touch $2/.$1-end } # End of 'runupd()' So, you might do something like: runupd freespace /var/log/ df -k Now, for the weirdness. This function works fine in my script so long as one of two conditions is met: 1) I run it interactively from the command line (bash) OR 2) I run it from 'cron' AND $3 is *not* another script If I try to run it from 'cron' and point $3 to a script, everything gets run as planned, however, the ending timestamp (touch $2/.$1-end) never runs. That is, the initial time stamp (.$1-begin) and the command itself are executed, and output is properly written to the logfile, but the final timestamp never happens. Could your $3 command be returning a nonzero exit code? You probably want something more like touch $2/.$1-begin { eval $3 21 $log ; touch $2/.$1-end } I am looking into this. so your end timestamp always gets created whether or not $3 succeeds. Also note that in your original script, you only backgrounded touch $2/.$1-end, which is probably not what you wanted. H - I have invoked the script that uses this function a variety of ways and the command being evaled *is* being backgrounded in every case with the syntax shown above ... -- Tim Daneliuk [EMAIL PROTECTED] PGP Key: http://www.tundraware.com/PGP/ ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: /bin/sh Madness
Dan Nelson wrote: In the last episode (Feb 16), Tim Daneliuk said: Here is a shell function that behaves quite strangely: #!/bin/sh # # Execute A Command, Noting Start/Stop Time, Logging Output # Args: # $1 Command Name # $2 Log Directory # $3 Command String To Execute # runupd() { log=$2/$1.log timestamp $log touch $2/.$1-begin eval $3 21 $log touch $2/.$1-end } # End of 'runupd()' So, you might do something like: runupd freespace /var/log/ df -k Now, for the weirdness. This function works fine in my script so long as one of two conditions is met: 1) I run it interactively from the command line (bash) OR 2) I run it from 'cron' AND $3 is *not* another script If I try to run it from 'cron' and point $3 to a script, everything gets run as planned, however, the ending timestamp (touch $2/.$1-end) never runs. That is, the initial time stamp (.$1-begin) and the command itself are executed, and output is properly written to the logfile, but the final timestamp never happens. Could your $3 command be returning a nonzero exit code? You probably want something more like touch $2/.$1-begin { eval $3 21 $log ; touch $2/.$1-end } so your end timestamp always gets created whether or not $3 succeeds. Also note that in your original script, you only backgrounded touch $2/.$1-end, which is probably not what you wanted. This seems not to work - I don't think 'sh' likes nested function braces - recall that this command appears inside a shell function. In any case, I think I have nailed the problem. It seems that the external script being called made reference to 'chown' without explicitly naming the program's path. Under 'cron' control, there is no path to /usr/sbin by default so the script was exiting with a non-zero exit status as several here suggested. Wipes large glob off egg off very red face. Many thanks to all who took time to answer. -- Tim Daneliuk [EMAIL PROTECTED] PGP Key: http://www.tundraware.com/PGP/ ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: /bin/sh Madness
In the last episode (Feb 17), Tim Daneliuk said: Dan Nelson wrote: Could your $3 command be returning a nonzero exit code? You probably want something more like touch $2/.$1-begin { eval $3 21 $log ; touch $2/.$1-end } so your end timestamp always gets created whether or not $3 succeeds. Also note that in your original script, you only backgrounded touch $2/.$1-end, which is probably not what you wanted. This seems not to work - I don't think 'sh' likes nested function braces - recall that this command appears inside a shell function. Oops. Braces aren't bourne syntax, and in this case they're the wrong choice anyway. parens (i.e. a subshell) are what's needed. touch $2/.$1-begin ( eval $3 21 $log ; touch $2/.$1-end ) I actually tested this one :) In any case, I think I have nailed the problem. It seems that the external script being called made reference to 'chown' without explicitly naming the program's path. Under 'cron' control, there is no path to /usr/sbin by default so the script was exiting with a non-zero exit status as several here suggested. Wipes large glob off egg off very red face. Many thanks to all who took time to answer. -- Dan Nelson [EMAIL PROTECTED] ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
/bin/sh Madness
Here is a shell function that behaves quite strangely: #!/bin/sh # # Execute A Command, Noting Start/Stop Time, Logging Output # Args: # $1 Command Name # $2 Log Directory # $3 Command String To Execute # runupd() { log=$2/$1.log timestamp $log touch $2/.$1-begin eval $3 21 $log touch $2/.$1-end } # End of 'runupd()' So, you might do something like: runupd freespace /var/log/ df -k Now, for the weirdness. This function works fine in my script so long as one of two conditions is met: 1) I run it interactively from the command line (bash) OR 2) I run it from 'cron' AND $3 is *not* another script If I try to run it from 'cron' and point $3 to a script, everything gets run as planned, however, the ending timestamp (touch $2/.$1-end) never runs. That is, the initial time stamp (.$1-begin) and the command itself are executed, and output is properly written to the logfile, but the final timestamp never happens. I have been fiddling with this on- and off and cannot seem to explain the behavior. I',m guessing that something about the combination of 'cron'and the 21 $log business is causing the last touch to never get invoked, but for the life of me, I cannot figure this one out. Interestingly, this works fine in Linux (SuSE 10.0). In Linux, both 'sh' and 'bash' are the same binary (bash is running as sh). So .. I went back to FreeBSD (4.x in this case, though the problem is also noted in 6.x) and changed the SHELL variable in the crontabs file to use 'bash' - same problem. This is starting to feel like a problem with file handles getting clobbered in the FreeBSD exec logic. Other explanations welcome... -- Tim Daneliuk [EMAIL PROTECTED] PGP Key: http://www.tundraware.com/PGP/ ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: /bin/sh Madness
In the last episode (Feb 16), Tim Daneliuk said: Here is a shell function that behaves quite strangely: #!/bin/sh # # Execute A Command, Noting Start/Stop Time, Logging Output # Args: # $1 Command Name # $2 Log Directory # $3 Command String To Execute # runupd() { log=$2/$1.log timestamp $log touch $2/.$1-begin eval $3 21 $log touch $2/.$1-end } # End of 'runupd()' So, you might do something like: runupd freespace /var/log/ df -k Now, for the weirdness. This function works fine in my script so long as one of two conditions is met: 1) I run it interactively from the command line (bash) OR 2) I run it from 'cron' AND $3 is *not* another script If I try to run it from 'cron' and point $3 to a script, everything gets run as planned, however, the ending timestamp (touch $2/.$1-end) never runs. That is, the initial time stamp (.$1-begin) and the command itself are executed, and output is properly written to the logfile, but the final timestamp never happens. Could your $3 command be returning a nonzero exit code? You probably want something more like touch $2/.$1-begin { eval $3 21 $log ; touch $2/.$1-end } so your end timestamp always gets created whether or not $3 succeeds. Also note that in your original script, you only backgrounded touch $2/.$1-end, which is probably not what you wanted. -- Dan Nelson [EMAIL PROTECTED] ___ freebsd-questions@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to [EMAIL PROTECTED]