[comp.unix.ultrix] Shell

cliffb@isavax.isa.com (cliff bedore*) (05/29/90)

I'm having a problem with a csh program that I wrote to log out idle users.
I originally wrote it as something of an exercise in learning to program in
"csh" but it seemed useful and it worked so we use it here.  However, I'm
having a problem getting it to work in one of our systems.  I've enclosed
the program below and those who would like to use it may feal free to do so.
It is run from cron every ten minutes and at least on our one system works
fine.  However it does not work at all under a second "identical" system.

Background.  MVII Ultrix 3.0 both machines 1 BA123 1 BA23, rd53's.
Program invoked from cron on both machines.

Symptoms are: on machine where it doesn't work, it seems to stop at the line
in the inner loop where it says "set proc=($proct)".  (Many debug statements 
show it not getting past this point.)  All of these problems show up when
run from cron.  When run as root (or su) it runs fine.  When I kill cron and
invoke cron as root, it works fine but when it is invoked from cron that
started at boot it won't work.  I've checked file permissions for
cron,vmunix etc etc and I can't find anything different.  I'm stumped and
looking for ideas.  It's probably something stupid but I can't find it.  


Any ideas???

Cliff


Here's the program (with debug statements)


#! /bin/csh -f
# Copyright 1989 Clifford J. Bedore
#set path=(/usr/local /bin /usr/ucb /usr/bin)
set path=(/usr/ucb /bin /usr/bin /etc /usr/etc /usr/local /usr/new /usr/hosts)
setenv PATH /usr/ucb:/bin:/usr/bin:/etc:/usr/etc:/usr/local:/usr/new:/usr/hosts 
echo " " >> /usr/tech/cliffb/idle_log  
echo "************************************" >> /usr/tech/cliffb/idle_log  
echo `date` >> /usr/tech/cliffb/idle_log  
echo `whoami` >> /usr/tech/cliffb/idle_log  
echo $path>> /usr/tech/cliffb/idle_log  
foreach usert ("`w -h |grep 'm ....[0-9]' |cut -c1-9,10-17,30-31`")
  set user=($usert)
  if ($1 == "debug") then
   echo $user >> /usr/tech/cliffb/idle_log 
  endif
  set time=$user[3]
  if ($time <30 ) then
# do warning
   if ($time >= 10) then
    @ remain = $time % 10
    @ temptime = 30 - $time 
    @ outtime = $temptime + $remain
    echo "`date`" >/dev/$user[2]
    echo " you have been idle for $time minutes
    echo " you will be logged off in approx $outtime minutes
    if ($1 == "debug") then
     echo " warning $user" >> /usr/tech/cliffb/idle_log 
     echo " you have been idle for $time minutes
     echo " you will be logged off in approx $outtime minutes
    endif
   endif
  else #do logout
   set portno=`echo $user[2] |cut -c4-5`
    if ($1 == "debug") then
     echo "new port $portno">> /usr/tech/cliffb/idle_log  
    endif
   if ($portno == "so") then
     set portno="co"
     set user=($user[1] "co" $user[3])
    if ($1 == "debug") then
     echo "new $user ">> /usr/tech/cliffb/idle_log  
    endif
   endif
     echo "just before foreach $user ">> /usr/tech/cliffb/idle_log  
   foreach proct ("`ps -aux |grep $user[1]|cut -c1-9,10-15,36-37`")
     echo "just after foreach $user proc $proct">> /usr/tech/cliffb/idle_log  
     set proc=($proct)
     echo "just after set proc $user ">> /usr/tech/cliffb/idle_log  
     if ($1 == "debug") then
      echo $proc>> /usr/tech/cliffb/idle_log  
      echo $proc[3] $portno>> /usr/tech/cliffb/idle_log   
     endif
     echo "just after debug proc $user ">> /usr/tech/cliffb/idle_log  
     if ( $proc[3] == $portno ) then
       kill -15 $proc[2]
       kill -9 $proc[2]
       if ($1 == "debug") then
        echo "$proc[2] was killed">> /usr/tech/cliffb/idle_log   
       endif
     endif 
   end
   echo $user[1]" was logged out on `date` " >> /usr/spool/mqueue/kill_log
   echo $user[1]" was logged out on `date` " >> /usr/tech/cliffb/idle_log
  endif
end

stefan@wheaton.UUCP (Stefan Brandle ) (05/30/90)

In article <1990May29.132532.15379@isavax.isa.com> cliffb@isavax.isa.com (cliff bedore*) writes:
>I'm having a problem with a csh program that I wrote to log out idle users.
>... All of these problems show up when
>run from cron.  When run as root (or su) it runs fine.  When I kill cron and
>invoke cron as root, it works fine but when it is invoked from cron that

How do you call it from cron?  I have this idea that cron runs `sh' by default,
so you might need to do something like 

10 * * * * /bin/csh your_program

Otherwise cron might be trying to run it from the Bourne shell.  (Since the
Bourne shell doesn't look at the `#!/bin/csh', that doesn't help you).

So maybe you are running it differently from cron on the two different machines?
Hope this helps...

-Stefan
-- 
---------------------------------------------- MA Bell: (708) 260-4110 ---------
Stefan Brandle                  UUCP: ...!{obdient,uunet!tellab5}!wheaton!stefan
Wheaton College			or	stefan@wheaton.UUCP
Wheaton, IL 60187 		"But I never claimed to be sane!"