I am trying to automate backup management on server on raspberry and put a regular tar triggering into a crontab. I set it up via bash scripts. First I set up the task and then use script to carry out expected actions. The problem is the script isn't executed by crontab. When I run the same command as saved in crontab (I check it out with crontab -l command to be sure it's exactly the same) it all works properly.

Here is my backup script:


function set_backup(){
    SCHEDULE="* * * * *"
    COMMAND=' /bin/bash ' #these spaces are essential for proper file formatting
    printf "$SCHEDULE " > $SCHEDULE_FILE
    #printf "$USER" >> $SCHEDULE_FILE
    printf "$COMMAND" >> $SCHEDULE_FILE
    printf "$SCRIPT_NAME\n" >> $SCHEDULE_FILE
    crontab $SCHEDULE_FILE

function perform_backup(){
    BACKUP_FILENAME=$(date +%Y-%m-%d)
    BACKUP_DIR=$(cat Configuration/PyHomeServer.conf | grep "Backup directory:" |  awk '{print $3}')

    FILES_TO_INCLUDE="Configuration/ Database/"
    if [ ! -d "$BACKUP_DIR" ]; then
        mkdir $BACKUP_DIR

function retrieve_latest_backup(){
    echo To be implemented

if [ "$#" = "0" ]; then
elif [ "$1" = "retrieve" ]; then
elif [ "$1" = "set" ]; then

This is what I see when I call crontab -l:

* * * * * /bin/bash  /home/gonczor/Documents/ServerPy/Admin/BaskupHomeServer.sh

I've temporarily got rid of passing username after reading this thread and I've also ensured the path is correct after reading this one.

  • 1) you want a backup very minute ? 2) there seems to be a missing / before home/gonczor/.... – Archemar Apr 23 '16 at 5:43
  • 1. It's just for testing. In target version I want user to be able to set own schedule. 2. Sorry, I was rewriting it from virtual machine by hand (shared clipboard refuses to work for some reason). – gonczor Apr 23 '16 at 11:09
  • 1
    You're calling the script with bash but the top of the script itself declares that sh should be used. I suggest you amend the first line of the script to be #!/bin/bash. (It won't make a difference here (yet) but it's better practice.) – roaima Apr 23 '16 at 11:22
  • Use = only for comparing strings. Use -eq for numbers, i.e. if [ $# -eq 0 ]; then.... It won't fix your issue here but it's good practice. – roaima Apr 23 '16 at 11:25
  • Quote your "$VARIABLES" when you use them. Unless you have a really good reason not to do so. Otherwise unexpected characters (including whitespace) will break your script – roaima Apr 23 '16 at 11:26

I've found an answer in "How Linux Works" by Brian Ward. I've simply messed the syntax. Deleting the passing of $USER fixed the problem. In other words the file file I am passing to crontab should have following structure:

m h dm m dw command

and not:

m h dm m dw user command

And again thank you roaima for useful tips.

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.