Bash - Dump script works when running manually but not by crontab [duplicate] - linux

This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 3 months ago.
For security purposes, I have been writing a script that daily create dump, and manage them for some daily dumps, and some monthly dumps. I have a weird issue that I can clearly understand. When I run the script above manually ./save_db.sh evrything works. But I did add this in crontab of the same user (which is not root).
cd /home/debian/script && sh save_db.sh
The script is this:
#!/bin/bash
nom_dump="$(date +%H:%M-%d%m%y)"
mysqldump -u debian --all-databases | gzip -c > /home/debian/bdd_dump/journalier/"$nom_dump".sql.gz
sauv_mensuelle="$(date +%d)"
if [ $sauv_mensuelle == "01" ]
then
cp "$nom_dump".sql.gz /home/debian/bdd_dump/mensuel
compte_sauv="$(ls /home/debian/bdd_dump/mensuel | wc -l)"
if (( $compte_sauv > 6 ))
then
clean_mensuel="$(ls -t /home/debian/bdd_dump/mensuel/ | tail -1)"
rm /home/debian/bdd_dump/mensuel/"$clean_mensuel"
fi
fi
compte_semaine="$(ls /home/debian/bdd_dump/journalier/ | wc -l)"
if (( $compte_semaine > 7 ))
then
clean_daily="$(ls -t /home/debian/bdd_dump/journalier/ | tail -1)"
rm /home/debian/bdd_dump/journalier/"$clean_daily"
fi
When crontab runs it, the last part is not working as intended, but I can't know why. Dumps older than 7 days are not being removed
Thanks :)

The way you are calling this script:
cd /home/debian/script && sh save_db.sh
Is environment dependent. Chmod +x your script and replace the call in crontab with
/path/to/script/my-script-name.sh
You only need one shell process to run, not two, and you don't need to change your working directory to invoke it.

Related

How to run script multiple times and after every execution of command to wait until the device is ready to execute again?

I have this bash script:
#!/bin/bash
rm /etc/stress.txt
cat /dev/smd10 | tee /etc/stress.txt &
for ((i=0; i< 1000; i++))
do
echo -e "\nRun number: $i\n"
#wait untill module restart and bee ready for next restart
dmesg | grep ERROR
echo -e 'AT+CFUN=1,1\r\n' > /dev/smd10
echo -e "\nADB device booted successfully\n"
done
I want to restart module 1000 times using this script.
Module is like android device witch has linux inside it. But I use Windows.
AT+CFUN=1,1 - reset
When I push script, after every restart I need a command which will wait module and start up again and execute script 1000 times. Then I do pull in .txt file and save all output content.
Which command should I use?
I try commands like wait, sleep, watch, adb wait-for-device, ps aux | grep... Nothing works.
Can someone help me with this?
I find the solution. This is how my script actually looks:
#!/bin/bash
cat /dev/smd10 &
TEST=$(cat /etc/output.txt)
RESTART_TIMES=1000
if [[ $TEST != $RESTART_TIMES ]]
then
echo $((TEST+1)) > /etc/output.txt
dmesg
echo -e 'AT+CFUN=1,1\r\n' > /dev/smd10
fi
These are the steps that you need to do:
adb push /path/to/your/script /etc/init.d
cd /etc
cat outputfile.txt - make an output file and write inside file 0 ( echo 0 > output.txt )
cd init.d
ls - you should see rc5.d
cd .. then cd rc5.d - go inside
ln -s ../init.d/yourscript.sh S99yourscript.sh
ls - you should see S99yourscript.sh
cd .. return to init.d directory
chmod +x yourscript.sh - add permision to your script
./yourscript.sh

Pipe echo of change the current directory to sh does not work [duplicate]

This question already has answers here:
Why can't I change directories using "cd" in a script?
(33 answers)
Closed 4 years ago.
If I do the code:
echo "printf 'working'" | sh
the code prints out working
but when I want to change the current directory this way:
echo "cd ../" | sh
the current directory isn't changed.
Do you know the reason behind that behavior?
do you know how to echo cd command to sh in a working way?
echo "cd /" | sh
actually creates 2 new processes: echo, and sh. The sh process most probably does change the directory, but then just exits. You could test this by
echo "cd ../; touch Jimmix_was_here" | sh
ls -l ../Jimmix_was_here
which should show empty file Jimmix_was_here file, with current timestamp (if you had write permission to the parent directory; otherwise the first command would throw error.)
There's no way to change current directory of a process from within a child; after all if it was possible, it would be a security hole!
Note: this reminds me of a seemingly paradoxical fact: why /bin/cd exists?
Note 2: Try pstree | cat and find both pstree and cat--they are siblings!

Bash script multiple commands issue

I am currently working on a program that needs to boot a program automatically whenever it registers that this program is not open already. It needs superuser rights to boot.
Currently, I have a working Bash script, looking as follows:
#!/bin/bash
while true; do #Continue indefinitely
if [ $(ps aux | grep '/odroid_detection' | grep -v '<defunct>' -c) -le 4 ]; then #if less than 3 odroid_servers are active (booter opens 3 processes)
xterm -iconic -e su -c "xterm -iconic -hold /home/odroid/Documents/SUNRISE-Odroid/_odroid_detection/_odroid_detection/bin/Debug/_odroid_detection"
fi
sleep 60 #check every minute
done
The program that executes, however, is not working exactly as planned because it is executed from the root map instead of the map it is in. I therefore want to cd to the map the executable is in (~/Documents/SUNRISE-Odroid/_odroid_detection/_odroid_detection/bin/Debug) but have the same functionality as mentioned above. This is what I came up with:
#!/bin/bash
while true; do #Continue indefinitely
if [ $(ps aux | grep '/odroid_detection' | grep -v '<defunct>' -c) -le 4 ]; then #if less than 3 odroid_servers are active (booter opens 3 processes)
xterm -iconic -e "cd ../_odroid_detection/_odroid_detection/bin/Debug/ && su -c "xterm -iconic -hold -e _odroid_detection""
fi
sleep 60 #check every minute
done
This does not work, however, and I have tried many alternatives but I cannot seem to get it working.. It gives the following errors in the terminal:
xterm: Can't execvp cd ../_odroid_detection/_odroid_detection/bin/Debug && su -c xterm: No such file or directory
The xterm that gives this error opens in the map ~/Documents/SUNRISE-Odroid/Bash, and executing the cd mentioned above does work when I execute it seperately, so I do not understand why it cannot find the file or directory.
Any suggestions?
The colouring of StackOverflow made me understand one mistake that I made: the starting quote after 'su -c' gets interpreted as an ending quote of the xterm execute line. The working code is as follows:
#!/bin/bash
while true; do #Continue indefinitely
if [ $(ps aux | grep '/odroid_detection' | grep -v '<defunct>' -c) -le 2 ]; then #if less than 3 odroid_servers are active (booter opens 3 processes)
xterm -iconic -e "cd ../_odroid_detection/_odroid_detection/bin/Debug/ && su -c ./_odroid_detection"
fi
sleep 60 #check every minute
done

Shell script to run two scripts when server load is above 20

I need a script that I can run on a cron every 5 minutes that will check if server load is above 20 and if it is it will run two scripts.
#!/bin/bash
EXECUTE_ON_AVERAGE="15" # if cpu load average for last 60 secs is
# greater or equal to this value, execute script
# change it to whatever you want :-)
while true; do
if [ $(echo "$(uptime | cut -d " " -f 13 | cut -d "," -f 1) >= $EXECUTE_ON_AVERAGE" | bc) = 1 ]; then
sudo s-
./opt/tomcat-latest/shutdown.sh
./opt/tomcat-latest/startup.sh
else
echo "do nothing"
fi
sleep 60
done
I then chmod +x the file.
When I run it I get this:
./script.sh: line 10: ./opt/tomcat-latest/shutdown.sh: No such file or directory
./script.sh: line 11: ./opt/tomcat-latest/startup.sh: No such file or directory
From the looks of it, your script is trying to execute the two scripts from the current working directory into opt/tomcat-latest/ -- which doesn't exist. You should confirm the full file paths for the two shell scripts and then use that instead of the current path.
Also, I'd recommend that you create a cron to do this task. Here's some documentation about the crontab. https://www.gnu.org/software/mcron/manual/html_node/Crontab-file.html
check the permission to execute the files shutdown.sh and startup.sh
Is sudo -s not sudo s-
And I recommend to put a sleep (seconds)
sudo -s /opt/tomcat-latest/shutdown.sh
sleep 15
sudo -s /opt/tomcat-latest/startup.sh
Or better
sudo -s /opt/tomcat-latest/shutdown.sh && sudo -s /opt/tomcat-latest/startup.sh
The startup.sh will executed only if shutdown.sh was executed with success.

How to use curl in a shell script? [duplicate]

This question already has answers here:
Execute bash script from URL
(16 answers)
Difference between sh and Bash
(11 answers)
Closed 1 year ago.
I'm trying to run this shell script in order to install RVM in an Ubuntu box
#!/bin/bash
RVMHTTP="https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer"
CURLARGS="-f -s -S -k"
bash < <(curl $CURLARGS $RVMHTTP)
but I get the following error
Syntax error: Redirection unexpected
Also tested not using the variables, but same result, could you tell what I'm missing?
#!/bin/bash
CURL='/usr/bin/curl'
RVMHTTP="https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer"
CURLARGS="-f -s -S -k"
# you can store the result in a variable
raw="$($CURL $CURLARGS $RVMHTTP)"
# or you can redirect it into a file:
$CURL $CURLARGS $RVMHTTP > /tmp/rvm-installer
or:
Execute bash script from URL
url=”http://shahkrunalm.wordpress.com“
content=”$(curl -sLI “$url” | grep HTTP/1.1 | tail -1 | awk {‘print $2′})”
if [ ! -z $content ] && [ $content -eq 200 ]
then
echo “valid url”
else
echo “invalid url”
fi
Firstly, your example is looking quite correct and works well on my machine. You may go another way.
curl $CURLARGS $RVMHTTP > ./install.sh
All output now storing in ./install.sh file, which you can edit and execute.

Resources