How can I execute `date` inside of a cron tab job?
Clash Royale CLAN TAG#URR8PPP
up vote
86
down vote
favorite
I want to create a log file for a cron script that has the current hour in the log file name. This is the command I tried to use:
0 * * * * echo hello >> ~/cron-logs/hourly/test`date "+%d"`.log
Unfortunately I get this message when that runs:
/bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
/bin/sh: -c: line 1: syntax error: unexpected end of file
I have tried escaping the date
part in various ways, but without much luck. Is it possible to make this happen in-line in a crontab file or do I need to create a shell script to do this?
cron quoting command-substitution
add a comment |Â
up vote
86
down vote
favorite
I want to create a log file for a cron script that has the current hour in the log file name. This is the command I tried to use:
0 * * * * echo hello >> ~/cron-logs/hourly/test`date "+%d"`.log
Unfortunately I get this message when that runs:
/bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
/bin/sh: -c: line 1: syntax error: unexpected end of file
I have tried escaping the date
part in various ways, but without much luck. Is it possible to make this happen in-line in a crontab file or do I need to create a shell script to do this?
cron quoting command-substitution
add a comment |Â
up vote
86
down vote
favorite
up vote
86
down vote
favorite
I want to create a log file for a cron script that has the current hour in the log file name. This is the command I tried to use:
0 * * * * echo hello >> ~/cron-logs/hourly/test`date "+%d"`.log
Unfortunately I get this message when that runs:
/bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
/bin/sh: -c: line 1: syntax error: unexpected end of file
I have tried escaping the date
part in various ways, but without much luck. Is it possible to make this happen in-line in a crontab file or do I need to create a shell script to do this?
cron quoting command-substitution
I want to create a log file for a cron script that has the current hour in the log file name. This is the command I tried to use:
0 * * * * echo hello >> ~/cron-logs/hourly/test`date "+%d"`.log
Unfortunately I get this message when that runs:
/bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
/bin/sh: -c: line 1: syntax error: unexpected end of file
I have tried escaping the date
part in various ways, but without much luck. Is it possible to make this happen in-line in a crontab file or do I need to create a shell script to do this?
cron quoting command-substitution
edited Jan 20 '12 at 23:00
Gilles
506k12010031529
506k12010031529
asked Jan 20 '12 at 17:12
cwd
12.7k52114153
12.7k52114153
add a comment |Â
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
134
down vote
accepted
Short answer:
Try this:
0 * * * * echo hello >> ~/cron-logs/hourly/test`date "+%d"`.log
Note the backslash escaping the %
sign.
Long answer:
The error message suggests that the shell which executes your command doesn't see the second back tick character:
/bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
This is also confirmed by the second error message your received when you tried one of the other answers:
/bin/sh: -c: line 0: unexpected EOF while looking for matching `)'
The crontab manpage confirms that the command is read only up to the first unescaped %
sign:
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.
awesome - thanks so much! +1
â cwd
Jan 20 '12 at 19:58
Sorry for my ignorance, but where do you see this error messages? When I do 'grep CRON /var/log/syslog' I see no error messages, although cron failed - kagda.ru/i/9a016249a39_20-05-2015-09:22:47_9a01.png
â Tebe
May 20 '15 at 6:24
@ÃÂþÿðÃÂÃÂ_èþ_ÃÂ_ýðÃÂõû cron will send an email with the error message,
â Jasen
Jan 1 '16 at 6:50
1
date +%Y %m %d %H:%M:%S
-cronlog
â DevilCode
Apr 4 '16 at 13:36
add a comment |Â
up vote
6
down vote
You can also put your commands into a shell file and then execute the shell file with cron.
jobs.sh
echo hello >> ~/cron-logs/hourly/test`date "+%d"`.log
cron
0 * * * * sh jobs.sh
add a comment |Â
up vote
5
down vote
If you would like to make the date formatting string as a variable (to avoid duplicating the whole string), DO NOT escape %
and DO NOT put it in $()
For example, while declare the string, just write:
DATEVAR=date +%Y%m%d_%H%M%S
Then, write cron statement with $($VARIABLE_NAME)
like this:
* * * * * /bin/echo $($DATEVAR) >> /tmp/crontab.log
Thanks to cyberx86, her/his answer at ServerFault might be more completed:
add a comment |Â
up vote
1
down vote
In cron, you can use this simple syntax:
*/15 01-09 * * * sh /script.sh >> /home/username/cron_$(date -d"-0 days" +%Y%m%d).log 2>&1
Output date format will retrun like cron_20180123.log
â bala4rtraining
Jan 24 at 13:51
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
134
down vote
accepted
Short answer:
Try this:
0 * * * * echo hello >> ~/cron-logs/hourly/test`date "+%d"`.log
Note the backslash escaping the %
sign.
Long answer:
The error message suggests that the shell which executes your command doesn't see the second back tick character:
/bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
This is also confirmed by the second error message your received when you tried one of the other answers:
/bin/sh: -c: line 0: unexpected EOF while looking for matching `)'
The crontab manpage confirms that the command is read only up to the first unescaped %
sign:
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.
awesome - thanks so much! +1
â cwd
Jan 20 '12 at 19:58
Sorry for my ignorance, but where do you see this error messages? When I do 'grep CRON /var/log/syslog' I see no error messages, although cron failed - kagda.ru/i/9a016249a39_20-05-2015-09:22:47_9a01.png
â Tebe
May 20 '15 at 6:24
@ÃÂþÿðÃÂÃÂ_èþ_ÃÂ_ýðÃÂõû cron will send an email with the error message,
â Jasen
Jan 1 '16 at 6:50
1
date +%Y %m %d %H:%M:%S
-cronlog
â DevilCode
Apr 4 '16 at 13:36
add a comment |Â
up vote
134
down vote
accepted
Short answer:
Try this:
0 * * * * echo hello >> ~/cron-logs/hourly/test`date "+%d"`.log
Note the backslash escaping the %
sign.
Long answer:
The error message suggests that the shell which executes your command doesn't see the second back tick character:
/bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
This is also confirmed by the second error message your received when you tried one of the other answers:
/bin/sh: -c: line 0: unexpected EOF while looking for matching `)'
The crontab manpage confirms that the command is read only up to the first unescaped %
sign:
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.
awesome - thanks so much! +1
â cwd
Jan 20 '12 at 19:58
Sorry for my ignorance, but where do you see this error messages? When I do 'grep CRON /var/log/syslog' I see no error messages, although cron failed - kagda.ru/i/9a016249a39_20-05-2015-09:22:47_9a01.png
â Tebe
May 20 '15 at 6:24
@ÃÂþÿðÃÂÃÂ_èþ_ÃÂ_ýðÃÂõû cron will send an email with the error message,
â Jasen
Jan 1 '16 at 6:50
1
date +%Y %m %d %H:%M:%S
-cronlog
â DevilCode
Apr 4 '16 at 13:36
add a comment |Â
up vote
134
down vote
accepted
up vote
134
down vote
accepted
Short answer:
Try this:
0 * * * * echo hello >> ~/cron-logs/hourly/test`date "+%d"`.log
Note the backslash escaping the %
sign.
Long answer:
The error message suggests that the shell which executes your command doesn't see the second back tick character:
/bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
This is also confirmed by the second error message your received when you tried one of the other answers:
/bin/sh: -c: line 0: unexpected EOF while looking for matching `)'
The crontab manpage confirms that the command is read only up to the first unescaped %
sign:
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.
Short answer:
Try this:
0 * * * * echo hello >> ~/cron-logs/hourly/test`date "+%d"`.log
Note the backslash escaping the %
sign.
Long answer:
The error message suggests that the shell which executes your command doesn't see the second back tick character:
/bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
This is also confirmed by the second error message your received when you tried one of the other answers:
/bin/sh: -c: line 0: unexpected EOF while looking for matching `)'
The crontab manpage confirms that the command is read only up to the first unescaped %
sign:
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.
edited Jan 20 '12 at 17:44
answered Jan 20 '12 at 17:31
Adam Zalcman
2,49611413
2,49611413
awesome - thanks so much! +1
â cwd
Jan 20 '12 at 19:58
Sorry for my ignorance, but where do you see this error messages? When I do 'grep CRON /var/log/syslog' I see no error messages, although cron failed - kagda.ru/i/9a016249a39_20-05-2015-09:22:47_9a01.png
â Tebe
May 20 '15 at 6:24
@ÃÂþÿðÃÂÃÂ_èþ_ÃÂ_ýðÃÂõû cron will send an email with the error message,
â Jasen
Jan 1 '16 at 6:50
1
date +%Y %m %d %H:%M:%S
-cronlog
â DevilCode
Apr 4 '16 at 13:36
add a comment |Â
awesome - thanks so much! +1
â cwd
Jan 20 '12 at 19:58
Sorry for my ignorance, but where do you see this error messages? When I do 'grep CRON /var/log/syslog' I see no error messages, although cron failed - kagda.ru/i/9a016249a39_20-05-2015-09:22:47_9a01.png
â Tebe
May 20 '15 at 6:24
@ÃÂþÿðÃÂÃÂ_èþ_ÃÂ_ýðÃÂõû cron will send an email with the error message,
â Jasen
Jan 1 '16 at 6:50
1
date +%Y %m %d %H:%M:%S
-cronlog
â DevilCode
Apr 4 '16 at 13:36
awesome - thanks so much! +1
â cwd
Jan 20 '12 at 19:58
awesome - thanks so much! +1
â cwd
Jan 20 '12 at 19:58
Sorry for my ignorance, but where do you see this error messages? When I do 'grep CRON /var/log/syslog' I see no error messages, although cron failed - kagda.ru/i/9a016249a39_20-05-2015-09:22:47_9a01.png
â Tebe
May 20 '15 at 6:24
Sorry for my ignorance, but where do you see this error messages? When I do 'grep CRON /var/log/syslog' I see no error messages, although cron failed - kagda.ru/i/9a016249a39_20-05-2015-09:22:47_9a01.png
â Tebe
May 20 '15 at 6:24
@ÃÂþÿðÃÂÃÂ_èþ_ÃÂ_ýðÃÂõû cron will send an email with the error message,
â Jasen
Jan 1 '16 at 6:50
@ÃÂþÿðÃÂÃÂ_èþ_ÃÂ_ýðÃÂõû cron will send an email with the error message,
â Jasen
Jan 1 '16 at 6:50
1
1
date +%Y %m %d %H:%M:%S
-cronlogâ DevilCode
Apr 4 '16 at 13:36
date +%Y %m %d %H:%M:%S
-cronlogâ DevilCode
Apr 4 '16 at 13:36
add a comment |Â
up vote
6
down vote
You can also put your commands into a shell file and then execute the shell file with cron.
jobs.sh
echo hello >> ~/cron-logs/hourly/test`date "+%d"`.log
cron
0 * * * * sh jobs.sh
add a comment |Â
up vote
6
down vote
You can also put your commands into a shell file and then execute the shell file with cron.
jobs.sh
echo hello >> ~/cron-logs/hourly/test`date "+%d"`.log
cron
0 * * * * sh jobs.sh
add a comment |Â
up vote
6
down vote
up vote
6
down vote
You can also put your commands into a shell file and then execute the shell file with cron.
jobs.sh
echo hello >> ~/cron-logs/hourly/test`date "+%d"`.log
cron
0 * * * * sh jobs.sh
You can also put your commands into a shell file and then execute the shell file with cron.
jobs.sh
echo hello >> ~/cron-logs/hourly/test`date "+%d"`.log
cron
0 * * * * sh jobs.sh
answered Jul 3 '15 at 14:41
Trevi Awater
16315
16315
add a comment |Â
add a comment |Â
up vote
5
down vote
If you would like to make the date formatting string as a variable (to avoid duplicating the whole string), DO NOT escape %
and DO NOT put it in $()
For example, while declare the string, just write:
DATEVAR=date +%Y%m%d_%H%M%S
Then, write cron statement with $($VARIABLE_NAME)
like this:
* * * * * /bin/echo $($DATEVAR) >> /tmp/crontab.log
Thanks to cyberx86, her/his answer at ServerFault might be more completed:
add a comment |Â
up vote
5
down vote
If you would like to make the date formatting string as a variable (to avoid duplicating the whole string), DO NOT escape %
and DO NOT put it in $()
For example, while declare the string, just write:
DATEVAR=date +%Y%m%d_%H%M%S
Then, write cron statement with $($VARIABLE_NAME)
like this:
* * * * * /bin/echo $($DATEVAR) >> /tmp/crontab.log
Thanks to cyberx86, her/his answer at ServerFault might be more completed:
add a comment |Â
up vote
5
down vote
up vote
5
down vote
If you would like to make the date formatting string as a variable (to avoid duplicating the whole string), DO NOT escape %
and DO NOT put it in $()
For example, while declare the string, just write:
DATEVAR=date +%Y%m%d_%H%M%S
Then, write cron statement with $($VARIABLE_NAME)
like this:
* * * * * /bin/echo $($DATEVAR) >> /tmp/crontab.log
Thanks to cyberx86, her/his answer at ServerFault might be more completed:
If you would like to make the date formatting string as a variable (to avoid duplicating the whole string), DO NOT escape %
and DO NOT put it in $()
For example, while declare the string, just write:
DATEVAR=date +%Y%m%d_%H%M%S
Then, write cron statement with $($VARIABLE_NAME)
like this:
* * * * * /bin/echo $($DATEVAR) >> /tmp/crontab.log
Thanks to cyberx86, her/his answer at ServerFault might be more completed:
edited Aug 10 '17 at 10:24
Stéphane Chazelas
282k53518851
282k53518851
answered Jan 4 '16 at 8:41
Gawi - Kai
5114
5114
add a comment |Â
add a comment |Â
up vote
1
down vote
In cron, you can use this simple syntax:
*/15 01-09 * * * sh /script.sh >> /home/username/cron_$(date -d"-0 days" +%Y%m%d).log 2>&1
Output date format will retrun like cron_20180123.log
â bala4rtraining
Jan 24 at 13:51
add a comment |Â
up vote
1
down vote
In cron, you can use this simple syntax:
*/15 01-09 * * * sh /script.sh >> /home/username/cron_$(date -d"-0 days" +%Y%m%d).log 2>&1
Output date format will retrun like cron_20180123.log
â bala4rtraining
Jan 24 at 13:51
add a comment |Â
up vote
1
down vote
up vote
1
down vote
In cron, you can use this simple syntax:
*/15 01-09 * * * sh /script.sh >> /home/username/cron_$(date -d"-0 days" +%Y%m%d).log 2>&1
In cron, you can use this simple syntax:
*/15 01-09 * * * sh /script.sh >> /home/username/cron_$(date -d"-0 days" +%Y%m%d).log 2>&1
edited Jan 24 at 14:41
Kevin Lemaire
1,037421
1,037421
answered Jan 24 at 13:50
bala4rtraining
112
112
Output date format will retrun like cron_20180123.log
â bala4rtraining
Jan 24 at 13:51
add a comment |Â
Output date format will retrun like cron_20180123.log
â bala4rtraining
Jan 24 at 13:51
Output date format will retrun like cron_20180123.log
â bala4rtraining
Jan 24 at 13:51
Output date format will retrun like cron_20180123.log
â bala4rtraining
Jan 24 at 13:51
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f29578%2fhow-can-i-execute-date-inside-of-a-cron-tab-job%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password