How to configure crontab to run ec2-automate-backup.sh script - amazon-ec2

Hi guys am trying to automate the backup of snapshots for my ec2 volumes on Amazon. I am following the ec2-automate-backup script by Collin Johnson
If run the command on command line it is creating the snapshot (working):
ubuntu#linuxserver:/usr/local/ec2/scripts$ sudo ./ec2-automate-backup.sh -s tag -t "Backup,Values=true" -c ./cron-primer.sh -r "eu-west-1"
For testing purposes if i create a crontab its not working
0 10 * * * ubuntu /usr/local/ec2/scripts/ec2-automate-backup.sh -s tag -t "Backup,Values=true" -c /usr/local/ec2/scripts/cron-primer.sh -r "eu-west-1"
Where is my problem here am running the script on ubuntu 14.04 - Amazon?

In crontab file, to execute a shell script you can use one of the following approach:
1. Call the shell script direcly, i.e.
0 10 * * * /path/to/script.sh
where the script.sh should be made executable.
2. Execute the script by sh utility, i.e.
0 10 * * * sh /path/to/script.sh
here the script.sh need not be made executable.
Now, if in your case, you need to go to a specific path and then execute script, then :
Either provide the full path of the script in crontab file directly, or
Enclose the execution commands in other shell file, and execute the enclosing file from cron.

There are two possibilites:
You need root access to run the script. You can solve this by modifying root's crontab:
sudo crontab -e
See How to run a cron job using the sudo command
You need to be in the same directory as the script to execute it
0 10 * * * ubuntu cd /usr/local/ec2/scripts && ./ec2-automate-backup.sh -s tag -t "Backup,Values=true" -c ./cron-primer.sh -r "eu-west-1"
See What is the 'working directory' when cron executes a job

Related

Some commands not working in the script running from the crontab -e

I'm running a script from crontab in which I want to set the symbolic link for npx. It does some other things which are dependent on the npx command itself. Its running the script as expected on the giving time interval, but its giving me no result for command which npx or whereis npx. When I try to run the script from terminal directly these commands does generate the correct path.
Note that, crontab I'm using is under the root user privilege, i.e set with sudo crontab -e and verified with echoing whoami inside the script which generate 'root')
By default, crontab will run your cron jobs using sh which might be the reason you are getting no results.
Try to explicitly change the shell to your default shell by adjusting the crontab entry:
*/30 * * * * /bin/bash -c "/my_script.sh"
In this case I changed it to bash, you can change it to your desired shell.

Cron and Bash script: impossible to execute

I've made a bash script that execute a docker command to dump a MySQL database:
dump_db.sh
#!/bin/bash
time=$( date +%Y%m%d%H%M )
currdir=$( pwd )
cat $currdir/container_list | while read container; do
echo "" | docker exec -i $container mysqldump -u <user> -p<password> <dbname> > $currdir/$container-$time.sql
done
If i try to run the script manually, all works fine, but if i use cron, the script does not execute.
My crontab is:
PATH=<same environment>
26 17 * * * /bin/sh /path/to/script/dump_db.sh
as you can see, i've tried also to export PATH (and checked it via env command in the crontab), but nothing.
I've also tried with these lines
26 17 * * * /bin/bash /path/to/script/dump_db.sh
26 17 * * * /path/to/script/dump_db.sh
Furthermore, also a simple bash script, like this:
#!/bin/bash
touch test_touch.txt
does not work, while the simple touch command via cron it is ok.
Where am i wrong?
Kindly check if the script has execution permission for other user.
When you're running manually you're running with the user you're logged in.
But when its running from Cron its another user. Sot that user has to have the permission to execute that script.
You may check this post for more help

docker exec is not working in cron

I have pretty simple command which is working fine standalone as a command or bash script but not when I put it in crontab
40 05 * * * bash /root/scripts/direct.sh >> /root/cron.log
which has following line
PATH=$PATH:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
SHELL=/bin/sh PATH=/bin:/sbin:/usr/bin:/usr/sbin:/root/
# Mongo Backup
docker exec -it mongodb mongodump -d meteor -o /dump/
I tried to change the url of script to /usr/bin/scirpts/ no luck
I even tried to run script directly in cron
26 08 * * * docker exec -it mongodb mongodump -d meteor -o /dump/ >> /root/cron.log
with no luck, any help appreciated.
EDIT
I don't see any errors in /root/cron.log file either
Your docker exec command says it needs "pseudo terminal and runs in interactive mode" (-it flags) while cron doesn't attach to any TTYs.
Try changing your docker exec command to this and see if that works?
docker exec mongodb mongodump -d meteor -o /dump/
for what it's worth I had this exact same problem. Fixing your PATH, changing permissions, and making sure you are running as the appropriate docker user are all good things, but that's not enough. It's going to continue failing because you are using "docker exec -it", which tells docker to use an interactive shell. Change it to "docker exec -t" and it'll work fine. There will be no log output anywhere telling you this, however. Enjoy!
cron debugging
1. /var/log or sendmail
As crond work as a daemon, without ability of failing, execution is more important than logging. Then by default, if something goes wrong, cron will send a mail to $USER#localhost reporting script output and errors.
Have a look at /var/mail or /var/spool/mail for some mails, maybe
and at /etc/aliases to see where root's mail are sents.
2. crond and $PATH
When you run a command by cron, have care that $PATH is user's default path and not root default path (ie no */sbin and other reserved path to super user tools).
For this, the simplier way is to print your default path in the environment where everything run fine:
echo $PATH
or patch your script from command line:
sed -e "2aPATH='$PATH'" -i /root/scripts/direct.sh
This will add current $PATH initializer at line 2 in your script.
Or this, will whipe from your script all other PATH=:
sed -e "s/PATH=[^ ]*\( \|$\)/\1/;2aPATH='$PATH'" -i /root/scripts/direct.sh
3. Force logging
Add at top of your script:
exec 1>/tmp/cronlog-$$.log
exec 2>/tmp/cronlog-$$.err
Try this:
sed -e '1a\\nexec 1>/tmp/cronlog-$$.log\nexec 2>/tmp/cronlog-$$.err' -i ~/scripts/direct.sh
Finalized script could look like:
#!/bin/bash
# uncomment two following lines to force log to /tmp
# exec 1>/tmp/cronlog-$$.log
# exec 2>/tmp/cronlog-$$.err
PATH='....' # copied from terminal console!
docker exec -it mongodb mongodump -d meteor -o /dump/
Executable flag
If you run your script by
40 05 * * * bash /root/scripts/direct.sh
no executable flag are required, but you must add them:
chmod +x ~/scripts/direct.sh
if you want to run:
40 05 * * * /root/scripts/direct.sh
1) Make sure this task is in the root user's crontab - it's probably the case but you didn't write it explicitly
2) cron may be unable to find bash. I would remove it and call directly your script after making it executable:
chmod 755 /root/scripts/direct.sh
and then set your crontab entry as 40 05 * * * /root/scripts/direct.sh 2>&1 >> /root/cron.log
If it's still not working, then you should have some useful output in /root/cron.log
Are you sure your script is running? Add an other command like touch /tmp/cronok before the docker exec call.
Don't forget that the crontab needs a newline at the end. Use crontab -e to edit it.
Restart the cron service and check the logs (grep -i cron /var/log/syslog).
If your OS is redhat/centos/fedora, you should try with the username (root) between the frequency and the command.
Check your mails with the mail command.
Check the crontab permissions. chmod 644 /etc/crontab.
Maybe you just don't want to reinvent the wheel.
Here's a few things I'd change-- first, capture STDERR along with STDOUT and remove the shell specification in cron-- use #! in your script instead.
40 05 * * * /root/scripts/direct.sh &>> /root/cron.log
Next, you are setting your PATH in the reverse order, and you are missing your shbang. I have no idea why you are defining SHELL as /bin/sh, when you are running bash, instead of dash. Change your script to this.
#!/usr/bin/env bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/root
PATH=$PATH:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
# Mongo Backup
docker exec -it mongodb mongodump -d meteor -o /dump/
See if that yields something better to work with.

Running a Shell Script as a Cronjob

I have a written a shell script to automate a build process.
The script checksout some code from an SVN repo, compiles and builds the code before extracting the built binary files and storing these in a central location.
I can manually execute the script ./autobuild.sh and it runs perfectly. There are a few sudo commands executed throughout the script, but I echo the password through for the first sudo command and the password holds for the entire time:
echo mypassword! | sudo -S make clean
When I add executing the script as a crontab it fails to complete all the tasks. I've tried to add it as a cronjob for the normal and root users.
Running crontab -e on my normal user account, I want the script to run at ten past midnight every day:
10 0 * * * /home/username/autobuild.sh
Also running a 32-but Cent OS 7 install with all the latest updates installed.
Can anyone provide any suggestions as to why it might work manually but not when run through a cron?
Try this
10 0 * * * /bin/bash /home/username/autobuild.sh
Step 1. find bash path
:~# whereis bash
Output
bash: /usr/bin/bash
step 2. create sh file add the line on top replace with your bash path
#!/usr/bin/bash
step 3. Make the script executable with command chmod +x .
step 4. add cron like this in for every minute to test
crontab -e
*/1 * * * * /usr/bin/bash ~/backup.sh >>test.log

Crontab cannot execute openstack's commands

I'm using Crontab execute CLI in Openstack, I create a shell script for execute all command.
My shell work when using ./ to execute in root
./myfile.sh
But:
when I using crontab it not working
Command of Crontab: in root
crontab -e
*/1 * * * * ./myfile.sh
all commands in myfile.sh is working, BUT
Openstack CLI's Commands is not working
eg:in myfile.sh
1 echo "abc" > abc.txt
2 nova keypair-add --pub-key ~/.ssh/id_rsa.pub key_1
3 echo "def" > def.txt
4 nova floating-ip-create ext_net
1 & 3 is work well, but
2 & 4 is not working
If you are using OpenStack you probably should have an openrc.sh file avilable that you can retrieve from your openstack dashboard. This script needs to be active to be able to use those commands, and that is an issue with crontab, it won't have this script active even if you run it in rc.local. If you have the rc file available in your shell do the following steps.
Open your shell script
paste the following command on top of the script: source /Path/nameOfRC.sh
Thats it
I have had the exact same issue with my script, and this solution worked for me.

Resources