Cron job not running shell script
So, I have made a backup.sh script that is supposed to run every hour but it doesn't. When I run it directly ./backup.sh
then it works fine.
I created the script using user "myname" and he is the owner of this file. I setup cron job as:
sudo crontab -e
0 * * * * /var/www/mysite/backup.sh
But when I run this command:
crontab -e
Then it opens a blank file. It seems when I use sudo then it opens root
cron file and when I run it without sudo then it opens myname
cron file. May be this is the issue?
Or may be cron is trying to run but there is an error? How do I check that error?
Comments
i usually do not sudo with cron jobs
do
crontab -l
and what do you see
It says
no crontab for myname
. When I run it withsudo
then it shows I have cron jobs.Powerful Core i9 VPS (aff)| List of all VirMach VPS (aff)
alright now last part before you owe me some cakes.
sudo su -
crontab -l
do you see the jobs?
Yes I do see cron job now. The cake is a lie!
Powerful Core i9 VPS (aff)| List of all VirMach VPS (aff)
Do you have a shebang in your script's start?
Also, try explicitly giving bash as interpreter;
0 * * * * /bin/bash /var/www/mysite/backup.sh
Talistech.com — ICT Consultancy and NVMe web hosting solutions.
then the jobs exists in the root cron list.
exit and get back as normal user.
You need to delete it "sudo crontab -e " and delete the lines.
Then under the normal without sudo make me a cake.
I have done this so let's see how it goes. Need to wait 33 minutes. If it doesn't work then I will try what @Talistech is saying.
Powerful Core i9 VPS (aff)| List of all VirMach VPS (aff)
VPS reviews and benchmarks |
You would have to adjust the paths for the O/S but should it not look something like this if it running as root cron ?
To check that the path is correct
whereis sh
should be listed in the PATH= statement below if it is not there add it.LES • About • Donate • Rules • Support
Looks yummy, unless you put a coin or something hard hahahahahahha
How about nails?
Powerful Core i9 VPS (aff)| List of all VirMach VPS (aff)
i will accept it if it was from vyas.
otherwise I'll use it to make a small puncture in someone else's tier.
Watch out for the walnut shells.
VPS reviews and benchmarks |
It worked. Using
crontab -e
instead ofsudo crontab -e
worked. Here's cake for @ehabPowerful Core i9 VPS (aff)| List of all VirMach VPS (aff)
glad it worked
we can all share .... lets have coffee and cake
Thanks
I've never seen a system where crontab is disabled for root, but not for ordinary user.
Most likely it works on both, but your backup script just dies when running from
root
(because wrong paths in scripts or something) before doing the backup part and you deem it as "it doesn't". Remember to disable the one "not working" so it won't one day "start to work" after you fix something in script and you get a double run script mess.btw. on Debians syslog should by default log all cron actions
sudo grep -i cron /var/log/syslog
Haven't bought a single service in VirMach Great Ryzen 2022 - 2023 Flash Sale.
https://lowendspirit.com/uploads/editor/gi/ippw0lcmqowk.png
Ok I found what the issue is. When I logged in as root and ran the shell script directly then I got this question
Are you sure you want to continue connecting (yes/no/[fingerprint])?
which means I hadn't connected to other VPS using root not even once. So cron was also probably experiencing this. Second issue was I am using keys to connect to other VPS and keys are only defined for other user and not root so I got this errorPermission denied (publickey)
. This is the reason cron was unable rsync files.Powerful Core i9 VPS (aff)| List of all VirMach VPS (aff)