Rohit
Rohit

Reputation: 7629

Enter password for a sudo command in a chain of commands

In Linux how can I enter the password for one of the commands in a chain of commands which requires a sudo. One example which I can think of is when after running a long compile job, I want to shutdown the machine.

make ; sudo init 0 

I want the shutdown command to run only after make finishes, but would like to enter the password right away, because I won't be there when the first command is done. Also, I don't want to run "make" with super user privileges. So switching to root and running the commands is also out of the question.

Thanks!

Upvotes: 5

Views: 5197

Answers (6)

Jay Sidri
Jay Sidri

Reputation: 6406

Why not pipe the password into sudo?

make; echo 'password' | sudo -S init 0

Upvotes: 1

Seth Robertson
Seth Robertson

Reputation: 31451

sudo sh -c "su -c 'make' $USER && init 0"

Change your uid early and often. That's the Chicago way!

Upvotes: 7

OpenSauce
OpenSauce

Reputation: 8623

You can run sudo -v to update the sudo timestamp - this means you won't need to enter a password for x minutes (default is 5). You can change the timeout by editing the timestamp_timeout in the sudoers file.

You also might want to change your command to

make && sudo init 0

which will only shut down if make completes successfully.

Upvotes: 3

DallaRosa
DallaRosa

Reputation: 5815

One possibility is to use Expect to automate the password input.

Here's a simple HOW-TO for expect and passwords: http://www.linux.com/archive/feed/56066

Another possibility is to edit your /etc/sudoers and set your user to be able to use sudo without password. if you check the file it will be explained in the comments there.

Upvotes: 1

freethinker
freethinker

Reputation: 1306

You can add that to your sudoers file

username ALL = NOPASSWD: /sbin/init

where username is the username you want to allow.

Upvotes: 0

Caleb
Caleb

Reputation: 5428

Change the timeout for sudo to something long enough to cover your usage, then run something like sudo echo to authenticate, then run your other commands. As long as the password timeout hasn't been reached, it should execute without asking again.

Upvotes: 0

Related Questions