문제

I had been trying to clear cache in a remote server and i got these commands.

First login as root user and execute

sync; echo 3 > /proc/sys/vm/drop_caches

But I had to automate this in a script, so i used this

ssh user@ipaddress "sudo su; sync; echo 3 > /proc/sys/vm/drop_caches";

But I am not able to get the root user privileges by sudo su and I thought removing sudo su and instead use

ssh user@ipaddress "sudo sync;sudo echo 3 > /proc/sys/vm/drop_caches";

But this says it dose'nt have enough permissions.

What am I missing??

도움이 되었습니까?

해결책

When you do this sudo echo 3 > .... only echo will be with "sudo" user permissions, redirection is with current user.

try something like this :

    ssh user@ipaddress "sudo sh -c \"sync; echo 3 > /proc/sys/vm/drop_caches\"";

다른 팁

Use tee as an alternate to redirection that works well with sudo:

ssh user@ipaddress 'echo 3 | sudo tee /proc/sys/vm/drop_caches > /dev/null'

The redirection to /dev/null is optional, if you want to avoid "3" being echoed to your terminal as well.

Also If you experience this message

>> sudo: sorry, you must have a tty to run sudo

as I had, you can fix it by editing /etc/sudoers and comment Defaults requiretty -> #Defaults requiretty.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top