sh script works manually but not via cron - shell

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

Related

Operation not permitted while trying to run script through crontab - Mac

I am trying to run a MongoDB backup script in the crontab (MacOS Catalina 10.15.6)
*/2 * * * * cd ~/Documents/MongoDB/hourly/ && source hourly_m_backup.sh >/tmp/stdout1.log 2>/tmp/stderr1.log
I am getting the error
/bin/sh: hourly_m_backup.sh: Operation not permitted
The details for the files:
-rwxrwxrwx# 1 Sethu staff 686 11 Sep 19:38 hourly_m_backup.sh
I went through a lot of StackOverflow posts. I couldn't get the script to run through crontab. But if I execute the script independently from the terminal, it's working. I even deleted the file and created a new file. Still, I am getting errors from CronTab. Not sure what to do anymore.
------------ Update --------------
I tried running with sudo command
*/1 * * * * echo "####" | sudo -S cd ~/Documents/MongoDB/hourly/ && echo "####" | sudo -S source hourly_m_backup.sh >>/tmp/stdout1.log 2>>/tmp/stderr1.log
I am getting the error
sudo: source: command not found
-----------Update ----------
I tried the solution from this post, and I implemented this solution
*/1 * * * * echo "####" | source <(sudo -S cat ~/Documents/MongoDB/hourly/hourly_m_backup.sh) >>/tmp/stdout1.log 2>>/tmp/stderr1.log
I am getting this error in the "mail"
/bin/sh: -c: line 0: syntax error near unexpected token (' /bin/sh: -c: line 0: echo "####" | source <(sudo -S cat ~/Documents/MongoDB/hourly/hourly_m_backup.sh) >>/tmp/stdout1.log
2>>/tmp/stderr1.log'
The solution is a bummer. I have to provide Full Disk Access to CRON in Mac.

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.

Bash shell script not executing command docker entrypoint

I have a shell script, it is not executing the "crontab" command when it is run from docker CMD. But works when I execute the file inside the docker container. What am I missing?
File entry.sh:
#!/bin/sh
echo "Starting cron"
/usr/sbin/cron -f -L 15
crontab /etc/cron.d/cron-python
Docker file:
FROM python:3
RUN apt-get update && apt-get -y install cron
COPY ./cron/entry.sh /entry.sh
COPY ./cron/crontab.txt /etc/cron.d/cron-python
RUN chmod +x /entry.sh
RUN chmod 0644 /etc/cron.d/cron-python
CMD ["./entry.sh"]
cron-python:
# crontab -e
*/1 * * * * python /app/myscript.py >> /var/log/script.log 2>&1
Following should work :
#!/bin/sh
# start cron
echo "Starting cron"
crontab /etc/cron.d/cron-python
/usr/sbin/cron -f -L 15
In your version, /etc/cron.d/cron-python does not get installed as you asked /usr/sbin/cron to run on foreground.
Also you need to copy /app/myscript.py.

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

Crontab script su root / user -c does not execute

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!

Resources