bash script that requires su & sudo to run

Contribute code to SolydXK and make it even better.
User avatar
xendistar
Posts: 438
Joined: 08 Jun 2014 08:17
Location: Bournemouth, UK

bash script that requires su & sudo to run

Postby xendistar » 22 Sep 2018 10:58

Ok I am trying to make a script that runs 4 commands.

From the command line, three of these commands will not run as a standard or sudo user, it will only run as su, the fourth command will run as either su or sudo how can I do this. I believe if I run the script as an icon on the desktop it will present me with a cli to enter the password but if I wanted to run this as a cronjob in the middle of the night how can I do that?

I have a very basic understanding of scripting, so simple plain English answer please :roll:

User avatar
Arjen Balfoort
Site Admin
Posts: 8844
Joined: 26 Jan 2013 19:36
Location: Netherlands
Contact:

Re: bash script that requires su & sudo to run

Postby Arjen Balfoort » 22 Sep 2018 11:55

Simplest is to just copy the script (and make it executable) to either one of these directories:
/etc/cron.daily
/etc/cron.hourly
/etc/cron.monthly
/etc/cron.weekly

Code: Select all

sudo cp -v myscript.sh /etc/cron.daily/
sudo chmod +x /etc/cron.daily/myscript.sh
If you need more flexibility (setting a specific time or run on a specific day) you can add a cronjob with:

Code: Select all

sudo crontab -e
This will run the script as root.
I found this handy tutorial about cronjobs: https://help.ubuntu.com/community/CronHowto


SolydXK needs you!
Development | Testing | Translations

User avatar
xendistar
Posts: 438
Joined: 08 Jun 2014 08:17
Location: Bournemouth, UK

Re: bash script that requires su & sudo to run

Postby xendistar » 22 Sep 2018 14:53

Cheers Arjen, I will have a look at the cronjob link later.

User avatar
xendistar
Posts: 438
Joined: 08 Jun 2014 08:17
Location: Bournemouth, UK

Re: bash script that requires su & sudo to run

Postby xendistar » 23 Sep 2018 10:17

OK here is my little script, unfortunately for what ever reason it did not run. I got the basic of it here:

https://www.tecmint.com/clear-ram-memor ... -on-linux/

Code: Select all

#! /bin/bash
#Clear caches

sync; echo 1 > /proc/sys/vm/drop_caches
sleep 10
sync; echo 2 > /proc/sys/vm/drop_caches
sleep 10
sync; echo 3 > /proc/sys/vm/drop_caches
sleep 10
#Clear swap file
swapoff -a /dev/sda4 && swapon -a /dev/sda4
Here is its permission in /etc/cron.daily

Code: Select all

-rwxr-xr-x   1 root root   303 Sep 22 22:39 cleardown.sh
I also adjusted crontab (daily) to run at 05:25, rather than the 06:25 it was set with

If I try to run the script from the cli I get the following:

Code: Select all

mit@andora:~/scripts$ ./cleardown.sh
./cleardown.sh: line 4: /proc/sys/vm/drop_caches: Permission denied
./cleardown.sh: line 6: /proc/sys/vm/drop_caches: Permission denied
./cleardown.sh: line 8: /proc/sys/vm/drop_caches: Permission denied
./cleardown.sh: line 11: swapoff: command not found
But could that be because I am not running it from a folder in my home folder?

When I started this yesterday I could only run the

Code: Select all

sync; echo 1 > /proc/sys/vm/drop_caches
as root (not sudo) from the cli

User avatar
grizzler
Posts: 2040
Joined: 04 Mar 2013 15:45
Location: The Hague, NL

Re: bash script that requires su & sudo to run

Postby grizzler » 23 Sep 2018 10:43

Didn't look at anything else, but the names of scripts in /etc/cron.daily (and hourly, weekly and monthly) may only contain letters, digits, dashes and underscores. So anything.sh won't work because of the dot.

Also, don't assume the PATH variable has a useful value, so don't use (e.g.) swapoff but /sbin/swapoff
Frank

SolydX EE 64 - tracking Debian Testing

kurotsugi
Posts: 2103
Joined: 09 Jan 2014 00:17

Re: bash script that requires su & sudo to run

Postby kurotsugi » 23 Sep 2018 22:08

due to security reason, directly use echo command won't work (you can run the command from terminal but not from a script). I recall there was a trick to make it work but I forgot what was it. as an alternative you can use sysctl command. "sysctl -w vm.drop_caches=3" should give similar result. also, you only need to pick one of them since the number represent how deep you clear the memory (i.e: level 3 clears anything). if you want to go to level 3, no need to go step by step since level 3 also clears what level 1 and level 2 did.

the kernel actually will clean the memory by itself. unless you have limited ram size, you never have to clean the memory by yourself. also, tweaking the vm parameter actually considered better. you can tweak either the size, timeout, and the dirty memory ratio.

User avatar
xendistar
Posts: 438
Joined: 08 Jun 2014 08:17
Location: Bournemouth, UK

Re: bash script that requires su & sudo to run

Postby xendistar » 24 Sep 2018 21:46

Hi Kurotsugi, thanks for the information, I had already realised the various level of the cache clearance and had reduced the script to one line using level 2.

I tried to get a script to run using "sysctl", with out any luck. I did a search on the net but everything I found about clearing the memory caches all revolved around using echo. I actually found a script that runs under sudo and use "echo" Now I don't know enough about bash scripting to know if this is a good or bad thing but I know if I run it from the cli it does exactly the same as my script does (although with added commentary).

I will continue playing with this for a while and see what happenes

kurotsugi
Posts: 2103
Joined: 09 Jan 2014 00:17

Re: bash script that requires su & sudo to run

Postby kurotsugi » 25 Sep 2018 00:22

mine seems still working fine

Code: Select all

kurotsugi@kurozv7:~$ free -m 
              total        used        free      shared  buff/cache   available
Mem:           5850         979        4265          99         605        4927
Swap:             0           0           0
kurotsugi@kurozv7:~$ sudo sysctl -w vm.drop_caches=3
[sudo] password for kurotsugi: 
vm.drop_caches = 3
kurotsugi@kurozv7:~$ free -m
              total        used        free      shared  buff/cache   available
Mem:           5850         956        4565          98         328        4951
Swap:             0           0           0
this command should be executed manually. you can also put it under sysctl.conf and run "sudo sysctl -p" to execute it. "echo 3 | sudo tee /proc/sys/vm/drop_caches" this one should works too. if you want to make it better, you can add logs into it so that you could check the result.

Code: Select all

#!/system/bin/sh
# auto cache cleaner
# copyright@mycat (2014) - lol...i'm just joking. no copyright or whatsoever
# contact : kaze.noichi17@gmail.com

# v.1 (19-12-2014) - first release
# v.1.1 (23-1-2015) - tuned too droids. pipeline with "tee -a" for user feedback. edit some typos. 

#log files handling. automatically deleted if filesize greater than 2MB.
LOG=/data/local/dropcache.log
LOG1=/data/local/dropcache.log.1
log_size=" $( stat $LOG |grep Size |awk '{print $2}' )"
if [ "$log_size" -gt 1000000 ]
then
   mv $LOG $LOG1;
fi

#define parameters treshold in MB. this value is tuned for droids with 512MB ram. 
tr1=10
tr2=15
tr3=20

free_mem=" $( free -m |grep Mem|awk '{ print $4 }' )"
#sync to avoid losing data
sync;

#drop_caches test cases. add date on the log to make it easy to track back
if [ "$free_mem" -lt "$tr1" ]
then
   echo "== $( date +"%m-%d-%Y %H:%M:%S" ) ==
current free_mem = $( free -m |grep Mem|awk '{ print $4 }' )" | tee -a $LOG;
   sysctl -w vm.drop_caches=3;
   echo "we have low mem space. cache cleared stage 3
now we have $( free -m |grep Mem|awk '{ print $4 }' ) MB free_mem" | tee -a  $LOG;
elif [ "$free_mem" -lt $tr2 ]
then
   echo "== $( date +"%m-%d-%Y %H:%M:%S" ) ==
current free_mem = $( free -m |grep Mem|awk '{ print $4 }' )" | tee -a  $LOG;
   sysctl -w vm.drop_caches=2;
   echo "we have low mem space. cache cleared stage 2
now we have $( free -m |grep Mem|awk '{ print $4 }' ) MB free_mem" | tee -a  $LOG;
elif [ "$free_mem" -lt "$tr3" ]
then
   echo "== $( date +"%m-%d-%Y %H:%M:%S" ) ==
current free_mem = $( free -m |grep Mem|awk '{ print $4 }' )" | tee -a  $LOG;
   sysctl -w vm.drop_caches=1;
   echo "we have low mem space. cache cleared stage 1
now we have $( free -m |grep Mem|awk '{ print $4 }' ) MB free_mem " | tee -a  $LOG;
else echo "== $( date +"%m-%d-%Y %H:%M:%S" ) ==  current free_mem = $( free -m |grep Mem|awk '{ print $4 }' )
we have lot of memory space. cache not cleared" | tee -a  $LOG
fi;

I made this one specifically for android device. it has logs and auto selection feature (i.e: it will automatically select the level based on memory usage). you'll need to define the treshold value and change the log mechanism.


Return to “Code”

Who is online

Users browsing this forum: No registered users and 1 guest