1830938 Members
2015 Online
110017 Solutions
New Discussion

script/cron question

 
Ridzuan Zakaria
Frequent Advisor

script/cron question

Hi,

I have a script run by an id then switch to another id in the middle of the script to complete the process.

i.e :
#/bin/ksh
...
....
(sleep 2
echo 'checkuser.sh'
sleep 2
exit ) | sw audituser
...
...

Note : sw - command to switch userid

The script run well if I ran it from command prompt but not thru cronjob. I am getting the follwoing error message:


rocess not attached to terminal
Usage: who [-rbtpludAasHTqRm] [am i] [utmp_like_file]

r run level
b boot time
t time changes
p processes other than getty or users
l login processes
u useful information
d dead processes
A accounting information
a all (rbtpludA options)
s short form of who (no time since last output or pid)
H print header
T status of tty (+ writable, - not writable, x exclusive open, ? hung)
q quick who
R print host name
Switching to audituser
pwd^M
Closed connection.^M
Process Completed


The requirement is to run the script under user A and then switch to user B (you cannot directly logged as user B but have to swtich from another user)

Is there any workaround?
Is there a different way to approach the problem?

I believe the SW script use whoami command and that is why it failed it is executed from cron and I can only use sw to switch id.

Thanks.
quest for perfections
7 REPLIES 7
Sridhar Bhaskarla
Honored Contributor

Re: script/cron question

Hi,

su without any option will complain if it does not have any terminal associated with it. I would suggest you run it using this way

su - audituser -c /wherever/checkuser.sh

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Ridzuan Zakaria
Frequent Advisor

Re: script/cron question

Sri,

The "su - audituser -c <script>" prompt for password.

audituser is a "no login id", it doesn't have password. The only way to logon to this account is from another user than switch to this user(in my case use sw command).

Thanks.
quest for perfections
harry d brown jr
Honored Contributor

Re: script/cron question

If you are running it from cron, then that's your issue, you don't have a terminal associated with your process.

fix the script?

live free or die
harry
Live Free or Die
Patrick Wallek
Honored Contributor

Re: script/cron question

You can only 'su' to another user without being prompted for a passwd if you are root.

If you are any other user, you will be prompted for a passwd if / when you 'su - username' to another user.
Jean-Louis Phelix
Honored Contributor

Re: script/cron question

hi,

To better help you, you should perhaps give us the 'sw' script, but the idea would be to add tests like :

if [ -t 1 ]
then
# stdout is a terminal
use whoami and stuff like stty
else
# run with no attached terminal (ie cron)
use another way to find info if really needed
fi

Regards.
It works for me (© Bill McNAMARA ...)
Steven Sim Kok Leong
Honored Contributor

Re: script/cron question

Hi,

You can use Expect scripting to automate such a process. The autoexpect tool makes this a breeze by creating the script for you as you walk through the su process.

The downside is that the Expect script will contain your user's password within itself.

If you have issues with the "whoami" command, then use the "who am i" command instead. One reports the user you su'ed to, the other reports the user you su'ed from.

Hope this helps. Regards.

Steven Sim Kok Leong
Ridzuan Zakaria
Frequent Advisor

Re: script/cron question

Hi,

I know the problem is with the
'sw script' and need to have the logic suggested by Jean.

Thanks everyone.
quest for perfections