2

I have this in crontab.(in /etc)

I edited the file as root.

Script works fine when run standalone, but not in crontab.

@daily          root    /home/andy/bin/Backup_18_04.sh
4
  • 1
    In what crontab, exactly? user crontabs versus /etc/crontab have different field structure Commented Aug 14, 2018 at 18:24
  • 2
    Please edit your question and let us know you you added the entry. Did you use corontab -e or sudo corontab -e? Which one?
    – user68186
    Commented Aug 14, 2018 at 18:25
  • Check your environment variable assumptions in the script. For example, '~' means root's home dir, not yours, when run by root.
    – user535733
    Commented Aug 14, 2018 at 18:37
  • I have read tutorials on crontab and looked at examples. To me crontab is overly complicated. I also tried putting my script in cron.daily.
    – fixit7
    Commented Aug 14, 2018 at 23:16

1 Answer 1

3

To determine if some script in crontab have problems, we have to check the system log using the command

less -S /var/log/syslog

and looking for CRON lines and searching for related error messages.

Another way to simplify this analysis is adding an explicit log file in the cron definition, like this:

@daily root /home/andy/bin/Backup_18_04.sh >/home/andy/backup.log 2>&1

The > symbol redirects all output of the script to the file, and the 2>&1 redirects all error messages to the same already redirected output file (1 is the internal descriptor where all programs by default send normal messages, and 2 is the same for errors, so 2>&1 causes 'channel' 2 to connect to a copy of 'channel' 1). IMPORTANT: the line definition has to be ended with a newline.

I prefer using the normal CRON system configured thru crontab -e; it only seems complex because it has a lot of flexibility. For example, you could fire the script daily at 3:25am using:

#MINUTE HOUR DAYOFMONTH MONTH DAYOFWEEK COMMAND
 25     3    *          *     *         /home/andy/bin/Backup_18_04.sh >/home/andy/backup.log 2>&1

The asterisks mean "all" for each field, so 'all days of all months at any day of week at hour 3 and minute 25' cron executes the script and saves its normal and error messages if any in the file /home/andy/backup.log.

The word @daily you were using is equivalent to 0 0 * * *, daily at 0:00am.

Besides that, the username after the time indication only is used in /etc/crontab, not in scripts created with crontab -e (as the user is the creator by default).

Two other important aspects are declaring the script as executable and indicating which shell interpreter will process it. Making the script executable is easy with the following command:

chmod a+x /home/andy/Backup_18_04.sh

To indicate the shell interpreter, the very first line of the script should have the simbols pound/exclamation #! and the desired shell path (this is called a 'shebang'), for example:

 #!/bin/bash

This will cause the script to be processed with Bash shell interpreter.

For more info about crontab line format read the corresponding manual using the command:

man 5 crontab
2
  • So, if it's set to go at 3 a.m. and computer is not on, does it run the next time computer is turned on ? I added this to anacrontab, and it does run my script.@daily /home/andy/bin/Backup_18_04.sh It seems that editing anacrontab is easier for me. Does anacrontab also use#MINUTE HOUR DAYOFMONTH MONTH DAYOFWEEK COMMAND ?
    – fixit7
    Commented Aug 15, 2018 at 1:26
  • No, the common crons won't execute commands not launched in time, and anacrontab don't uses the cron format for jobs. In man anacron says that anacron executes commands only when some number of days passed after the last execution, but cron can have shorter periods, even one minute apart, and has more units for specifying time periods. In which OS you use the @daily indicator? In the man anacrontab page of Ubuntu and others says anacron only accepts "period delay ID command" job lines, where period can only be @monthly or a number.
    – Fjor
    Commented Sep 18, 2018 at 20:18

You must log in to answer this question.

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