Unix Tip: Am I interactive?: Take three

January 19, 2005, 11:34 AM —  ITworld.com — 

Send in your Unix questions today!


See additional Unix tips and tricks


If there's one thing that every Unix systems administrator knows, it's that there's always more than one way to get something done. Some ways of solving a problem are more efficient than others. Other ways of solving a problem are easier to understand or extend to similar, but not identical, situations. But there are always multiple solutions to a task.

In recent columns, we have looked at two very different methods of determining whether or not a script was being run interactively or through cron. In the first, we used the Solaris ptree command to determine the parentage (or "ancestry") of a task. If a process was started by crond or its parent process was started by crond, we would know that it was run through cron. In the second, we used the tty command to identify the controlling terminal. In fact, just knowing that there is a controlling terminal (such as /dev/ttyp1), we know a process is being run interactively; cron jobs do not have controlling terminals. In this third and last method, we will use a very simple conditional test, built into the shell, to determine whether a process is interactive. This simple test, -t, is in the same family of tests as -f (testing if there is a file with the specified name) and -d (testing if there is a directory with the specified name). The -t test determines whether the specified file descriptor is associated with a terminal. Let's look at a couple of examples to see how this works.

On the command line, we can use the -t test like this:

$ if [ -t 0 ]; then
> echo interactive
> fi
interactive

Alternately, we can turn the command into a simple script. In this example, we're looking at a Bourne shell script, though this same syntax should work with bash and ksh. We are using the -t test to determine whether there is a terminal associated with file descriptor 0 (i.e., standard in). If there is an associated terminal, we print the message "interactive". If there is no associated terminal, we print "non-interactive".

#!/bin/sh
# test0

if [ -t 0 ]; then
   echo interactive
else
   echo non-interactive
fi

Of course, what we need to do next is verify that we get the opposite response when we're not running our script interactively. To run this script through cron and verify that its output will be "non-interactive", we'll set up a cron task like this:

5 * * * * /export/home/shs/test0 > /var/log/test.log

Once the script runs, we should see the word "non-interactive" in our log file, basically demonstarting that standard in for a cron job is not associated with a terminal.

Sign up for ITworld's Daily newsletter
Follow ITworld on Twitter @IT_world

I like it!
Post a comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
peer-to-peer

Esther Schindler
If the comments are ugly, the code is ugly

claird
SVG a graphics format for 21st century

pasmith
Take Chrome OS for a test spin

Sandra Henry-Stocker
Solaris Tip: Have Your Files Changed Since Installation?

sjvn
64-bits of protection?

jfruh
Android fragments vs. the iPhone monolith

mikelgan
What Gizmodo missed about the Pro WX Wireless USB disk drive

 

Where Google Chrome security fails: the password
I heard mention that the Chrome OS will have some sort of encryption available a la bitlocker. If it's possible to encrypt personal data using another password or key, then it may have potential for very secure data.... And Ubuntu has an 'encrypt home directory' option, perhaps google should follow suit.
- Dann

Join the conversation here

The Daily Tip

The Daily TipQuick, practical advice for IT pros. Made fresh daily.

Hot tips:

Want to cash in on your IT savvy? Send your tip to tips@itworld.com. If we post it, we'll send you a $25 Amazon e-gift card.

Newsletters

Subscribe to ITWORLD TODAY and receive the latest IT news and analysis.

I would like to receive offers via email from ITworld partners.
By clicking submit you agree to the terms and conditions outlined in ITworld's privacy policy.
Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

Marketplace