Operating System - HP-UX
1833871 Members
1656 Online
110063 Solutions
New Discussion

Run a command as user without password

 
SOLVED
Go to solution
Coolmar
Esteemed Contributor

Run a command as user without password

Hi folks,

One of my users (USER1) needs to run a command that belongs to USER2. The problem is that before the command is run, the profile for USER2 has to be run. I realize that su - USER2 -c "command" could work, but it is being run from a batch file and therefore prompting for a password is no good. "su" doesn't seem to have a switch to hard code the password. On the other hand SUDO will run the command with the -S switch allowing for the password, but SUDO will not run the .profile of USER2 and I read in another forum that it is not possible either with SUDO. So any ideas on how I can run the profile of USER2, run the command, and pass the password in a batch file?

Thanks!
4 REPLIES 4
Steven E. Protter
Exalted Contributor
Solution

Re: Run a command as user without password

The only user that can do what you wish is root.

Its obvious why you don't want to use root.

The other solution is to take the command that user2 owns and put it in a common area like /usr/contrib/bin

There permnissions can be set to allow user1 to run the command.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
RAC_1
Honored Contributor

Re: Run a command as user without password

Prepare a script that will run
user2's profile
user2 command

Call it xxx, run this xx through sudo

Anil
There is no substitute to HARDWORK
A. Clay Stephenson
Acclaimed Contributor

Re: Run a command as user without password

Probably the least evil way to accomplish this is via sudo. Although you think you need to include a the user's .profile, I submit that you do not because almost all .profile's in HP-UX contain commands that deal with terminals (stty, tset, tabs) that will hang when stdin is not a terminal.
Here is what i would do. Create a file (e.g. /usr/local/bin/USER2env.sh) that sets and exports any needed environment variables. This file must not contain an exit or return statement. Next, alter USER2's .profile and remove all the environment variables you set in /usr/local/bin/USER2env.sh and insert this single line:
. /usr/local/bin/USER2env.sh

and also insert the same line in your batch script. The "." command sources the file for both USER2's .profile and your command and everyone is happy.
If it ain't broke, I can fix that.
Coolmar
Esteemed Contributor

Re: Run a command as user without password

Thanks everyone. I will try your suggestions.