Cronjob run same php script many times every minute - cron

Is it possible to have a cronjob to run every minute the same php file many times?
For example i am not allowed to add these crons because they are the same:
php /manytimes_samescript.php
php /manytimes_samescript.php
php /manytimes_samescript.php
php /manytimes_samescript.php
.
.
.
php /manytimes_samescript.php
How can i make the manytimes_samescript.php to run many times every minute by using cron only?

2 possible options:
Put many crontab lines with the same configuration
Write a small shell script which launches many PHP processes with your script

CMD in crontab will run the job every minute. The first star (asterisk) is the minutes column. A * there will run the command every minute. A */5 there would run the job every 5 minutes.
I got this info here: http://www.thegeekstuff.com/2009/06/15-practical-crontab-examples/
Very helpful site.

Related

Run script daily with cron and create compressed log file

Im trying to run PHP script on this location on every 15 minutes
0,5,10,15 * * * * /usr/local/bin/php /var/www/html/adform.php
and to put results of the script in specific folder in specific file and to compress it daily. Is it possible and how? Pls help.
This is possible.
Instead of running /usr/local/bin/php every 15 minutes, why not replace that call with a shell script which does everything you described?
This shell script would run your php script, pipe the output to a file somewhere on your system which you can then compress.

How to stop minute cron job?

I have a program written in python,my program scrapes a value from some financial website every minute and pushes that value into my DB.My program takes like 1 or maximum 1.5 seconds to do this job. I have set a cron job to call my program every minute. I need to run my program in this way everyday from 09AM to 04PM. Now sometimes I may have to stop my program to kill the program at any time between 09AM to 4PM. How can I do this?
According to this link
I tried ps -o pid,sess,cmd afx | grep -A20 "cron$" and I was unable to find my program in the list since it completes it's work in seconds.
Referring to this I tried /etc/init.d/cron stop and pkill cron which kills all cron jobs which I don't want. I am running this cron in ubuntu linux .Any help with this would be appreciated.
Modify the program so it runs only if a particular file exists. Remove the file if you need to stop the program. (Or have it run only if the file doesn't exist, and touch the file to stop the program.)
If you're not able to modify the program, you can execute a shell if statement as a cron command.
simply rename the script name, so it will not be executed
Is it feasible to include a date time check within the program? And it won't run if before 9AM or after 4PM?
The following should also work:
* 9-15 * * * script.sh
0 16 * * * script.sh
To switch: perhaps have it read from a config/detect the presence of a file (acting as a flag) to determine whether or not to run.

cron jobs set for different times, but execute at the same time.

I have a file which I loaded into crontab with the following jobs:
# script1 executes at 12:30 daily
# script2 executes at 12:35 daily
30 12 * * * /usr/bin/wget -q -O temp.txt http://<host-url>/cronjob/script1.php
35 12 * * * /usr/bin/wget -q -O temp.txt http://<host-url>/cronjob/script2.php
I followed the rules as per this site:
How to Set-up a Cron Job
I initially had an issue with having both jobs display in the crontab list. I resolved that issue by having all jobs set up on one line as detailed in the section of:
Dealing with Error Messages from Crontab
Once I had both jobs listed correctly, I tested to verify that they executed correctly. At 12:31 I noted that both scripts executed.
Why did this occur? How can I schedule it so that script2 executes at it's own scheduled time?
I realize this is not such a huge issue but I am curious to know.
Thank you for your assistance.
I opened the cron job to be edited using:
crontab -e
And made edits using Nano.
I added a new line character between jobs and this fixed the problem.
So the file initially created was done on a Windows machine which was FTP to the server. I checked out the file and it was created in Notepad++ without an extension, also tried with an extension of .TXT. The EOL character was set to UNIX.
No matter the scenario, when loading the file, both jobs executed on the first scheduled time. After I modified cron jobs using the edit above. The cron jobs executed at the scheduled time (aka 5 minutes apart).

BASH - Most efficient method of setting this script as a timed/routine event?

I've recently started a simple project, just to help me learn the BASH scripting language a little better. Anyway, this script is set to alternate/rotate the user's desktop background/wallpaper at a given interval.
Given that this task would most likely be done every 30 minutes to 1 hour, how should I go about doing this. Would 30 minute/1 hour timers be very inefficient? Or, could cronjobs do a better job for me?
Also, how could I get this script to run in the background, so that a terminal window is not always required to be open?
Could you provide some sort of an idea into the syntax, if you can, as well.
This would be a suitable job for cron. cron would take care of invoking the script at regular intervals. You would not then have to be concerned in your script when the script should run and managing a script running in the background.
Running in the background would be extravagent as the script does not need to do much - not much more than change the current desktop setting. Typically the script would only take a small fraction of a second to complete the task.
cron entries have six fields-:
mins hours day month day-of-week path_to_command
0-59 0-23 1-31 1-12 0-6 command
days of the week start on Sunday. 0=Sunday, 1=Monday etc.
cron entry to run the script every hour for all days and months-:
0 * * * * /path/change_wallpaper.sh
to list your current cron jobs, type
crontab -l
Edit your cron jobs and add the new cron entry-:
crontab -e
Check the new setting is in place -:
crontab -l
I would personally run the script using following crontab:
0 * * * * $HOME/changewallpaper.sh
which you can install as a user with this command
crontab -e
Other solutions include running daemon script from file ~/.xprofile
For more information please refer to
man crontab
man 5 crontab
Also check out this project Variety.
Also, how could I get this script to run in the background, so that a terminal window is not always required to be open?
That would be a daemon. And there's no need to write your own. It's a bit tedious in bash if you want pidfile, start|stop|restart etc. Just add a new cronjob which'll execute your script every n minute or something.
Edit your cronjobs
crontab -e
Execute script every 30 min: (not the same as 30, which would do it every hh:30!)
*/30 * * * * /path/to/your/script
Restart cron. How depends on distro, here's Ubuntu:
service cron restart
List cronjobs:
crontab -l

How to execute a php script every day [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Cron job on Ubuntu for php
I am running and ubuntu server and wanted to run a php script every day. I have done some research and found that cron is the best way of doing this however, this is where i got stuck, a lot of the information on the internet about cron is very hard to follow and understand.
So i wanted to execute a simple php script once a day, the script i made for testing simply just deletes a record from a database, but the real script will do a lot more.
I tried setting up a task through plesk which is provided through my web host service but it didn't seem to execute when i wanted it to, i used 1 for minutes, 22 for hours, * for day, * for week and * for month and thought this would execute every day at 22:01.
I have the directories on my server:
cron.hourly
cron.daily
cron.weekly
cron.monthly
I thought i could dump i file in there and it would execute for example every day, but i'm guessing i need to make a cron script to call a php script right?
If i were to go the way of putting a file in the cron.daily folder how would i go about it?
Also if there are any steps i need to take on the php side please let me know?
Thanks a lot for your time.
There's couple of ways to setup cron job. Assuming you got shell access you could do crontab -e from console and define job there, i.e. like this:
1 22 * * * command
which would trigger command (whatever it is) at 22:01 each day (not sure why you set minutes to 1 instead of 0 though). To launch PHP script from there you would either have to install php-cli, and then invoke it that way:
1 22 * * * <path>/php -q script.php
You can also call bash script here, to setup all the stuff like paths etc and then call your php script form bash - sometimes it is simpler to do that way instead of crafting way too long command line for cron. And it's simpler to update it later. also, you could turn your php script into bash-runnable script by setting it execution bit (chmod a+x script.php) and adding shell's shebang:
#!/usr/bin/php -q
<?php
...
If your script got too many dependencies and you'd prefer to call it via web, you could use wget to mimic a browser. so your command would be:
/usr/bin/wget --delete-after --quiet --spider <URL-TO-YOUR-SCRIPT>
wget manual can be accessed by man wget or wget -h, or is on this website. Aternatively you may use HEAD tool from perl-www package - but it requires perl while wget is a standalone tool. If you use HTTPS with self signed certs, add --no-check-certificate to your invocation arguments. And you may also want to setup .htaccess and limit web access to your cron script to localhost/127.0.0.1
every minute:
* * * * * /path/script.php
every 24hours (every midnight):
0 0 * * * /path/script.php
Se this reference for how crontab works: http://adminschoice.com/crontab-quick-reference, and this handy tool to build cron jobx: http://www.htmlbasix.com/crontab.shtml

Resources