4

I tried this:

#!bin/bash
ssh [email protected]  'sudo /etc/init.d/script restart'

But I get this error:

sudo: no tty present and no askpass program specified

How can I run that script? Now when I need to run that script I do these steps:

ssh [email protected]
sudo /etc/init.d/script restart

But I don't want to manually log in to remote server all the time and then enter restart command.

Can I write local script that I could run so I would only need to enter password and it would run remote script and restart the process?

1
  • Ask your sysadmin to give you the privilege to run sudo /etc/init.d/script without a password. If you're the sysadmin, see man visudo and man sudoers Commented Sep 3, 2014 at 13:28

1 Answer 1

4

You can use -t option in ssh command to attach a pseudo-tty with your ssh command:

ssh -t -t [email protected] 'sudo /etc/init.d/script restart'

As per man ssh:

-t Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.

Sign up to request clarification or add additional context in comments.

3 Comments

Today I noticed strange thing. When I execute that command from my local machine, I see normal message that remote server restarts and everything is fine, but actually that server goes down for some reason. If I restart normally from remote server, it restarts normally. Why could this happen?..
Oh and after using remote restart command, when I use that command directly from that server, it gives this message: failed to kill 2190: No such process. So for some reason it kills process instead of restarting.
I guess that kiil command would be part of your /etc/init.d/script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.