Score:1

/etc/cron.d not running job

om flag
Ror

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 [email protected]  Tue May  2 15:25:01 2023
Return-Path: <[email protected]>
Received: from myserver.com (localhost [127.0.0.1])
        by myserver.com 
        for <[email protected]>; 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: <[email protected]>
From: [email protected] (Cron Daemon)
To: [email protected]
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
    
    ...
}
Romeo Ninov avatar
in flag
Have you consider to use the old way of creating `cron` records, via `crontab` util?
Ror avatar
om flag
Ror
Edited my question with new informations
Prem avatar
cn flag
**(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.
Ror avatar
om flag
Ror
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 ]]`
Prem avatar
cn flag
**(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 !
Ror avatar
om flag
Ror
I did extract the user name from `whoami` and now it works ! Thanks !
Score:0
om flag
Ror

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.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.