Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cron job and "am I" option

Status
Not open for further replies.

bi

Technical User
Apr 13, 2001
1,552
0
0
US
I am running a Korn shell script as root from cron. The script su's to a user (using the su - name -c) and then runs three commands with a semi-colon in between each command.

error messages are sent to root's mail. I get the following messages:

stty: : No such device or address
stty: : No such device or address

Must be attached to terminal for 'am I' option

The job runs, apparently OK.

What is the 'am I' option and how do I correct what must be an error in my script?
 
Your script must contain the command who am i which only works when you're running it from a terminal. A cron job, since it's set off automatically by the cron daemon, is not associated with any terminal.

The command may be either in your script or in that user's .profile. Annihilannic.
 
why start as root and su to the other user?
why not run cron as that user ? -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
I have an idea that there is a method of checking in your .profile whether the session is connected to a terminal or whether it is being run from, e.g. a cron job. I saw somewhere that, in the c shell, one creates a if-prompt-endif clause.

However, I'm using the Bourne shell and bi, I notice, uses the Korn shell. Does anyone know the syntax for the equivalent if clause there? Can it be that if-tty clause which appears in local.profile when creating a new user under Solaris 8?

mvh/regards

James
 
The following sh/ksh script would do what you describe:

[tt]if [ "`/usr/bin/tty`" != "not a tty" ]
then
echo "It's a tty."
fi[/tt]

Note that your .profile is not run before a cron job. Annihilannic.
 
Thanks Annihilannic:

I did a bit of experimenting today and came up with:

if [ ! "`tty -s`" ] ; then
echo "It's a tty"
fi

Basically the same thing in reverse, since I didn't know what text would appear if it were not a tty. The results of `tty -s` are 0: it's a tty, 1: it's not a tty >1: error.

I didn't know that one's .profile was not run before a cron job. It must be the one at /etc/profile which gets run, because _some_ profile sure gets run before the cron job.

Or can it be that, say, a cron job beginning:

su - someuser -c somecommand

will run someuser's .profile?

mvh/regards

James
 
I forgot to suggest to bi how he can get rid of the mails in root's mailbox. (It was for the same reason that I was looking for a solution. We have root mailboxes which consist almost entirely of this kind of message so that really important messages disappear in the flood.)

Do this: put the commands in /etc/profile which require a terminal to be attached within an if-fi clause of the kind Annihilanic or I have suggested. stty is a common one, often used to make the backspace key behave as one expects it to. who am i is another. Do the same in /.profile (root's) and the ~/.profile file belonging to the cron job user. (Of course do this in your own .profile also, and suggest to your System Administrator that all users have their .profiles configured in this way.) If you still get these messages in root's mailbox, you have missed one (or more). Try and identify it and put it inside your if-fi clause.

Don't put the whole lot inside the if-fi clause, as none of them will then be implemented when your cron job starts, and some of them, for example PATH and LD_LIBRARY_PATH, will probably be necessary.

Let us know how you get on or if you have further concerns around this problem.

mvh/regards

James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top