Am I being run interactively?

1 comment | 1I like it!
January 12, 2005, 12:30 PM —  ITworld.com — 

In one of last month's columns, we looked at a method for determining whether a process was being run by cron. In that column, we used the ptree command, which shows a process' lineage in an indented display like that shown below, to determine whether the "ancester" of the process is the cron process. In this week's column, we'll examine an even simpler way to determine whether a process is being run interactively -- by using the tty command.

% ptree $$
335   /usr/local/sbin/sshd
  2675  /usr/local/sbin/sshd
    2677  /usr/local/sbin/sshd
      2679  -bash
        2682  ptree 2679

The tty command displays the name of the terminal that is open as standard input. In other words, it identifies the terminal device that is associated with your login session. Type the tty command at your system prompt and you will see something like this:

% tty
/dev/pts/2

The who command displays this same information along with some additional data, but displays your terminal in a different and less useful format. In particular, if you are logged in more than once on the same system, the tty command will quickly tell you which tty is associated with each login session; the who command will not.

% who
shs        pts/2        Jan 13 08:22    (10.11.10.11)
shs        pts/3        Jan 13 09:11    (10.11.10.11)

You can use the tty command in a script to easily determine whether the script has a controlling terminal and is, thus, being run interactively. When the tty command is run non-interactively, the output of the command (at least on a Solaris system) is "not a tty". To test this, you can insert a line like this in your crontab file:



10 10 * * * /usr/bin/tty > /export/home/shs/tty-log

Once every day, at 10:10 AM, a line "not a tty" will be added to your log.

A skeletal script for executing different logic depending on the output of the tty command might look like this:

---------------------------- cut here ----------------------------
#!/bin/bash

if `tty >/dev/null 2>&1` ; then
        echo "interactive"
else
        echo "non-interactive"
fi
---------------------------- cut here ----------------------------

The "if `tty >/dev/null 2>&1`" statement is one of those cute little Unix tricks that requires a little explanation. Knowing that the tty command will either yield a string such as "/dev/pts/2" or will issue the phrase "not a tty", we need to determine which response we get to determine whether or not we have a controlling terminal.

=One way we could do this would be to construct an (excuse the pun!) awkward command such as this:

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

I like it!
Comments

Slightly simpler still, and

Slightly simpler still, and avoids exec'ing tty ...

if [ -t 0 ]; then
  echo "interactive"
else
  echo "non-interactive"
fi
| reply
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