Saturday, May 4, 2024
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 1492  / 2 Years ago, sun, october 23, 2022, 10:57:01

I am trying to make a bash script to clear my RAM caches.



Here's what I have so far:



#! /bin/bash

free -m
sync
sudo -s
echo 3 > /proc/sys/vm/drop_caches


But it doesn't fully work. The first three commands work perfectly, at the fourth command it exits.



Please explain.


More From » command-line

 Answers
1

sudo -s creates a login shell session. Then after you exit that it, you return to being a regular user. Then it executes the 4th line, but you aren't root at that point so it fails.



You can't simply stick a sudo in front of the echo statement since that's a built-in command.



So try this instead:



#!/bin/bash

free -m
sync
sudo sh -c "echo 3 > /proc/sys/vm/drop_caches"






From man sudo:




-s [command]
The -s (shell) option runs the shell specified by the SHELL
environment variable if it is set or the shell as specified in passwd(5).
If a command is specified, it is passed to the shell for execution.
Otherwise, an interactive shell is executed.


[#37187] Tuesday, October 25, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
horicgly

Total Points: 36
Total Questions: 126
Total Answers: 104

Location: Iceland
Member since Thu, Dec 1, 2022
1 Year ago
;