Clear cache in remote server using ssh

Uday Reddy picture Uday Reddy · Sep 30, 2013 · Viewed 20.6k times · Source

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??

Answer

Duffydake picture Duffydake · Sep 30, 2013

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\"";