Unix Tip: Am I interactive?: Take three
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
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?
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
Quick, practical advice for IT pros. Made fresh daily.
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.













