Run a cron job hourly in a set window - cron

My crontab file looks like this:
00 07-22 * * * /usr/bin/php /ab/c.php
but rather than running from 7am to 10pm, it runs at midnight as well. I want it strictly run every hour between 7 am to 10 pm.

Did you try to remove the leading 0s? They can cause you problems. Try
0 7-22 * * * /usr/bin/php /ab/c.php
Besides the 0s which sound strange to me (I never used them), the rest seems ok. Hope this help.

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

how to specify multi time in cron expression

Is it possible to have one cron expression to run at 6am and 7:30pm every day?. I understand the following will will run 6am, 6:30am, 7:00pm and 7:30pm,
0,30 6,19 * * *
How will I schedule 6am and 7:30pm in one cron?
You can put more intelligence into the command that gets run, something like:
0,30 6,19 * * * t=T$(date +%H%M) ; [ ${t} = T0600 -o ${t} = T1930 ] && payload
This will actually run the crontab command four times a day (at 6am, 6:30am, 7pm and 7:30pm) but execute the payload proper only at 6am and 7:30pm as desired.
Some people opt to put this extra intelligence into a script that gets run and simply exits if the time isn't one of the desired ones, but I tend to prefer keeping all scheduling control in the crontab file itself.

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

Using a crontab to gzip a file

I need to make a crontab to gzip a file named mh located on my desktop every 2 minutes in the same path. I tried
2 * * * * gzip home/Desktop/mh >> home/Desktop
But it is not working, any help is greatly appreciated.
There are several errors here.
The gzip command should be simply gzip home/Desktop/mh. Remove the >> and everything afterwards.
Your current cron will only run on the second minute of every hour. Instead you want */30 * * * * ... to run 30 times per hour.
Note that gzip is "destructive" in the sense that your original file (mh) will disappear after each gzip. That would be bad if some other process is trying to write to it continually...
If you want to keep the content of mh and just update mh.gz from it periodically, you want to do
*/30 * * * * gzip < /home/Desktop/mh > /home/Desktop/mh.gz

best way to reindex sphinx in ubuntu hardy

I'm running a slice of ubuntu hardy.
I've installed sphinx, and I would like to run the sphinx indexer every x minutes.
What is the best way to go about doing this?
The standard Unix approach is cron, so you could for example edit /etc/crontab and add a line like
*/5 * * * * root sphynx [whatever other options you need]
which means
'every five minutes' (for the */5 part)
of every hour (the * in position 2)
of every day of the month (the * in position 3)
of every month (the * in position 4)
of every day of the week (the final * in position 5)
Another example: '4 5 * * 6' amounts to 'at 5:04am (four minutes after five) on every Saturday (day of the week is 6).
You may need to or want to switch the user from root to, say, www-data is sphynx runs as that, and you obviously need to adjust the arguments.
Lastly, look into the directories
$ ls -1d /etc/cron.*
/etc/cron.d
/etc/cron.daily
/etc/cron.hourly
/etc/cron.monthly
/etc/cron.weekly
for examples --- other packages put their jobs there (and this mechanism is more general, and newer, than direct editing of /etc/crontab.
Here is what I do to reindex and then restart the search daemon once a day.
* * /1 * * root cd /home/sphinx && bin/indexer --all --rotate && bin/searchd --stop && bin/searchd

Resources