0

here was the original question How to add a crontab job to crontab using a bash script?

 #!/bin/bash
cronjob="* * * * * /path/to/command"
(crontab -u userhere -l; echo "$cronjob" ) | crontab -u userhere -

if I try to use command below bash is importing command and replacing (date +\%H_\%M-\%d.\%m.\%Y at the date and time of the import and I see imported command with fixed date stamp as..../mysql/21_18-27.05.2019_dbbackup.sql.gz

is there a way to keep date imported a formula?

cronjob="05 21 * * * /usr/bin/mysqldump -u mysqluserhere -pmysqlpassword DBname | gzip > /path/to/mysql/$(date +\%H_\%M-\%d.\%m.\%Y)_dbbackup.sql.gz" (crontab -u userhere -l; echo "$cronjob" ) | crontab -u userhere -

Many thanks

mag8891
  • 1
  • 3

2 Answers2

0

you can

a) escape the $ in the path, like this:

    /path/to/mysql/\$(date +\%H_\%M-\%d.\%m.\%Y)_dbbackup.sql.gz

this way the "$(date..." will end up in the cron tab

b) put the whole command into a shellscript(file) and then just execute that file from the cronjob

Chris
  • 200
  • 1
  • 6
0

Using single quotes instead of double around the string will prevent the shell from interpolating anything.

Minimally, switch to single quotes around the variable assignment:

cronjob='05 21 * * * /usr/bin/mysqldump -u mysqluserhere -pmysqlpassword DBname | gzip > /path/to/mysql/$(date +\%H_\%M-\%d.\%m.\%Y)_dbbackup.sql.gz'
(crontab -u userhere -l; echo "$cronjob" ) |
crontab -u userhere -

though of course, a variable you only use once is rather pointless.

(crontab -u userhere -l
 echo '05 21 * * * /usr/bin/mysqldump -u mysqluserhere -pmysqlpassword DBname | gzip > /path/to/mysql/$(date +\%H_\%M-\%d.\%m.\%Y)_dbbackup.sql.gz' ) |
crontab -u userhere -

You probably don't need the full path to mysqldump either.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • sorry don't see how to insert code in comments. perfect! thanks a lot! also probably shorter command will be (crontab -l ; echo '05 21 * * * /usr/bin/mysqldump -u mysqluserhere -pmysqlpassword DBname | gzip > /path/to/mysql/$(date +\%H_\%M-\%d.\%m.\%Y)_dbbackup.sql.gz') | crontab - – mag8891 Jun 01 '19 at 19:53
  • is there a way to insert in to the beginning of the file rather then to the end? – mag8891 Jun 01 '19 at 19:56
  • Of course; just switch the order of the commands inside the parentheses. – tripleee Jun 02 '19 at 07:35
  • Hi no what I asked was can I instruct to insert the command on the very fisrt line in file prior even the green text this file # Edit this file to introduce tasks to be run by cron. .... so it is easy to see when you open the file. At the moment it posts after all the green text in file. – mag8891 Jun 07 '19 at 17:21
  • It's not clear what you mean by green text; plain text does not have colors or other formatting facilities. `(echo x; crontab -l)` produces a file where the first line is `x` followed by the user's crontab. That sounds like exactly what you are describing; if it's not, can you find a different way to explain how it's wrong? – tripleee Jun 07 '19 at 17:31
  • https://stackoverflow.com/questions/33815600/indenting-heredocs-with-spaces has some ideas for preserving indentation in here documents, if that's what you are alluding to. Maybe also explore `printf -v variable '\t%s\n' "first line" "second line" "third line"` etc. – tripleee Jun 07 '19 at 18:39