zsh-users
 help / color / mirror / code / Atom feed
* interactive vs cron called
@ 1997-12-18 20:30 Jason Zapman II
  1997-12-18 21:11 ` Frank Li
  1997-12-18 21:47 ` Jason Zapman II
  0 siblings, 2 replies; 7+ messages in thread
From: Jason Zapman II @ 1997-12-18 20:30 UTC (permalink / raw)
  To: zsh-users

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.

Any ideas?

Thanks;
Jason

-- 
     Jason Price    |     If you want to build a ship, don't drum up people 
      Theta Xi,     |   together to collect wood and don't assign them tasks
   Beta, Alpha 449  | and work, but rather teach them to long for the endless 
 jprice@poboxes.com |    immensity of the sea. -- Antoine de Saint Exupery


^ permalink raw reply	[flat|nested] 7+ messages in thread
* Re: interactive vs cron called
@ 1997-12-18 21:05 Sweth Chandramouli
  0 siblings, 0 replies; 7+ messages in thread
From: Sweth Chandramouli @ 1997-12-18 21:05 UTC (permalink / raw)
  To: zsh-users


On Thu, Dec 18, 1997 at 03:30:51PM -0500, Jason Zapman II wrote:
> 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.
	why not just have it print output to screen, and in the crontab run the 
command through a pipe to a command-line mailer?
	alternately, write your script to accept a command line option to send 
you mail, and invoke the cron using that option.
	
	-- sweth.

-- 
"Countin' on a remedy I've counted on before
Goin' with a cure that's never failed me
What you call the disease
I call the remedy"  -- The Mighty Mighty Bosstones


^ permalink raw reply	[flat|nested] 7+ messages in thread
* Re: interactive vs cron called
@ 1997-12-19  9:14 John Riddoch
  0 siblings, 0 replies; 7+ messages in thread
From: John Riddoch @ 1997-12-19  9:14 UTC (permalink / raw)
  To: zsh-users

> 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


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~1997-12-19  9:43 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-12-18 20:30 interactive vs cron called Jason Zapman II
1997-12-18 21:11 ` Frank Li
1997-12-18 21:47 ` Jason Zapman II
1997-12-19  1:44   ` Danek Duvall
1997-12-19  2:34     ` Manual removal ramos
1997-12-18 21:05 interactive vs cron called Sweth Chandramouli
1997-12-19  9:14 John Riddoch

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).