2

I created a cron job as root with the command:

crontab -e

Then I added 0 4 24-31 * 4 /home/backupscript.sh after the last line.

The output of crontab -e looks like this:

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
0 4 24-31 * 4 /home/backupscript.sh

My script at /home/backupscript.sh looks like this:

cd "/home/backups/"
datestring="$(date +%Y-%m-%d)"
sudo /opt/bitnami/ctlscript.sh stop
sudo tar -pczvf ${datestring}.openproject-backup.tar.gz /opt/bitnami
sudo /opt/bitnami/ctlscript.sh start

It works fine when I run it with bash /home/backupscript.sh.

7
  • 3
    Does your script start with a shebang, like #!/bin/bash?
    – Arronical
    Jun 16, 2016 at 11:44
  • 6
    Today is the 16th where I live. It's not the 24th yet. Might that be the problem?
    – Jos
    Jun 16, 2016 at 11:48
  • 3
    1. you do not put scripts in /home/. You put those in "/usr/local/bin" 2, "sudo"?!?!?! Where is the password suppose to come from?
    – Rinzwind
    Jun 16, 2016 at 11:49
  • When I run #!/bin/bash /home/backupscript.sh nothing happens
    – Orlando
    Jun 16, 2016 at 11:49
  • 3
    try 0 4 24-31 * 4 bash /home/backupscript.sh, maybe the script only needs a execute flag (chmod +x /home/backupscript.sh)
    – stalet
    Jun 16, 2016 at 11:51

2 Answers 2

3

Make sure your script has set the execute flag when executing it directly or use bash as interpreter with the script as input.

To use bash as interpreter change the line to 0 4 24-31 * 4 bash /home/backupscript.sh

To set the execute flag for the script use: chmod +x /home/backupscript.sh

1

A bash script must start with a shebang, to let the launching process know it's a script that should be run in the bash shell. So the first line of your script should be #!/bin/bash. This is a good convention to get into, it is how bash scripts should be written. Your script currently works when you run bash /home/backupscript.sh because you're already telling it to run a bash shell with the bash part of that command. You could perhaps get round this by making your crontab line:

0 4 24-31 * 4 bash /home/backupscript.sh

This is not the correct way to do this, as it skips the permissions on the script, though it would work.

Most likely you don't have the correct permissions on your script, you need to set the execute bit. This is the domain of the chmod command, instructions on how to use it can be found here.

If you want anyone on your system to be able to execute this command you can run:

chmod +x /home/backupscript.sh

Though you may want to restrict access to just your user, assuming your user created the script, which would be:

chmod u+x /home/backupscript.sh

My advice is to add the shebang to the beginning of your script, and change the permissions to make it executable.

You must log in to answer this question.

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