Score:1

Why doesn't my crontab -e execute the .sh script?

cn flag

In sudo crontab -e I have this:

*/2 * * * * date >> /home/rki/test.log
*/5 * * * * sudo sh /home/rki/docker-compose-stuff/dokuwiki/backups/dokuwiki_backup.sh

*/2 * * * * date >> /home/rki/test.log works without a problem, writing every 2 minutes into the test.log file.

It just won't execute the backup.sh script which has this:

#!/bin/bash
# Remove previous backups
#sudo rm dokuwiki-backup*.tar.gz

# Set backup name
name=dokuwiki-backup_$(date '+%Y-%m-%d-%H%M%S')

# Backup stuff
/bin/tar -zcvf "$name.tar.gz" /var/lib/docker/volumes/dokuwiki_data

I can do sudo sh dokuwiki_backup.sh fine without a problem. The bash script is chmoded 0777 and chown rki:rki.

What's that I'm doing wrong?

sudodus avatar
jp flag
Tasks that need elevated permissions should be uploaded to cron with `sudo crontab -e` and should not contain sudo, so try with `*/5 * * * * /bin/sh /home/rki/docker-compose-stuff/dokuwiki/backups/dokuwiki_backup.sh`
cn flag
1. Don't put sudo in crontab 2. use absolute paths.
Grumpy ol' Bear avatar
cn flag
Is `/home/rki/docker-compose-stuff/dokuwiki/backups/dokuwiki_backup.sh ` not an absolute path?
cn flag
and never do `chmod 0777`. never use the 3rd 7. 0700 or 0750 for scripts is the best security when you know the user that executes it. @Grumpyol'Bear it is but `sh` does not have one. It is best to always use paths even it the path is inside the default for cron We do not know what your systems path is inside cron ;)
Grumpy ol' Bear avatar
cn flag
I just tested `*/5 * * * * /bin/sh /home/rki/docker-compose-stuff/dokuwiki/backups/dokuwiki_backup.sh` no dice. `*/2 * * * * date >> /home/rki/test.log` still works, still no backup.
sudodus avatar
jp flag
@Grumpyol'Bear, 1. It may even be necessary to put absolute paths inside the shellscript. The reason is that the environment used by `cron` can very bare-bone (you cannot expect it to be the same as what you get as a logged in user); 2. Did you use `sudo crontab -e` ?
Grumpy ol' Bear avatar
cn flag
Yes, always `sudo crontab -e`, I want it as root not rki. Otherwise I'd go `crontab -e` for rki cronjobs.
sudodus avatar
jp flag
1. I suggest that you put full path onto the call of `date` too; 2. It is not compatible to call the script with `/bin/sh` and at the same time have the shebang `#!/bin/bash` in its first line. Either call with the shell program or use the shebang. In the latter case the script must also be executable.
Grumpy ol' Bear avatar
cn flag
Fuck all that, I'm just gonna do it manually. Fucking shit not working...
pLumo avatar
in flag
Maybe it works just fine, but your `$name.tar.gz` is relative and you don't know where it is ?
Grumpy ol' Bear avatar
cn flag
Ah shite... @pLumo, that was it indeed. Everything in ./root/ . Fucks sake...
sudodus avatar
jp flag
Good catch @pLumo :-)
Score:1
in flag

If you add your cronjob via sudo crontab -e, it will run as root.

  • No need for sudo, use:

    */5 * * * * /bin/sh /home/rki/docker-compose-stuff/dokuwiki/backups/dokuwiki_backup.sh
    
  • The default working directory should be roots $HOME, so usually /root. You can find $name.tar.gz there. But better to use absolute paths, e.g.:

    /bin/tar -zcvf "/home/rki/Backups/$name.tar.gz" /var/lib/docker/volumes/dokuwiki_data
    
Score:-1
cn flag

To run cronjobs with full root environment use this command

*/5 * * * * su - root -c '/home/rki/docker-compose-stuff/dokuwiki/backups/dokuwiki_backup.sh'
Grumpy ol' Bear avatar
cn flag
No dice. Doesn't work.
Thomas Aichinger avatar
cn flag
Your script might have an error. Try to run it from command line.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.