-1

I was not able to find the answer for this myself.

As cron uses /bin/sh, im getting Syntax error's. I have a cron job, which uses the following command in it

2,17,32,47 * * * * tar -czvf /opt/SOMEFILES /var/lib/$(date +"%Y/%m/%d")

OR

2,17,32,47 * * * * tar -czvf /opt/$(date +"%Y/%m/%d")/somefiles /var/lib/$(date +"%Y/%m/%d")

Basically, this is just an example, the real cron job is a little different. Tried to give some type of an example.

$(date +"%Y/%m/%d") This is the part i am not sure how to use in Cron.

How could i use it in cron? the curdate could even be a folder where it goes.

TheSebM8
  • 459

2 Answers2

4

I don't have the exact errors you are getting, but my guess would be this is an escaped characters issue (you are using forward slashes rather than back slashes for date)

The "sixth" field (the rest of the line) specifies the command to be run. The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the cronfile. Percent-signs (%) in the command, unless escaped with backslash (), will be changed into newline charac- ters, and all data after the first % will be sent to the command as standard input.

Try more like this format

`date +\%Y\%m\%d\`
  • The thing is, i also need to use this as a folder field. I have a program that generates data and makes folders as 2018/04/05 and sometimes i have to move inside the curdate folder, so i cant use \ – TheSebM8 Apr 06 '18 at 07:56
  • Nvm, thanks. this also helped. Just my bad understanding was the reason i failed to get it working at first. – TheSebM8 Apr 06 '18 at 08:09
2

Solution 1: Put backslash \ with date

2,17,32,47 * * * * tar -czvf /opt/SOMEFILES >> /var/lib/`date +\%Y\%m\%d\`

Solution 2:

You can also put your commands into a shell file and then execute the shell file with cron.

jobs.sh

 tar -czvf /opt/SOMEFILES >> /var/lib/`date +\%Y\%m\%d\` 

cron

2,17,32,47 * * * * jobs.sh