I have the following cron job in /etc/cron.d/backup
:
*/1 * * * * backupbot /home/backupbot/bin/backup-script.sh
Basically, I want the backup-script.sh
to run every minute (and the user backupbot should be executing the job).
The /home/backupbot/bin/backup-script.sh
file is owned by backupbot (and he has "x" permission on it). The file looks as follows:
#!/bin/bash
set -e
{
BACKUP_DIR=/var/app/backups
STORAGE_ACCOUNT_URL=https://myserver/backups
BACKUP_FILE=$(ls $BACKUP_DIR -t | head -1)
if [ -z "$BACKUP_FILE" ]; then
echo "There are no backups to synchronize"
exit 0
fi
azcopy login --identity
azcopy copy $BACKUP_DIR/$BACKUP_FILE $STORAGE_ACCOUNT_URL/$BACKUP_FILE
} >/tmp/cron.backup-script.$$ 2>&1
Normally, any output should be logged into /tmp/cron.backup-script.xxxx
. Such a file is never created.
The only evidence that the job is being noticed by Cron is the following output of systemctl status cron.service
:
● cron.service - Regular background program processing daemon
Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2021-04-13 09:59:08 UTC; 6h ago
Docs: man:cron(8)
Main PID: 1086 (cron)
Tasks: 1 (limit: 4915)
CGroup: /system.slice/cron.service
└─1086 /usr/sbin/cron -f
Apr 13 16:00:01 my-vm CRON[17201]: pam_unix(cron:session): session closed for user root
Apr 13 16:00:01 my-vm CRON[17198]: pam_unix(cron:session): session closed for user root
Apr 13 16:00:01 my-vm CRON[17199]: pam_unix(cron:session): session closed for user root
Apr 13 16:01:01 my-vm CRON[17402]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 13 16:01:01 my-vm CRON[17403]: (root) CMD ([ -f /etc/krb5.keytab ] && [ \( ! -f /etc/opt/omi/creds/omi.keytab \) -o \( /etc/krb5.keytab -nt /etc/opt/omi/creds/omi.keytab \) ] &&
Apr 13 16:01:01 my-vm CRON[17401]: pam_unix(cron:session): session opened for user backupbot by (uid=0)
Apr 13 16:01:01 my-vm CRON[17404]: (backupbot) CMD (/home/backupbot/bin/backup-script.sh)
Apr 13 16:01:01 my-vm CRON[17402]: pam_unix(cron:session): session closed for user root
Apr 13 16:01:01 my-vm CRON[17401]: (CRON) info (No MTA installed, discarding output)
Apr 13 16:01:01 my-vm CRON[17401]: pam_unix(cron:session): session closed for user backupbot
It mentions something about sessions for backupbot. How can I investigate further?
/var/app/backups
directory doesn't exist, I get the following log:ls: cannot access '/var/app/backups': No such file or directory There are no backups to synchronize
. Shouldn't the script exit on thels
error already? I have theset -e
...ls
does not haveset -e
enabled, and if it had, exiting the subshell would not terminate the script. The exit status of the command substitution is the exit status ofhead
, and that runs successfully.{ ...; }
. I.e., it never even starts (or terminates straight afterset -e
, which is terribly unlikely). Could you show the permissions and ownership of the file/home/backupbot/bin/backup-script.sh
?(CRON) info (No MTA installed, discarding output)
-- having a working email system, even within the same host would help a lot in debugging cron, as you'd get any errors in the emails. What options there are to do that easily (and without exposing the SMTP server to the internet...) may depend on the distribution, though. Anyway, lacking that, an alternative would be to put redirections directly the crontab command, i.e.*/1 * * * * backupbot /home/backupbot/bin/backup-script.sh > /tmp/crontest.out 2>/tmp/crontest.error
, or> file 2>&1
to put both in the same file, or something like that