Crontab INSECURE MODE

During backup and restore specifically, you might need to correct a permission. The requirement is only the user to have read and execute permissions to the /var/spool tab

Shell

1

2

3

$sudo chmod600/var/spool/cron/crontabs/user

You will need to reload the crontabs after a permission change.

Shell

1

2

3

$sudo touch/var/spool/cron/crontabs

Comments in Crontab

# you can add a hash in front to comment/note

Echo the Path in Crontab Scripts

Most errors are caused by path being incorrectly used when calling the script. By default only /bin and /usr/bin are available. Add this to your script that you call from crontab and look at what path shows. Add a line that tells you the path your script sees in the top of your script

Which Path for Cron Scripts?

You can always use “which ” to find the path you should use, and put the whole path in your script, if you are struggling with figuring out paths.
You can see what the actual path should be using the “which” command:

Shell

1

2

3

4

$which date

/bin/date

Echo the Entire Cron Environment in Script

Just like path problems will prevent the script, so will environment variables. Add this to your script that you call from crontab, then analyze your environment.

Shell

1

2

3

4

#!/bin/bash

/usr/bin/env>/root/allEvnVars.txt

Nested Crontab Variables

You can add variables to crontab -e and they will work. However, it will not work to use nested variables (a variable in another variable like bash scripting).

Last Line of Cron Job Doesn’t Run

Cron requires all commands to be terminated with a new line. Manually add a new line in your script or make sure the dos2unix has been run on your script and then view it again in vim to make sure line breaks and new lines are as expected.

Shell

1

2

3

4

$dos2unix somescript.sh

$catsomescript.sh

Crontab Errors in Formatting

Cron doesn’t like special characters, and one prime example is the % sign. You must escape the % if you use it. This will happen if you try to use auto generated dates without escaping them. The correct format is: