LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Containers
User Name
Password
Linux - Containers This forum is for the discussion of all topics relating to Linux containers. Docker, LXC, LXD, runC, containerd, CoreOS, Kubernetes, Mesos, rkt, and all other Linux container platforms are welcome.

Notices


Reply
  Search this Thread
Old 09-23-2018, 08:55 AM   #1
DimTheo
LQ Newbie
 
Registered: Nov 2016
Posts: 18

Rep: Reputation: Disabled
bash script does not run when trying to exec into a container


i am trying to take daily backups of my mongo db

this is the script i wrote
Code:
#!/bin/sh
start_time="$(date -u +%s)"
DIR=`date +%m%d%y`
DEST=./mongobackups/$DIR
mkdir $DEST
kubectl exec -it mongodb-0 -- bash -c "mongodump -o /tmp/$DIR"
sleep 3;
kubectl cp default/mongodb-0:/tmp/$DIR $DEST
end_time="$(date -u +%s)"
elapsed="$(($end_time-$start_time))"
finished_on="$(date +%m-%d-%y/%H:%M:%S)"
echo "Backup finished on $finished_on and took $elapsed seconds to finish"
scp -r $DEST admin@node5:/var/mongodumps
echo "Backup $DEST copied to node5
"


it fails with this error


Code:
Defaulting container name to mongo.
tar: Removing leading `/' from member names
Backup finished on 09-21-18/22:52:20 and took 4 seconds to finish
Backup ./mongobackups/092118 copied to node5
mkdir: cannot create directory ./mongobackups/092118: No such file or directory
/home/admin/MONGODB-BACKUP/mongodump.sh: line 6: kubectl: command not found
/home/admin/MONGODB-BACKUP/mongodump.sh: line 8: kubectl: command not found
Backup finished on 09-21-18/23:50:04 and took 3 seconds to finish
./mongobackups/092118: No such file or directory
Backup ./mongobackups/092118 copied to node5

i have it run under cron and it fails . if i manually execute it like so ./mongodumb.sh >> datelog 2>&1 it works fine

any tips..? thanks
 
Old 09-23-2018, 09:01 AM   #2
lougavulin
Member
 
Registered: Jul 2018
Distribution: Slackware,x86_64,current
Posts: 182

Rep: Reputation: 56
Under cron, the environment is minimal. For example, PATH is not defined.

So, either you define it or all command have to be call with their access path (/usr/bin/...).
 
Old 09-23-2018, 09:04 AM   #3
DimTheo
LQ Newbie
 
Registered: Nov 2016
Posts: 18

Original Poster
Rep: Reputation: Disabled
this is my cron line

crontab -l
50 23 * * * sudo -u admin /home/admin/MONGODB-BACKUP/mongodump.sh >> /home/admin/MONGODB-BACKUP/datelog 2>&1

also tried without sudo -u and it fails
 
Old 09-23-2018, 09:09 AM   #4
lougavulin
Member
 
Registered: Jul 2018
Distribution: Slackware,x86_64,current
Posts: 182

Rep: Reputation: 56
Your cron seems good.

The issue is inside your script.

When cron launch /home/admin/MONGODB-BACKUP/mongodump.sh, PATH is NOT defined. So your script and all scripts call by it do not know where to find commands, as kubectl.

With admin, do :
Code:
echo $PATH
And in your script, add at the beginning (under #!/bin/sh) :
Code:
export PATH=<what the echo above returns>
 
Old 09-23-2018, 09:11 AM   #5
DimTheo
LQ Newbie
 
Registered: Nov 2016
Posts: 18

Original Poster
Rep: Reputation: Disabled
this

echo $PATH
/usr/local/rvm/gems/ruby-2.4.0/bin:/usr/local/rvm/gems/ruby-2.4.0@global/bin:/usr/local/rvm/rubies/ruby-2.4.0/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/usr/local/rvm/bin:/home/admin/.local/bin:/home/admin/bin
admin@prod1: ~$
 
Old 09-23-2018, 09:15 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 11,369

Rep: Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418Reputation: 3418
cron does not cd into the dir you want, so first you need to cd or use full path inside your scripts (instead of relative path). Also you may need to use mkdir with -p.
 
Old 09-23-2018, 09:45 AM   #7
DimTheo
LQ Newbie
 
Registered: Nov 2016
Posts: 18

Original Poster
Rep: Reputation: Disabled
this is my updated script now i will have to wait and see if runs tonight

Code:
[admin@node1 ~]$ cat MONGODB-BACKUPS/mongodump.sh
#!/bin/sh
export PATH=/usr/local/bin:/bin/sh
start_time="$(date -u +%s)"
DIR=`date +%m%d%y`
DEST=/home/admin/mongobackups/$DIR
mkdir -p $DEST
/usr/local/bin/kubectl exec -it mongodb-0 -- bash -c "mongodump -o /tmp/$DIR"
sleep 3;
/usr/local/bin/kubectl cp default/mongodb-0:/tmp/$DIR $DEST
end_time="$(date -u +%s)"
elapsed="$(($end_time-$start_time))"
finished_on="$(date +%m-%d-%y/%H:%M:%S)"
echo "Backup finished on $finished_on and took $elapsed seconds to finish"
scp -r $DEST admin@node5:/var/mongodumps
echo "Backup $DEST copied to node5"
[admin@node1 ~]$
 
Old 09-23-2018, 10:30 AM   #8
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 19 MATE
Posts: 6,104
Blog Entries: 2

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Why don't you throw your script at ShellCheck first? You might get some additional advice from it.

https://www.shellcheck.net/
 
Old 09-24-2018, 12:10 PM   #9
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Yawnstown, Ohio
Distribution: Mojave
Posts: 9,301
Blog Entries: 36

Rep: Reputation: Disabled
If "the script" works, stop messing with it.
sudo is interactive. Keep it out of scripts.

Quote:
Originally Posted by DimTheo View Post
this is my cron line

crontab -l
50 23 * * * sudo -u admin /home/admin/MONGODB-BACKUP/mongodump.sh >> /home/admin/MONGODB-BACKUP/datelog 2>&1

also tried without sudo -u and it fails
try
Code:
crontab -e -u admin
as root
using
Code:
50 23 * * */home/admin/MONGODB-BACKUP/mongodump.sh >> /home/admin/MONGODB-BACKUP/datelog 2>&1
there.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using bash script with PHP Exec with parameters Pekeltje Linux - Server 2 03-03-2014 05:59 AM
[SOLVED] Foreign Bash exec will not run from launcher as exe Needsashave Linux - Newbie 1 04-20-2013 04:55 PM
[SOLVED] Bash Script Freezes on exec 3</dev/ttyPIC for Serial to USB Converter idoneous Linux - Hardware 10 03-04-2013 05:26 PM
using Systme and exec to run shell script in perl program jlien Linux - Newbie 10 02-16-2012 12:06 AM
chroot: cannot run command `/bin/bash': Exec format error snakeo2 Linux - Newbie 6 09-28-2010 03:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Containers

All times are GMT -5. The time now is 10:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration