Script does not run from crontab [duplicate] - linux

This question already has an answer here:
How is % (percent sign) special in crontab?
(1 answer)
Closed 4 years ago.
I'm writing files out to a log ran by a bash script using cron. The call on cron looks like this:
*/25 * * * * bash script.sh > "/var/log/$(date +%Y-%m-%d_%H:%M).log"
But when I check the crontab it records as
*/25 * * * * bash script.sh > "/var/log/$(date +).log"
And it never writes the log file. Is there something I need to change to get cron to write the date?

It is a matter of escaping variables:
* * * * * /usr/bin/touch /tmp/$(date +\%Y:\%m).log
# ^ ^
worked to me.
From man 5 crontab:
Percent-signs (%) in the command, unless escaped with backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.
So
*/25 * * * * /bin/bash script.sh > "/var/log/$(date +\%Y-\%m-\%d_\%H:\%M).log"
# ^ ^ ^ ^ ^
should work.
Note I used /bin/bash instead of just bash.

Related

Searching for end removing crontab jobs

I want to check to see if the line exists with a script:
*/2 * * * * ls -1 /var/www/html/shared-media > /var/www/html/files.txt
I also need a way to remove that SPECIFIC line in the same script.

Crontab schedule issue

I have several scripts that are run every 3 minutes and schedule looks like this:
*/3 * * * * /some/script1.php
*/3 * * * * /some/script2.php
*/3 * * * * /some/script3.php
I suppose that these scripts run at the same time, but I wish that these scripts run every 3 minute but not in the same time. Tell me please how can I reach this.
You can't reach that with the lines you are having, those will allways be running at the same times. However, you can simply create one "master" script that gets called via cron and then calls the scripts one after the other.
*/3 * * * * /usr/bin/php /some/masterscript.php
masterscript.php:
<?php
exec('/usr/bin/php /some/script1.php');
exec('/usr/bin/php /some/script2.php');
exec('/usr/bin/php /some/script3.php');
?>
EDIT:
Depending on your server's setup - install node.js. There's a cron package you can set for every second. Maybe this can help..
You can but not with that syntax, instead you should use this kind of syntax:
1,4,7,10,13,16,etc... * * * * /some/script1.php
2,5,8,11,14,17,etc... * * * * /some/script2.php
and so on....
If you just want each script to execute in turn, with the second not starting until the first has finished, and so forth, just put them all in a single cron command.
cron invokes each command by passing the command string to /bin/sh -- and the shell can very easily invoke several commands in sequence.
*/3 * * * * /some/script1.php ; /some/script2.php ; /some/script3.php
do you think a couple of seconds between scripts run could be enough ?
what about a command like this ?
*/3 * * * * echo "<?php echo 'Start ...';sleep(2);echo Go; ?>"|php /some/script1.php
*/3 * * * * echo "<?php echo 'Start ...';sleep(4);echo Go; ?>"|php /some/script2.php
*/3 * * * * echo "<?php echo 'Start ...';sleep(6);echo Go; ?>"|php /some/script3.php
You could also substitute fixed waiting time with random waiting time.
Instead of sleep(2) try a generic sleep(rand(1,10)).
I hope this could be useful

Laravel artisan is not reading argument date linux string

I have written a custom command that has 1 argument which is the current time (in string format)
artisan check-banners $(date +%X)
The argument is then passed to the fire method and I can successfully insert the current time into the database. However... when i try to cronjob this custom command it does not work. see code below:
* * * * * php /Applications/AMPPS/www/laravel/artisan check-banners $(date +%X)
I have tried insert 'foo'instead of $(date +%X) as an argument and it successfully inserts into the database.
Why can't I insert this $(date +%X) through the cronjob?? But I can manually type it via artisan check-banner $(date +%X) and it works
Many thanks.
Percentage signs have special meaning in crontab and need to be escaped, like so:
* * * * * php /Applications/AMPPS/www/laravel/artisan check-banners $(date +\%X)
Also, I'm not sure, but you may need to wrap the +%X argument in quotes as well:
* * * * * php /Applications/AMPPS/www/laravel/artisan check-banners $(date "+\%X")

Backup database use crontab with date function [duplicate]

This question already has answers here:
Is there a special restriction on commands executed by cron? [duplicate]
(2 answers)
Closed 4 years ago.
I can use this command
mysqldump -u"root" myDB| gzip > mydb_`date +%d-%m-%Y`.sql.gz
but when run in crontab
* * * * * mysqldump -u"root" myDB| gzip > mydb_`date +%d-%m-%Y`.sql.gz
( this error cause by function date, when i remove it , crontab run good )
on ubuntu, it happen this error in log file.
ubuntu CRON[xxxx] (user) CMD(mysqldump -u"root" myDB| gzip > mydb_`date+)
ubuntu CRON[xxxx] (CRON) error ( grandchild #5353 failed with exit status 2)
ubuntu CRON[xxxx] (CRON) info (no MTA installed, discarding output)
% signs in a crontab command are converted to newlines, and all data after the first % is sent to the command's stdin. Replace each % with \%.
(And you only had 4 time fields: * * * *; you need 5 (you later fixed the question).)
From the man 5 crontab:
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 crontab file.
Percent-signs (%) in the
command, unless escaped with backslash (), will be changed into
newline characters, and all data after the first % will be sent to the
command as standard input. There is no way to split a single command
line onto multiple lines, like the shell's trailing "\".

Is there a special restriction on commands executed by cron? [duplicate]

This question already has an answer here:
How is % (percent sign) special in crontab?
(1 answer)
Closed 4 years ago.
I have a crontab that looks like
0 0 * * * pg_dump DB_NAME > /path/to/dumps/`date +%Y%m%d`.dmp
which works fine when I run it manually, but not when cron runs it. After digging through the logs, I see
Dec 12 00:00:01 localhost crond[17638]: (postgres) CMD (pg_dump DB_NAME > /path/to/dumps/`date +)
It looks like a problem with percent signs, but the man page doesn't even contain the percent character at all, so I thought they were alright.
You have to escape percent signs with a backslash:
0 0 * * * pg_dump DB_NAME > /path/to/dumps/`date +\%Y\%m\%d`.dmp
From man 5 crontab:
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 crontab file. Percent-signs (%) in the
command,
unless escaped with backslash (\), will be changed into newline
characters, and all data after the first % will be sent to the command
as
standard input. There is no way to split a single command line
onto
multiple lines, like the shell’s trailing "\".
There's another characteristic problem that can affect programs run by cron as compared with the command line (other than the interpretation of '%' signs described by Robert Gamble).
That difference is in the environment. If the program run relies on special environment variables, then it will work when you run it from the command line, with the environment you normally use, and it will likely work if you run it with at because that captures the environment when you create the job. But cron does no special environment setting.
I habitually, therefore, configure cron to run scripts by absolute pathname, and that script does the environment setting that I need (adds my $HOME/bin directory to PATH, for example). I even have a standardized infrastructure for this - a shell script that sets the environment and runs other programs.
# #(#)$Id: crontab,v 4.2 2007/09/17 02:41:00 jleffler Exp $
# Crontab file for Home Directory for Jonathan Leffler (JL)
#-----------------------------------------------------------------------------
#Min Hour Day Month Weekday Command
#-----------------------------------------------------------------------------
0 * * * * /usr/bin/ksh /work1/jleffler/bin/Cron/hourly
1 1 * * * /usr/bin/ksh /work1/jleffler/bin/Cron/daily
23 1 * * 1-5 /usr/bin/ksh /work1/jleffler/bin/Cron/weekday
2 3 * * 0 /usr/bin/ksh /work1/jleffler/bin/Cron/weekly
21 3 1 * * /usr/bin/ksh /work1/jleffler/bin/Cron/monthly
The script in /work1/jleffler/bin/Cron sets the environment and then runs the script of the same name in /work1/jleffler/bin to do the real work. The names in the Cron sub-directory are actually all links to the same script.

Resources