1

I need to run a script as a user to backup my database every night. I added the following cron job in /etc/cron.d/backup-mysql via ansible.

0 3 * * * backup_mysql /path/backup-mysql.sh

I can see the job running in syslog :

Apr 29 03:00:01 myserver CRON[1534185]: (backup_mysql) CMD (/path/backup-mysql.sh)

However it does nothing and I don't understand why. I checked the path, run the script as the user. Everything works except when using this cron job.

The workaround I found is to replace the job with :

0 3 * * * root sudo -u backup_mysql /path/backup-mysql.sh

which works.

---------------------- EDIT 1 --------------------------

Following @Gerald Schneider advice in the comments, I checked the emails sent by cron yesterday during my tests :

From backup_mysql@myserver.com  Tue May  2 15:25:01 2023
Return-Path: <backup_mysql@myserver.com>
Received: from myserver.com (localhost [127.0.0.1])
        by myserver.com 
        for <backup_mysql@myserver.com>; Tue, 2 May 2023 15:25:01 GMT
Received: (from backup_mysql@localhost)
        by myserver.com 
        for backup_mysql; Tue, 2 May 2023 15:25:01 GMT
Date: Tue, 2 May 2023 15:25:01 GMT
Message-Id: <xxxxxxxxxxxxx.xxxxxxxxxxxxx@myserver.com>
From: root@myserver.com (Cron Daemon)
To: backup_mysql@myserver.com
Subject: Cron <backup_mysql@myserver> /path/backup-mysql.sh
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/backup_mysql>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=backup_mysql>

backup-mysql.sh: Script can only be run as the "backup_mysql" user

The script seems to be run by a different user than backup_mysql. However, I specified the job to run as this user so I've no idea why it does not work.

Here's the part of the script that check the user :

backup_owner="backup_mysql"
sanity_check () {
    # Check user running the script
    if [ "$USER" != "$backup_owner" ]; then
        error "Script can only be run as the \"$backup_owner\" user"
    fi
    
    ...
}
Ror
  • 321
  • 3
  • 16
  • Have you consider to use the old way of creating `cron` records, via `crontab` util? – Romeo Ninov May 03 '23 at 12:58
  • Edited my question with new informations – Ror May 03 '23 at 14:24
  • 1
    **(1)** You should update this line `error "Script can only be run as the \"$backup_owner\" user"` to something like this line `error "Script can only be run as the \"$backup_owner\" user [[ MY DEBUG DATA :: USER=$USER :: LOGNAME=$LOGNAME ]] "` , then check what mail you get. **(2)** It looks like USER is not set. 2 Suggestions : **(2A)** You can try using `LOGNAME` in the crontab itself **(2B)** Alternatively , you can try checking the output of `whoami` command , rather than using `$USER` variable. – Prem May 03 '23 at 14:57
  • Indeed it seems that USER is unset : `backup-mysql.sh: Script can only be run as the "backup_mysql" user [[ MY DEBUG DATA :: USER= :: LOGNAME=backup_mysql ]]` – Ror May 03 '23 at 15:53
  • 1
    **(2A)** Easier Way : It will work if you change `$USER` to `$LOGNAME` in the `if` line. **(2B)** Slightly Harder Way : It will work if you extract username from `whoami` command , rather than from Environment variable , to use in the `if` line. Either Way , your cron will work fine ! – Prem May 03 '23 at 18:49
  • I did extract the user name from `whoami` and now it works ! Thanks ! – Ror May 04 '23 at 08:29

1 Answers1

0

As suggested in the comments by @Prem, the variable $USER was not defined in my script when run from cron.d. I changed my script to have the $USER variable extracted from the whoami command and now it works.

Ror
  • 321
  • 3
  • 16