From: John Riddoch <jr@scms.rgu.ac.uk>
To: zsh-users@math.gatech.edu
Subject: Re: interactive vs cron called
Date: Fri, 19 Dec 1997 09:14:50 +0000 (GMT) [thread overview]
Message-ID: <199712190914.JAA04785@astar.scms.rgu.ac.uk> (raw)
> 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
next reply other threads:[~1997-12-19 9:43 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
1997-12-19 9:14 John Riddoch [this message]
-- 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=199712190914.JAA04785@astar.scms.rgu.ac.uk \
--to=jr@scms.rgu.ac.uk \
--cc=zsh-users@math.gatech.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).