📄 ch40_04.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 40] 40.4 Choosing the Shell Run (We Hope) by at </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:50:34Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch40_01.htm"TITLE="40. Delayed Execution"><LINKREL="prev"HREF="ch40_03.htm"TITLE="40.3 The at Command "><LINKREL="next"HREF="ch40_05.htm"TITLE="40.5 Avoiding Other at and cron Jobs "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch40_03.htm"TITLE="40.3 The at Command "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 40.3 The at Command "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 40<BR>Delayed Execution</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch40_05.htm"TITLE="40.5 Avoiding Other at and cron Jobs "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 40.5 Avoiding Other at and cron Jobs "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-2314">40.4 Choosing the Shell Run (We Hope) by at </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-44556"></A><ACLASS="indexterm"NAME="AUTOID-44559"></A><ACLASS="indexterm"NAME="AUTOID-44562"></A><ACLASS="indexterm"NAME="AUTOID-44565"></A>If you are using BSD UNIX, the <EMCLASS="emphasis">-c</EM> option tells<SPANCLASS="link"><EMCLASS="emphasis">at</EM> (<ACLASS="linkend"HREF="ch40_03.htm"TITLE="The at Command ">40.3</A>)</SPAN>toexecute your script via the C shell; the <EMCLASS="emphasis">-s</EM> option tells <EMCLASS="emphasis">at</EM>to use the Bourne shell.<EMCLASS="emphasis">at</EM> defaults to the shell you log in with.If you're lucky (and many users are), this will work the first time.But I've seen a fair number of users run into sticky problems whenthey first use <EMCLASS="emphasis">at</EM> on a new system.If you're one of those unlucky users, here's some help.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-44576"></A><EMCLASS="emphasis">at</EM> jobs are run in an environment that'sdifferent from your normal login sessions.Be careful about using command aliases, shell functions, and variables,and other things that may not be set for you by the system.The easiest way to find out what's happening is by having your jobwrite its environment into some temporary files, then read them afterthe job runs:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"> <ACLASS="co"HREF="ch06_08.htm"TITLE="6.8 Shell Variables ">set</A> <ACLASS="co"HREF="ch06_01.htm"TITLE="6.1 What Environment Variables Are Good For ">printenv</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">% <CODECLASS="userinput"><B>at 1234</B></CODE>set<CODECLASS="userinput"><B> > $HOME/at.set</B></CODE>printenv<CODECLASS="userinput"><B> > $HOME/at.env</B></CODE><KBDCLASS="keycap">[CTRL-d]</KBD></PRE></TD></TR></TABLE></P><PCLASS="para">(On some systems you'll need <CODECLASS="literal">$LOGDIR</CODE> instead of <CODECLASS="literal">$HOME</CODE>and <EMCLASS="emphasis">env</EM> instead of <EMCLASS="emphasis">printenv</EM>.)</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-44594"></A><ACLASS="indexterm"NAME="AUTOID-44597"></A>If you use a shell like <EMCLASS="emphasis">csh</EM> or <EMCLASS="emphasis">bash</EM> that reads a setupfile when every shell (not just a<SPANCLASS="link">login shell (<ACLASS="linkend"HREF="ch02_08.htm"TITLE="Identifying Login Shells ">2.8</A>)</SPAN>)starts, the shell will read your per-shell file (like <EMCLASS="emphasis">.cshrc</EM>or <EMCLASS="emphasis">.bashrc</EM>) when the job starts running.This is good news and bad news.The good news is that you can set shell parameters to be used by your <EMCLASS="emphasis">at</EM>job.If you have interactive commands in your <EMCLASS="emphasis">.cshrc</EM>, though, your <EMCLASS="emphasis">at</EM>job might ignore them or might hang forever, waiting for an answer.For instance, the<SPANCLASS="link"><EMCLASS="emphasis">tty</EM> (<ACLASS="linkend"HREF="ch03_08.htm"TITLE="What tty Am I On? ">3.8</A>)</SPAN>command will print the error <CODECLASS="literal">not a tty</CODE>;if you try to use <EMCLASS="emphasis">tty</EM> to set a shell variable, it can cause "unsetvariable" errors, which can abort your <EMCLASS="emphasis">.cshrc</EM> file... and so on.That's the bad news.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-44613"></A>You can use a <SPANCLASS="link"><EMCLASS="emphasis">set prompt</EM> test (<ACLASS="linkend"HREF="ch02_09.htm"TITLE="Speeding Up Your C Shell with set prompt Test ">2.9</A>)</SPAN>in your <EMCLASS="emphasis">.cshrc</EM> file, or test <CODECLASS="literal"> $-</CODE> in <EMCLASS="emphasis">ksh</EM> or<EMCLASS="emphasis">bash</EM>, to make sure that there are no interactive commandsrun by <EMCLASS="emphasis">at</EM>.But unless I need shell features, I usually just use <EMCLASS="emphasis">at s</EM> to run the job under the Bourne shell. (Note that some systems use<EMCLASS="emphasis">ksh</EM> or <EMCLASS="emphasis">bash</EM> as their "Bourne" shell.)</P><PCLASS="para">Our SunOS 4.1.3 system has a problem:It sets the<EMCLASS="emphasis">prompt</EM> variablein the C shell that runs <EMCLASS="emphasis">at</EM> jobs; this makes the shell readmy <EMCLASS="emphasis">.cshrc</EM> file as if I were doing an interactive login!My <EMCLASS="emphasis">at</EM> jobs were all failing with a complaint about an unset<EMCLASS="emphasis">TERM</EM> variable.There's a workaround for this in article<ACLASS="xref"HREF="ch02_10.htm"TITLE="Gotchas in set prompt Test ">2.10</A>.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-44634"></A>Here's a way to track down problems like that.Temporarily add the command<CODECLASS="literal">set verbose echo</CODE> to the first line of your <EMCLASS="emphasis">.cshrc</EM> file,or add<SPANCLASS="link"><CODECLASS="literal">set -xv</CODE> (<ACLASS="linkend"HREF="ch46_01.htm#UPT-ART-7160"TITLE="Tips for Debugging Shell Scripts ">46.1</A>)</SPAN>to the first line of your Korn Shell <EMCLASS="emphasis">ENV</EM> file or <EMCLASS="emphasis">bash</EM><EMCLASS="emphasis">.bashrc</EM> file.When the <EMCLASS="emphasis">at</EM> job starts your shell, you'll see verbose messages thatshow commands running and variables being set in the shell startupfile.You'll probably also see your shell execute the commands from the<EMCLASS="emphasis">at</EM> job itself.All of this information will be emailed to you (on versions of <EMCLASS="emphasis">at</EM>I've seen, at least) with a subject like "Output from your at job."This is a great way to track down tricky <EMCLASS="emphasis">at</EM> problems in your shellsetup files.Another tip for spotting problems: sprinkle commands like thesethrough your shell startup file:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"> <ACLASS="co"HREF="ch13_01.htm"TITLE="13.1 Using Standard Input and Output">>></A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">echo "got to <CODECLASS="replaceable"><I>here</I></CODE>" | mail <CODECLASS="replaceable"><I>yourname</I></CODE>echo "got to <CODECLASS="replaceable"><I>here</I></CODE>" >> $HOME/at.log</PRE></TD></TR></TABLE></P><PCLASS="para">After an <EMCLASS="emphasis">at</EM> job runs, that lets you find out how far it got beforeyour shell ran into trouble.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</SPAN>, <SPANCLASS="authorinitials">ML</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch40_03.htm"TITLE="40.3 The at Command "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 40.3 The at Command "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch40_05.htm"TITLE="40.5 Avoiding Other at and cron Jobs "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 40.5 Avoiding Other at and cron Jobs "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">40.3 The at Command </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">40.5 Avoiding Other at and cron Jobs </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed & awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -