Crontab Env Variable Subsitution issue

General support questions
Post Reply
dkardell
Posts: 3
Joined: 2018/11/16 14:46:53

Crontab Env Variable Subsitution issue

Post by dkardell » 2018/11/16 14:49:10

I'm using a cron job to zip up files that are downloaded from a server at 15 past each hour. At 11:45pm I am running a zip to compress and archive the files with the name of the date that I store in an env variable.

The files have the form of db_dump-11-12-2018_0_31.json

The env variable is set in my .bash_profile with

NOW=`/bin/date +"%-m-%-d"`
export NOW

The crontab has the following entry, and yet the zip file is missing the "$NOW" env variable. (stores the zip as archive-.zip vs archive-11-11.zip

45 23 * * * cd /home/admin/Documents/NODE && zip -m archive-$NOW.zip db_dump-$NOW*.json

User avatar
TrevorH
Forum Moderator
Posts: 27127
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: Crontab Env Variable Subsitution issue

Post by TrevorH » 2018/11/16 14:51:37

A % is a special character for cron. Read man 5 crontab paying special attention to the paragraph that starts

The "sixth" field (the rest of the line) specifies the command to be run.
CentOS 6 will die in November 2020 - migrate sooner rather than later!
CentOS 5 has been EOL for nearly 3 years and should no longer be used for anything!
Full time Geek, part time moderator. Use the FAQ Luke

dkardell
Posts: 3
Joined: 2018/11/16 14:46:53

Re: Crontab Env Variable Subsitution issue

Post by dkardell » 2018/11/16 17:26:21

Thanks TrevorH, however I'm not using % in the cron. Its used in my .bash_profile to set the ENV variable "NOW"

The crontab should then use the $NOW to resolve to 11-11 in this example. It does on the wildcard of the zip command, just not in the file name.

(it picks up all of the 11-11*.json files, it just not use the $NOW in the name of the output file name of the zip commend ?)

User avatar
TrevorH
Forum Moderator
Posts: 27127
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: Crontab Env Variable Subsitution issue

Post by TrevorH » 2018/11/16 17:34:38

Well two things: first cron doesn't necessarily have the same environment variables set up as an interactive session does. You can tell that by setting up an entry that runs the 'env' command and pipes the output to a file. Second, if it does resolve it then your crontab entry has a % sign in it...
CentOS 6 will die in November 2020 - migrate sooner rather than later!
CentOS 5 has been EOL for nearly 3 years and should no longer be used for anything!
Full time Geek, part time moderator. Use the FAQ Luke

MartinR
Posts: 466
Joined: 2015/05/11 07:53:27
Location: UK

Re: Crontab Env Variable Subsitution issue

Post by MartinR » 2018/11/16 17:50:17

Cron doesn't run an interactive login, so won't pick up anything set in it. You can't move the date command into the cron entry due to the "%" as Trevor has pointed out. I would suggest that you keep the crontab entry simple, just an invocation of a shell script and do all the processing in the script. Something along the lines of:
45 23 * * * MyZipScript
where MyZipScript contains:
NOW=`/bin/date +"%-m-%-d"`
cd /home/admin/Documents/NODE && zip -m archive-$NOW.zip db_dump-$NOW*.json

dkardell
Posts: 3
Joined: 2018/11/16 14:46:53

Re: Crontab Env Variable Subsitution issue

Post by dkardell » 2018/11/16 19:31:11

Thanks MartinR, I'll switch to a script. However crontab does run interactive login

UPDATED: (I stand corrected, it does not run interactive, my logic was flawed and it was not picking up the $NOW at all but moving items to the zip because of the wild card - Point taken. Using a Script file instead)

as it does pick up the fact that NOW is set to the current date as set in my .bash_profile, and used correctly in the last part of the zip command, just not in the archive filename. Very odd. I'm running the crontab from my local account, not as root.

Post Reply

Return to “CentOS 6 - General Support”