Script does not run under cron but runs manually again - linux

I am very sorry for asking this again, but I've tried all advices. I have 2 scripts in /var/TPbackup_script/. This is the first one:
mysqldump -u root -pPASSWORD teampass > /var/TPbackups/TPbackup_$(date +"%Y-%m-%d").sql
Corresponding cronjob in /etc/crontab
20 9 * * * root sudo sh /var/TPbackup_script/TPbackup_script
This script works in crontab. All is good. The second script does not run:
s3cmd sync /var/TPbackups s3://PwdMgmt
Corresponding cronjob in /etc/crontab:
25 9 * * * root sudo sh /var/TPbackup_script/TPsyncS3_script
This one fails. If i run it manually in terminal:
sudo sh /var/TPbackup_script/TPsyncS3_script
then it works perfectly. What i tried:
1) Trying to add shebang #!/bin/sh to the beginning of the script
2) Renaming script to TPsyncS3_script.sh
3) I have added script into cron.daily and it was in the list of daily cron tasks (i see it with command run-parts --test /etc/cron.daily)
No success.
Here is my /etc/crontab file:
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
16 9 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
20 9 * * * root sudo sh /var/TPbackup_script/TPbackup_script
25 9 * * * root sudo sh /var/TPbackup_script/TPsyncS3_script.sh > /var/TPbackup_script/sync_log.txt
#
All permissions on scripts were set with sudo chmod 777.
And by the way. sync_log.txt was created after cronjob, but it's empty.
Any help is appreciated

Had the same problem. Solved this by adding the option to specify the location of the s3cfg:
--config /root/.s3cfg
e.g:
s3cmd sync --config /root/.s3cfg /var/TPbackups s3://PwdMgmt

I had a similar problem. Try to run your script using root's crontab.
do:
sudo crontab -e
Add your script and try again. It worked for me :)

Related

Docker Container: Cronjobs not starting (definity my error)

I'm using the current time to develop myself a little bit and I decided to invest some time in Docker Containers.
I created myself some small learning excercises where I am working on now.
I have an Issue with my Cronjobs. It should be a cronjob which is running every minute. Runtime of the completed scripts will be something around 40-50 seconds. Therefore I decided not to start a container every minute, I want to keep the container running and run the cronjobs inside the container.
My Issue: Cronjob is not starting.
#Dockerfile
FROM ubuntu
RUN apt-get update
RUN apt-get -yq install cron
COPY scripts/cmd.txt /home/cmd.sh
RUN chmod 744 /home/cmd.sh
COPY scripts/cron.txt /etc/cron.d/test-cron
RUN chmod 644 /etc/cron.d/test-cron
RUN crontab /etc/crontab.d/test-cron
RUN touch /var/log/cron.log
CMD cron && tail -f /var/log/cron.log
# scripts/cmd.txt
echo "test aus CMD" >> /var/log/cron.log
# scripts/cron.txt
* * * * * root /home/cmd.sh >> /var/log/cron.log 2>&1
* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1
From my understanding the /var/log/cron.log should get some content over time but nothing happens. I'm pretty sure that's me who caused this error but I'm not finding it.
Anybody an Idea what I did wrong?
Thanks and kind regards
Holger
There are a few typos in your files.
#Dockerfile
FROM ubuntu
RUN apt-get update
RUN apt-get -yq install cron
COPY scripts/cmd.txt /home/cmd.sh
RUN chmod 744 /home/cmd.sh
COPY scripts/cron.txt /etc/cron.d/test-cron
RUN chmod 644 /etc/cron.d/test-cron
RUN crontab /etc/crontab.d/test-cron # Here it is, just replace `crontab.d` by `cron.d`
RUN touch /var/log/cron.log
CMD cron && tail -f /var/log/cron.log
# scripts/cron.txt
* * * * * root /home/cmd.sh >> /var/log/cron.log 2>&1 # Remove the `root` command from this line...
* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1 # ...and also from that one

Docker Node.js Cron

Hello Everyone I just about have my entire app dockerized except my cron jobs here is my dockerFile
FROM nodesource/precise
# Update install os dep
RUN apt-get update && apt-get install -y apt-utils cron
RUN apt-get -y install pwgen python-setuptools curl git unzip vim
# Add code
RUN mkdir /var/sites
ADD /api /var/sites/api
ADD /services /var/sites/services
RUN cd /var/sites/services && npm install
RUN cd /var/sites/api && npm install
# Add crontab file in the cron directory
ADD crontab /etc/cron.d/hello-cron
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/hello-cron
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
# Run the command on container startup
CMD cron && tail -f /var/log/cron.log
my cron file
* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1
* * * * * cd /var/sites/services/ldapSync && node index.js >> 2>&1
# An empty line is required at the end of this file for a valid cron file.
if I remove the node cron job just leave the hello world it works fine but when I have the node cron in there it doesn't appear to do anything. If I go into the container and do crontab -e and add it manually it works fine.
Any ideas what I'm doing wrong?
Thanks
In second line of your cron file you are missing username in the format
So instead of
* * * * * cd /var/sites/services/ldapSync && node index.js >> 2>&1
you should have
* * * * * root cd /var/sites/services/ldapSync && node index.js >> 2>&1
For more info see this
Have a look at redmatter/cron image. It took me a while to get crond to behave.
There is an example in the test sub folder on github.
You can also refer to my answer here.

CRON chown and chgrp to a directory from root user [duplicate]

This question already has an answer here:
CRON - chown and chgrp to a directory from root user to www-data
(1 answer)
Closed 9 years ago.
I am trying to change group and owner (from root to www-data)for a directory at each 5 min interval.
So I have set a cron on root user like `
0,5 * * * * sudo /bin/chown -R www-data /var/www/pdf/ && sudo /bin/chgrp -R www-data /var/www/pdf/
But it's not working .
Kindly help me out.
Thanks in advance.
0,5 * * * * sudo /bin/chown -R www-data /var/www/pdf/ && sudo /bin/chgrp -R www-data /var/www/pdf/
First off, the chgrp is redundant, you can manage the same with the chown command itself.
So instead of doing sudo /bin/chown -R www-data /var/www/pdf/ && sudo /bin/chgrp -R www-data /var/www/pdf/, you can do sudo /bin/chown -R www-data:www-data /var/www/pdf
Next instead of 0,5 * * * * as your cron frequency, run it using */5 * * * *
Finally, instead of adding cron to a user's crontab with sudo / to systemwide cron using /etc/cron.d, add it to the root user's crontab using
sudo crontab -e
*/5 * * * * /bin/chown -R www-data:www-data /var/www/pdf/

Why does these sudo commands fail? [duplicate]

This question already has answers here:
How do I use sudo to redirect output to a location I don't have permission to write to? [closed]
(15 answers)
Closed 9 years ago.
I would like to add a crontab entry from a script as a normal user, so I use sudo to get root permissions, but fails no matter what I try.
$ sudo { crontab -u root -l; echo ' 15 9 * * * root /opt/script.sh'; } | crontab -u root
bash: syntax error near unexpected token `}'
$ sudo echo ' 15 9 * * * root /opt/script.sh' >> /etc/crontab
bash: /etc/crontab: Permission denied
$ sudo echo ok
ok
Because you are running
sudo echo .......
as "su" then writing the result to /etc/crontab with:
>> /etc/crontab
so in the moment you are writing to /etc/crontab you're not "su" anymore
In sudo echo ' 15 9 * * * root /opt/script.sh' >> /etc/crontab, sudo echo ' 15 9 * * * root /opt/script.sh' is ran first then the shell takes the output of the sudo command and appends it to /etc/crontab. Since the shell is started as a normal user and so doesn't have root privileges, the shell can't write to /etc/crontab, which only root can modify. To solve the problem one starts a subshell as root, which allows it to append to /etc/crontab. Fortunately, this has already been implemented as su -c, however since the system uses sudo, sudo has to be prepended. The fixed command is sudo sh -c "echo ' 15 9 * * * root /opt/script.sh' >> /etc/crontab"

sudo password automation is not working as expected when executing from crontab?

I have a shell script as follows.
abc.sh
echo "Password" | sudo -S /etc/init.d/mysqld status
It is working fine when I am executing directly from shell. My problem comes into picture when I am trying to execute the same as cron (crontab), it is not working. sudo -S options is not working well with crontab. Is there any other option to specify sudo password in shell script(automation)
I could try modifing the /etc/sudoers file by adding NOPASSWD option, if I have root access. But unfortunately I dont have root access to modify /etc/sudoers file. I have the sudo access only for executing certain commands.
Sudo -S seems to works on my Ubuntu 12.04:
# m h dom mon dow command
* * * * * cat /etc/shadow > /tmp/shadow.txt 2>&1
results in:
$ cat /tmp/shadow.txt
cat: /etc/shadow: Permission denied
whereas
# m h dom mon dow command
* * * * * echo 'password' | sudo -S cat /etc/shadow > /tmp/shadow.txt 2>&1
results in:
$ head /tmp/shadow.txt
[sudo] password for user: root:!:15736:0:99999:7:::
daemon:*:15453:0:99999:7:::
bin:*:15453:0:99999:7:::
...
Edit:
Here's a hack to get the above Ubuntu code to work on CentOS 6.4:
* * * * * export DISPLAY=:0 && gnome-terminal -e 'bash -c "echo password | sudo -S cat /etc/shadow > /tmp/shadow 2>&1"'

Resources