Laravel artisan is not reading argument date linux string - linux

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")

Related

How can I add a timestamp to a cron job output?

I have a crontab job set up which records the speed of my network every two minutes:
*/2 * * * * /usr/local/bin/speedtest >> ~/Documents/speedtest.log
The output is saved to speedtest.log:
Testing download speed................................................................................
Download: 60.10 Mbit/s
Is there a way to add a timestamp (the format given is an example only; the actual format is irrelevant) to each entry to achieve something like this?
20200125221000: Testing download speed................................................................
20200125221042: Download: 60.10 Mbit/s
If so, how?
You can do:
*/2 * * * * echo "$(/usr/local/bin/speedtest) $(date)" >> ~/Documents/speedtest.log

Script does not run from crontab [duplicate]

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.

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

Append current date to the filename via Cron?

I've created a Cron task at my webhost to daily backup my database and I would like it to append the current date to the filename.
My Cron job looks like this
mysqldump -u username -pPassword db_name > www/db_backup/db_backup+date%d%m%y.sql
But the file I get is this: db_backup+date no file extension or date.
I've also tried this command
mysqldump -u username -pPassword db_name > www/db_backup/db_backup_'date +%d%m%y'.sql
but that doesn't even give an file output.
What is the right syntax for getting the date appended to my file??
* * * * * echo "hello" > /tmp/helloFile_$(date +\%Y\%m\%d\%H\%M\%S).txt
You just need to escape the percent signs.
Other date formats:
http://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/
You should use `` instead of '' around the command you want to execute to generate the current date for your filename.
You must escape the format and use evaluation
mysqldump -u username -pPassword db_name > www/db_backup/db_backup_`date +\%d\%m\%y`.sql
I need to create a new log file every time command is executed. So every day I should have a log like this /home/me/Logs/power_20151230.log
The crontab line that I use is this:
00 8 * * * /home/me/power.py ON >> /home/me/Logs/power\_`date +20\%y\%m\%d`
Note the underscore character must be escaped too.

Multiple Query String Items in crontab Job

I have the following cron job command:
* * * * * /usr/bin/lynx -term=vt100 http://abc.com/dir1/di2/script.php?action=add&config=xyz >/dev/null 2>&1
My PHP script does not recognize _GET['config'] and I get a "Cron Daemon" email message which seems to alert me that the crontab instruction is not correct.
If I take out the 2nd _GET var I do not get the "Cron Daemon" email.
Any thoughts or suggestions on how to define multiple query string items in a crontab job?
BTW, I tried the URL Encode char for the ampersand and that did not work either.
Try putting your url in quotes :
* * * * * /usr/bin/lynx -term=vt100 "http://abc.com/dir1/di2/script.php?action=add&config=xyz" >/dev/null 2>&1
For the little explanation, & is a special character which put the process in the background, so you have to put the url in quotes, otherwise cron try to put the first part in the background and execute the second part.

Resources