Crontab script su root / user -c does not execute - shell

if been trouble-checking for hours and can't find out why my shell script won't execute properly when using a root crontab.
I'm on a vServer eqipped with
Ubuntu 14.04.4 LTS
3.13.0-042stab113.11.
my script is a chmod 711 file:
/usr/local/sbin/bckup_script
and looks like this:
#!/bin/bash
DATE=`date +%Y-%m-%d_%H_%M_%S`
su - -c "chgrp postgres /backup/db"
su - -c "chmod 770 /backup/db"
su - -c "chown user /backup/db"
su - postgres -c "pg_dump db_name > /backup/db/${DATE}db_name.sql && pg_dumpall > /backup/db/${DATE}_all_db.out"
su - -c "rsync -a /home/user/value /backup/"
The crontab is started using
crontab -e
as
root
user
The crontab executes as far as I can tell from syslog.
When executed as root user (no crontab), the file will do what it's told to. Also my PATH is set properly and working.
I have no idea what am doing wrong.
Solution:
Thx to Jay jargot I found out what was wrong. To complete the question, here are the outputs you "asked" for:
crontab -l
#m h dom mon dow command
* * * * * bckup_script
Output of crontab was
/bin/sh: bckup_script: command not found
which lead me to the conclusion to use the absolute Path to the file which solved the problem.
my crontab -l now looks like follows and everything works like a charm!
# m h dom mon dow command
49 20 * * 1-5 /usr/local/sbin/bckup_script
Thx very much!

Related

Bash Script with docker commands not running via Crontab

hey guys not sure what I am doing wrong here, but was hoping for some help.
I have a bash script with the following.
#!/bin/bash
docker exec -t wekan-db bash -c "scripts/wekandb_backup.sh"
docker exec -t wekan-db bash -c "rm -r /dump/*; cp -r /mongodb_backup/ /dump/mongodb_backup"
docker cp wekan-db:/dump /home/ikadmin/codes/backup/wekan/$(date +%Y-%m-%d)
Everything executes correctly when I run the bash script from the terminal.
However when I try to run it via crontab -e it does not work. Logs do show crontab trying to run it.
Just in case the bash script is currently set as 777 as well.
Any help would be appreciated
EDIT: crontab command
19 8 * * * /bin/bash /home/ikadmin/codes/scripts/backup-wekan-docker.sh

Execute a "docker exec -it" command within in a ubuntu cron job

For a daily database backup, I created the following cron job :
File : crontab -e
SHELL=/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin
* * * * * /bin/bash /var/path/deploy/database/scripts/backup.sh
File : /var/path/deploy/database/scripts/backup.sh
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
cp -r /var/path/deploy/database/scripts /var/bricoolpathpostgresql/data
chmod 755 -R /var/path/postgresql/data
docker exec -it database /var/lib/postgresql/data/scripts/pg_backup_rotated.sh
When I execute the script directly it works well and the backup is created successfully. But the script is being excuted from the cron job the command docker exec -it database /var/lib/postgresql/data/scripts/pg_backup_rotated.sh does not seem to work.
I have no error output in /var/log/syslog
as Danny Ebbers mentioned in comment, the problem ws the -i argument in Docker command.

Cronjob not redirecting output

I have created one test.sh shell script which I have scheduled using crontab -e to execute after every 1 minutes and redirecting output to a file.
test.sh
echo "Printing all Environment Var"
env
echo "Bye Bye"
Below is how my crontab look like
#crontab
0,1,2,3,4,5,6,7,8,9,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59 * * * * sudo su - admiir -c /u01/users/admiir/test.sh > /u01/app/iir/InformaticaIR/iirlog/crontab_launchsh.log
When I run ls -ltr the timestamp is getting updated but nothing is getting printed in the output file.
To run the cron every minute and to save to a file the current environment used this could be used:
* * * * * env > ~/cronenv
Next, you can start a shell like it will be run within cron by doing:
env - `cat ~/cronenv` /bin/sh
Here you could try something like:
su - admiir -c "/u01/users/admiir/test.sh > /u01/app/iir/InformaticaIR/iirlog/crontab_launchsh.log"
You can omit the sudo su and only use su
Once your script is working you could then update your cron with:
* * * * * su - admiir -c "/path/to/test.sh > /path/to/out.txt"
You could also run the cron as the specific user by doing:
sudo crontab -u username -e

sh script works manually but not via cron

I'm trying to run a sh script from crontab. If I run script manually it works perfect, but when I run it in crontab, I get errors.
The script:
#!/bin/bash
sudo tar -zcvf /var/www/nextcloud/data/nextcloud/files/backup.tar.gz /home/beno/stuff/
sudo -u www-data /usr/bin/php /var/www/nextcloud/occ file:scan --all >> /var/www/nextcloud/data/nextcloud/files/backup_log.txt
The script is supposed to make a tar backup of a folder and put it in nextcloud folder and run command files:scan, so nextcloud rescans filesystem and starts synchronization...as I read it here:
https://doc.owncloud.org/server/9.0/admin_manual/configuration_server/occ_command.html#file-operations-label
When crontab runs the script, backup.tar.gz is created, then I get following error:
An unhandled exception has been thrown:
Doctrine\DBAL\DBALException: Failed to connect to the database: An exception occured in driver: SQLSTATE[HY000] [2002] No such file or directory in /var/www/nextcloud/lib/private/DB/Connection.php:60
I'm using ubuntu16 and nextcloud11. Please help!
Following you'r request
Instead of using you'r own crontab, you have to use specic user's crontab, by sudo your crontab command:
sudo -s
then
crontab -e
now you coul add the root's 1st line:
tar -zcvf /var/www/nextcloud/data/nextcloud/files/backup.tar.gz /home/beno/stuff/
and once finished
exit
sudo -s www-data
... and so on...
or (if you could'nt run a shell under www-data):
sudo www-data crontab <<<'01 2 * * * /usr/bin/php /var/www...'
Note: This will overwrite www-data's crontab by this only line!
To prevent this, you could:
sudo www-data crontab -l
to see what contain actual crontab, then
sudo www-data crontab -l |
sed -e '$a 01 2 * * * /usr/bin/php /var/www...'
For adding you'r line, and finally
sudo www-data crontab -l |
sed -e '$a 01 2 * * * /usr/bin/php /var/www...' |
sudo www-data crontab
For replacing actual crontab by modified one.
But it could be simplier to
sudo vi /etc/cron.d/mybackups
Then add you'r rule by following time spec by user name:
21 22 * * * root tar -zcvf /var/w...
22 23 * * * www-data /usr/bin/php /var/www...
Further doc
see man -P'less +/SYSTEM' 5 crontab

Crontab execute the command but the ruby script is not running

I've set a crontab for my ruby script.
crontab -l
12 10 * * * /bin/bash -c 'cd /home/user/path/ && /home/user/.rvm/rubies/ruby-1.9.2-p136/bin/ruby -rubygems script.rb'
the command /bin/bash -c 'cd /home/user/path/ && /home/user/.rvm/rubies/ruby-1.9.2-p136/bin/ruby -rubygems script.rb' runs well.
And in cron, cron is executing the command (I checked the syslog:
sudo tail /var/log/syslog
(user) CMD (/bin/bash -c 'cd /home/user/path/ && /home/user/.rvm/rubies/ruby-1.9.2-p136/bin/ruby -rubygems script.rb')
)
But the script is not executing exactly. I'm not getting the expected out put from it.
What may be the cause of the issue? It'll be grate if some one can help me.
Try:
12 10 * * * /bin/bash -c 'source /home/user/.rvm/scripts/rvm && cd /home/user/path/ && /home/user/.rvm/bin/ruby-1.9.2-p136 -rubygems script.rb'
If you are using .rvmrc file in your project to setup ruby version for you project. Then you need to tell rvm to trust rvmrc file in ~/.rvmrc
rvm_trust_rvmrcs_flag=1
this will disable prompt and your cron will not hang.
Try running env -i $SHELL --norc in terminal and then run the ruby script. The terminal will have the same environment as the cron

Resources