From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 22250 invoked from network); 19 Dec 1997 09:43:55 -0000 Received: from ns2.primenet.com.au (7795@203.24.36.3) by ns1.primenet.com.au with SMTP; 19 Dec 1997 09:43:55 -0000 Received: (qmail 1235 invoked from network); 19 Dec 1997 09:43:40 -0000 Received: from math.gatech.edu (list@130.207.146.50) by ns2.primenet.com.au with SMTP; 19 Dec 1997 09:43:40 -0000 Received: (from list@localhost) by math.gatech.edu (8.8.5/8.8.5) id EAA17024; Fri, 19 Dec 1997 04:17:49 -0500 (EST) Resent-Date: Fri, 19 Dec 1997 04:13:55 -0500 (EST) Message-Id: <199712190914.JAA04785@astar.scms.rgu.ac.uk> Date: Fri, 19 Dec 1997 09:14:50 +0000 (GMT) From: John Riddoch Reply-To: John Riddoch Subject: Re: interactive vs cron called To: zsh-users@math.gatech.edu MIME-Version: 1.0 Content-Type: TEXT/plain; charset=us-ascii Content-MD5: 8CdHhYWGs7gYuGCGg1up/w== X-Mailer: dtmail 1.2.0 CDE Version 1.2 SunOS 5.6 sun4m sparc Resent-Message-ID: <"PNOme.0.y84.HhZcq"@math> Resent-From: zsh-users@math.gatech.edu X-Mailing-List: archive/latest/1212 X-Loop: zsh-users@math.gatech.edu X-Loop: zsh-workers@math.gatech.edu Precedence: list Resent-Sender: zsh-workers-request@math.gatech.edu > Is there a way to tell if a script is being called by cron or via > interactive script? I'm writing something to tell me when disk usage of > various partitions get's to dangerous levels. When it's run via cron, I > want it to mail me the results. If it's interactive, I want it to print > the output to the screen. A rather complex solution, but should work nonetheless. Find the parent process of the script (ie, parent of $$) and then put a name to it. If the parent is /usr/sbin/cron, it's cron invoked, else its interactive. I'm not 100% sure if parent process would be the shell (e.g. /usr/local/bin/zsh) but you could probably get it's parent process. How about: PPID=`ps -Al | nawk -v pid=$$ '{ if ( $4 == pid ) print $5 }'` PPNAME=`ps -Al | nawk -v ppid=$PPID '{ if ( $4 == ppid ) print $14 }'` Do a check on PPNAME, et voila! >>From a quick test, if invoked via interactive script, it returns 'zsh' (ie, the shell). If invoked via crontab, it returns 'sh' (since crontab invokes everything under /bin/sh). If you take it a step up, the interactive shell will return xxxx (for me it's currently dtterm) and the crontab will return cron. So: PPID=`ps -Al | nawk -v pid=$$ '{ if ( $4 == pid ) print $5 }'` PPPID=`ps -Al | nawk -v ppid=$PPID '{ if ( $4 == ppid ) print $5 }'` PPPNAME=`ps -Al | nawk -v pppid=$PPPID '{ if ( $4 == pppid ) print $14 }'` and now PPPNAME will be the name of the parent of the parent of the current process. NB: from a quick check on ps -Al, the 11th field doesn't seem to always print; I believe off the top of my head there's a way to check the last field which would circumvent this. -- John Riddoch Programmer/Webmaster Room C6, School of Computer and Mathematical Science Robert Gordon University, Aberdeen, AB25 1HG Telphone: (01224)262721 Email jr@scms.rgu.ac.uk