zsh-users
 help / color / mirror / code / Atom feed
* Re: interactive vs cron called
@ 1997-12-19  9:14 John Riddoch
  0 siblings, 0 replies; 6+ 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] 6+ messages in thread

* Re: interactive vs cron called
  1997-12-18 21:47 ` Jason Zapman II
@ 1997-12-19  1:44   ` Danek Duvall
  0 siblings, 0 replies; 6+ messages in thread
From: Danek Duvall @ 1997-12-19  1:44 UTC (permalink / raw)
  To: zsh-users

Checking the return value of tty -s does the trick for me in a similar
situation (distinguishing between command-line invocation and getting
called as a pipe), as it tells you what's attached to stdin.  I'm pretty
sure that cron runs its jobs with stdin not connected to anything, so that
trick ought to work for you, too.

Perl has a builtin function for this, as the -t flag to the [ function.  It
might be useful to have this functionality in zsh, too, but as it's
obviously possible with a functioning unix setup, it's not necessary.

Danek


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

* Re: 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
  1997-12-19  1:44   ` Danek Duvall
  1 sibling, 1 reply; 6+ messages in thread
From: Jason Zapman II @ 1997-12-18 21:47 UTC (permalink / raw)
  To: zsh-users

Jason Zapman II <zapman@cc.gatech.edu> writes:
> When it's run via cron, I want it to mail me the results.

I should have made myself more clear.  I know the output of cron is mailed
to the account for which it is run, but I want it mailed to a DIFFERENT
account.  (script to be run as root, mail going to my more often read
account, as this is something along the lines of a warning bell)

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] 6+ messages in thread

* Re: 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
  1 sibling, 0 replies; 6+ messages in thread
From: Frank Li @ 1997-12-18 21:11 UTC (permalink / raw)
  To: Jason Zapman II; +Cc: zsh-users

On 18 Dec 1997, 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.
> 
> Any ideas?
> 

actually, just have the program output to the screen since crontab will
automatically E-mail any stdout of the program to you anyway. Try it. 

--------------------------------------------------------------------------
Frank Li				Assistant System Admin (Finally !)
E-mail(work)  fli@austin.dsccc.com
E-mail(personal) prefect@ccwf.cc.utexas.edu
What I say is what I think, and no one else's (including the company)
--------------------------------------------------------------------------


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

* Re: interactive vs cron called
@ 1997-12-18 21:05 Sweth Chandramouli
  0 siblings, 0 replies; 6+ 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] 6+ messages in thread

* 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; 6+ 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] 6+ messages in thread

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

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-12-19  9:14 interactive vs cron called John Riddoch
  -- strict thread matches above, loose matches on Subject: below --
1997-12-18 21:05 Sweth Chandramouli
1997-12-18 20:30 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

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).