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
Crontab Env Variable Subsitution issue
Re: Crontab Env Variable Subsitution issue
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.
The "sixth" field (the rest of the line) specifies the command to be run.
The future appears to be RHEL or Debian. I think I'm going Debian.
Info for USB installs on http://wiki.centos.org/HowTos/InstallFromUSBkey
CentOS 5 and 6 are deadest, do not use them.
Use the FAQ Luke
Info for USB installs on http://wiki.centos.org/HowTos/InstallFromUSBkey
CentOS 5 and 6 are deadest, do not use them.
Use the FAQ Luke
Re: Crontab Env Variable Subsitution issue
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 ?)
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 ?)
Re: Crontab Env Variable Subsitution issue
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...
The future appears to be RHEL or Debian. I think I'm going Debian.
Info for USB installs on http://wiki.centos.org/HowTos/InstallFromUSBkey
CentOS 5 and 6 are deadest, do not use them.
Use the FAQ Luke
Info for USB installs on http://wiki.centos.org/HowTos/InstallFromUSBkey
CentOS 5 and 6 are deadest, do not use them.
Use the FAQ Luke
Re: Crontab Env Variable Subsitution issue
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:
where MyZipScript contains:45 23 * * * MyZipScript
NOW=`/bin/date +"%-m-%-d"`
cd /home/admin/Documents/NODE && zip -m archive-$NOW.zip db_dump-$NOW*.json
Re: Crontab Env Variable Subsitution issue
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.
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.