1

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?

12
  • 3
    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
    – sudodus
    Sep 27 at 9:33
  • 2
    1. Don't put sudo in crontab 2. use absolute paths.
    – Rinzwind
    Sep 27 at 9:36
  • 1
    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 ;)
    – Rinzwind
    Sep 27 at 9:59
  • 1
    Maybe it works just fine, but your $name.tar.gz is relative and you don't know where it is ?
    – pLumo
    Sep 27 at 12:13
  • 1
    Ah shite... @pLumo, that was it indeed. Everything in ./root/ . Fucks sake... Sep 27 at 12:26
1

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
    
-1

To run cronjobs with full root environment use this command

*/5 * * * * su - root -c '/home/rki/docker-compose-stuff/dokuwiki/backups/dokuwiki_backup.sh'
2
  • No dice. Doesn't work. Sep 27 at 10:28
  • Your script might have an error. Try to run it from command line. Sep 28 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.