0

I have a shell script with the following lines (the full script at the end of post):

CRON_BCK_CMD=/home/user/bck.sh
CRON_CONF=" */30 * *   *   *     $CRON_BCK_CMD"

but when I simply run these in shell and then echo $CRON_CONF I get

*/30 ... /home/user/bck.sh

where ... stand for all files listed from /home/user, e.g. */30 bck.sh bck-ssh.sh Documents davmail.log.../home/user/bck.sh

The script used to work on my other machine I think. How do I do what I obviously want to do?


# SETUP
# CRON_BCK_CMD sets the full path to the backup script
CRON_BCK_CMD=/home/user/bck.sh
# CRON_CONF sets the configuration line for cron, the time is set as:
# minute hour day_of_month month day_of_week
# * means any value, eg. 15 * * * * would mean backup at any hour, any day, when minute on the clock equals 15
# repetition can be managet by /, eg. */15 * * * * means backup every 15 minutes
CRON_CONF=" */30 * *   *   *     $CRON_BCK_CMD"
# SETUP END
CRON_IS_BCK_CMD=$(crontab -l 2>/dev/null | grep $CRON_BCK_CMD) || true
if [[ ! $CRON_IS_BCK_CMD ]]
then
    echo "No entry for backup found in crontab, do you want to schedule?"
    read -p "y/n (y) " REPLY
    if [[ $REPLY =~ ^[n|N]$ ]]
    then
        echo "Nothing to do, exitting..."
        exit 0
    fi
    (crontab -l ; echo "$CRON_CONF") | crontab -
else
    echo "Found crontab entry, do you want to stop schedule?"
    read -p "y/n (y) " REPLY
    if [[ $REPLY =~ ^[n|N]$ ]]
    then
        echo "Nothing to do, exitting..."
        exit 0
    fi
    crontab -l | grep -v $CRON_BCK_CMD | crontab -
fi
0

Use quotation marks to represent a variable as a string and not as the original source of the variable. Because otherwise an asterisk, if it follows a slash, will be treated as a "star convention" in the directory path specification - so that asterisk will represent all the files in the specified directory.

If you want to run a shell script via CRON, it's also recommended, for problems under certain circumstances, to run the script through a shell interpreter (/bin/bash -c /home/script.sh &) and also run it as the task in background (& at the end).

#!/bin/bash

# SETUP
# CRON_BCK_CMD sets the full path to the backup script
CRON_BCK_CMD="/home/user/bck.sh"
# CRON_CONF sets the configuration line for cron, the time is set as:
# minute hour day_of_month month day_of_week
# * means any value, eg. 15 * * * * would mean backup at any hour, any day, when minute on the clock equals 15
# repetition can be managet by /, eg. */15 * * * * means backup every 15 minutes
CRON_CONF="*/30 * * * *      /bin/bash -c $CRON_BCK_CMD &"     # change the Shell to the one used in your Linux (dash, sh, ash, ...)
# SETUP END

if crontab -l | grep "$CRON_BCK_CMD" > /dev/null; then
    echo "Found crontab entry, do you want to stop schedule?"
    read -p "y/n (y): " REPLY
    if [ "$REPLY" != "n" ]; then            # if the "ENTER" key was pressed or the "y" character was typed...
        crontab -l | grep -v "$CRON_BCK_CMD" | crontab -
    fi
else
    echo "No entry for backup found in crontab, do you want to schedule?"
    read -p "y/n (y): " REPLY
    if [ "$REPLY" != "n" ]; then            # if the "ENTER" key was pressed or the "y" character was typed...
        (crontab -l; echo "$CRON_CONF") | crontab -
    fi
fi

exit 0
| improve this answer | |

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.