2

I'm testing to set up some backup. I set up my crontab to run every minute. I have a disk that is mounted on machine2 that I will upload the backup to.
I'm compressing the content of folder /home/user/important to important.tar.gz and moving the tar.gz file to machine2's /mnt/backup2 folder.

cron tab entry:

* * * * * tar -czvf /home/user/important/important.tar.gz /home/user/important && 
rsync -vzhe ssh /home/user/important/*.tar* machine2:/mnt/backup2

This will not run with crontab.

I'm running the same command in terminal. Then it works:

/home/user/important/
/home/user/important/a.txt
/home/user/important/test.txt
/home/user/important/this.txt
/home/user/important/is.txt
/home/user/important/important.tar.gz
/home/user/important/important.txt
/home/user/important/test1/
important.tar.gz

sent 9.85K bytes  received 35 bytes  19.76K bytes/sec
total size is 9.75K  speedup is 0.99

And file is received on machine2 in the /mnt/backup2 folder.

Any suggestions why it doesn't work with crontab? Im running Ubuntu.

8
  • 1
    Can you describe the symptoms of "doesn't work"? May 17, 2021 at 0:29
  • Yes, the tar.gz files isent uploaded to machine2:/mnt/backup2 it is uploaded when i run the same command line outside crontab. May 17, 2021 at 0:41
  • Are you running the cron job exery minute - it appears you are, did you check the /var/log/cron.log file, and how many tar.gz files are being transfered - it appears only one. Try using the full name of the file in "/home/user/important/*.tar*" and replace "rsync -vzhe ssh" with scp since you appear to be only transferring 1 file which steps on the existing file. Or maybe just add the rhost user id to the "machine2:/mnt/backup2" piece of the command. May 17, 2021 at 1:14
  • Is there an actual newline in the crontab entry? Did you check if cron is running (systemctl status cron)?
    – Freddy
    May 17, 2021 at 1:26
  • 2
    Does this answer your question? Shell script work on terminal, not when it has run by cronjob
    – Philippos
    May 17, 2021 at 2:54

2 Answers 2

1

It's hard to know what's the real problem here, but here a couples things you could try:

  • Put this one-liner in a script, chmod -x it and use this in the crontab

This will allow you more control over whatever is happening (eg: set -x, etc).

  • Enable debugging output to see what is causing your crontab entry to not work as expected:
#!/bin/bash -x

or if you're using sh

#!/bin/sh -x

or just use set -x instead.

I'm guessing it might be a problem of permissions or shell globing, but if it doesn't get solved by being in a script, you should do this:

script.sh &> script.log

So you can clearly see what and how something is failing when being run as a cronjob...

  • Check if there any entry in crontab

This one is obvious, but since some may use the crontab file instead of doing adding it directly through the crontab:

echo "job entry" | crontab -

or the "normal" way, but require text editor to be opened, and the user copy pasting/typing their job/crontab entry manually...:

crontab

or if you prefer a method that prevent duplicated entry:

cronadd() {
if crontab -l | grep -wq -- "$@"; then
    :
else
    (crontab -l 2>/dev/null; echo "$@") | crontab -
fi
}

And you could use it like so:

cronadd "job entry here"

It should prevent adding duplicate job and you can make sure it's actually added... Always check with crontab -l though.

1

Thanks for all the good replies. I tried everything until:

"I'm guessing it might be a problem of permissions or shell globing, but if it doesn't get solved by being in a script, you should do this: script.sh &> script.log"

The problem was with permissions. I ran crontab with Sudo, this made some problems. When I added the command to a crontab ran as the user it worked.

1
  • How? How you run as the user? Can you give more details?
    – canbax
    Jun 13, 2022 at 11:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .