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

  • ehabehab Content Writer

    i usually do not sudo with cron jobs

    do
    crontab -l

    and what do you see

  • lesuserlesuser OG
    edited November 2022

    @ehab said:
    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 with sudo then it shows I have cron jobs.

  • ehabehab Content Writer

    @lesuser said:

    @ehab said:

    alright now last part before you owe me some cakes.

    sudo su -
    crontab -l

    do you see the jobs?

  • @ehab said:

    @lesuser said:

    @ehab said:

    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!

    Thanked by (1)ehab
  • 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

    Thanked by (1)AlwaysSkint

    Talistech.com — ICT Consultancy and NVMe web hosting solutions.

  • ehabehab Content Writer

    @lesuser said:

    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.

  • @ehab said:

    @lesuser said:

    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.

  • vyasvyas OGRetired
    edited November 2022

    @ehab said:

    @lesuser said:

    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.

    image

    Thanked by (2)ehab FrankZ
  • FrankZFrankZ Moderator
    edited November 2022

    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.

    SHELL=/bin/bash
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=root
    
    0 * * * *  root  /var/www/mysite/backup.sh
    
    Thanked by (3)ehab lesuser IAmNix

    LES • AboutDonateRulesSupport

  • ehabehab Content Writer

    @vyas said:

    Looks yummy, unless you put a coin or something hard hahahahahahha

    Thanked by (1)vyas
  • @ehab said:

    @vyas said:

    Looks yummy, unless you put a coin or something hard hahahahahahha

    How about nails?

  • ehabehab Content Writer

    @lesuser said:

    @ehab said:

    @vyas said:

    Looks yummy, unless you put a coin or something hard hahahahahahha

    How about nails?

    i will accept it if it was from vyas.
    otherwise I'll use it to make a small puncture in someone else's tier.

    Thanked by (2)vyas lesuser
  • vyasvyas OGRetired

    @ehab said:

    @vyas said:

    Looks yummy, unless you put a coin or something hard hahahahahahha

    Watch out for the walnut shells.

    Thanked by (1)ehab
  • It worked. Using crontab -e instead of sudo crontab -e worked. Here's cake for @ehab

    Thanked by (1)ehab
  • ehabehab Content Writer

    glad it worked

    we can all share .... lets have coffee and cake

    :) Thanks

    Thanked by (1)lesuser
  • JabJab
    edited November 2022

    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

  • lesuserlesuser OG
    edited November 2022

    @Jab said:
    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

    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 error Permission denied (publickey). This is the reason cron was unable rsync files.

    Thanked by (1)IAmNix
Sign In or Register to comment.