I want to add a cron job in my Raspberry to execute a task every five minutes.
So I do in the terminal:
crontab -e
and then add at the file:
*/1 * * * * /usr/bin/php myscript path.
The script is something very simple. just to try if it works:
<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = date("l jS \of F Y h:i:s A") . "<br>";;
fwrite($myfile, $txt);
fclose($myfile);
?>
The problem is that the date isn't updated, so the cron job doesn't work. Any idea of the problem ?
UPDATE
This what I got, when I execute crobtab -e
GNU nano 2.2.6 File: /tmp/crontab.3IXg0z/crontab
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
* * * * * /usr/bin/php /full/path/myscript.php
First, make sure the script is executable:
chmod +x /path/to/some/script.php
Second, make sure your script has an appropriate #! (or "shebang") on the very first line:
#!/usr/bin/php
Then make sure your cron job is configured correctly. The format for cron is typically m h dom mon dow command
sudo crontab -e
*/5 * * * * /path/to/some/script.php
If you wanted to run a script every 5 minutes you should add this entry.
*/5 * * * * /usr/bin/php /full/path/to/php/script.php
Make sure you have PATH variable set correctly on crontab, so it can locate your file.
You can simple put following line in top of crontab
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/path/to/newfile.txt
Try, following test
* * * * * touch /tmp/hello
Do following to redirect result
*/5 * * * * /usr/bin/php /full/path/to/php/script.php > /tmp/out.txt
Make sure your script running on command line.
/usr/bin/php /full/path/to/php/script.php
Use the -f option to execute the script:
*/5 * * * * /usr/bin/php -f /full/path/to/php/script.php
Tail logs file to see its executing every 5 min
tail -f /var/log/cron
Related
I am trying to run PHP file on path /var/www/html/rss_feed/mirror.php through cron tabs. For this I performed the below steps.
sudo crontab -e
Then I edited the file by inserting the following code
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# avt 5 a.m every week with:
# 0 5 * * 1 tar -zcf /ar/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
*/2 * * * * /usr/bin/php /var/www/html/rss_feed/nbt_times.php
#
# m h dom mon dow command
As you can see below is the line of code inserted to schedule cron tab after every 2 minutes to run a php file.
*/2 * * * * /usr/bin/php /var/www/html/rss_feed/nbt_times.php
I even tried following
2 * * * * lynx -dump http://192.168.0.232/rss_feed/mirror.php > /dev/null 2>&1
and even this
2 * * * * /your/path/to/php /var/www/html/rss_feed/nbt_times.php
and many more formats from different articles.
The php script is not running. Do I need to put the code somewhere else? Or I am on the wrong way. Can you please help me find out what is the issue.
It should work but need to get some clarifications here, file name seems to be different one time its mirror.php and another time its nbt_times.php
which php file has to run on the specific time that has to be specified. As you specified you're running /var/www/html/rss_feed/mirror.php then, then specify mirrror.php on your first command .
The 2nd and third command will run the job for 2nd minute of every hour. like (4:02,5:02,6:02, etc)
Try this crontab
*/2 * * * * /usr/bin/php /var/www/html/rss_feed/mirror.php
This will run for every 2 mins hope this works.
Try with php5-cli:
*/2 * * * * /usr/bin/php5-cli -f /var/www/html/rss_feed/nbt_times.php
Also add write to logs and see them after minute:
* * * * * /usr/bin/php /var/www/html/rss_feed/nbt_times.php >>log.log 2>>log.err
Check permissions of your php-file. Make 0777 for checking. Try create the simplest php file with one row and run it via cron.
I need to run a PHP file every 1 hour.
What I'm doing is:
sudo crontab -e
(In the editor) * 01 * * * /usr/bin/php /var/www/devicecheck.php
But somehow, it's not working. The command works on the command line. Before this, I was trying php /var/www/devicecheck.php
Any suggestions?
To execute devicecheck.php every 1 hour try the following:
Method A :: Execute the script using php from the crontab
# crontab -e
00 * * * * /usr/bin/php/var/www/devicecheck.php
Method B: Run the php script using URL from the crontab
If your php script can be invoked using an URL, you can lynx, or curl, or wget to setup your crontab as shown below.
The following script executes the php script (every hour) by calling the URL using the lynx text browser. Lynx text browser by default opens a URL in the interactive mode. However, as shown below, the -dump option in lynx command, dumps the output of the URL to the standard output.
00 * * * * lynx -dump http://www.yourwebsite.com/yourscript.php
The following script executes the php script (every 5 minutes) by calling the URL using CURL. Curl by default displays the output in the standard output. Using the “curl -o” option, you can also dump the output of your script to a temporary file as shown below.
*/5 * * * * /usr/bin/curl -o temp.txt http://www.yourwebsite.com/yourscript.php
The following script executes the php script (every 10 minutes) by calling the URL using WGET. The -q option indicates quite mode. The “-O temp.txt” indicates that the output will be send to the temporary file.
*/10 * * * * /usr/bin/wget -q -O temp.txt http://www.yourwebsite.com/yourscript.php
UPDATE::
# chmod a+x /home/username/yourscript.php
# crontab -e
00 * * * * /home/username/yourscript.php
Script should have execute permission. Give it by
chmod +x /var/www/devicecheck.php
Also check /var/log/syslog for Errors.
I got it to work with wget. You maybe have to install wget first. After it will allow you to run php scripts from a cronjob.
The syntax looks like:
* 01 * * */usr/local/bin/wget "http://localhost/devicecheck.php"
Where /usr/local/bin/wget points to the directory where wget is installed.
If you wish no output just add '-O /dev/null', like this:
* 01 * * */usr/local/bin/wget "http://localhost/devicecheck.php" -O /dev/null
You can also pass parameters in the url:
* 01 * * */usr/local/bin/wget "http://localhost/devicecheck.php?task=somevalue" -O /dev/null
command to runI am using ubuntu 12 and lamp server . I want to run a php script after every 1 hour . i have create a crontab to execute this and if i check my cron list with command crontab -l it is showing like this
# Edit this file to introduce tasks to be run by cron.
0 * * * * /usr/bin/php5 -q /var/www/cronjobs/cron1.php
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
this is my php script
0 * * * * /usr/bin/php5 -q /var/www/cronjobs/cron1.php
but it is not executing
how can i check why it is not working , please help
You can use crontab to add/remove/edit cronjobs.
Hit Alt+Ctrl+T to open terminal.
First make sure the script is executable by running:
chmod +x YOURSCRIPT
Then run the following command to add your cronjob:
crontab -e
Add your cronjob like this:
0 * * * * /usr/local/bin/php path/of/php/file
That's it!
Your can check the current user's crontab entries by running:
crontab -l
For more information about crontab run:
crontab --help
OR
man crontab
To find out what is wrong with your cron you can type the following command in your terminal:
grep -i "cron1.php" /var/log/syslog
The syslog contains all log of crons.
Try run the code /usr/bin/php5 -q /var/www/cronjobs/cron1.php on terminal to check if there are errors.
You can also redirect all errors to a file:
0 * * * * /usr/bin/php5 -q /var/www/cronjobs/cron1.php 2> /tmp/errorCron1.txt
Make Script File /etc/scripts/cron.sh with contain
cd /var/www/
/usr/bin/php cron.php
Save it then
chmod +x cron.sh
then go on /etc/crontab
15 15 * * * root /etc/scripts/cron.sh
save it
and wait
This is the description of crontab arguments
# Minute Hour Day of Month Month Day of Week Command
# (0-59) (0-23) (1-31) (1-12 or Jan-Dec) (0-6 or Sun-Sat)
0 2 12 * * /usr/bin/find
To Run your script every hour use below crontab entry.
0 */1 * * * /usr/bin/php5 -q /var/www/cronjobs/cron1.php
This way your script will start executing every hour.
I want a script file to run once every minute.. I've written this command.
* * * * * php -q /home/<username>/public_html/cron.php
But, this cronjob is not working. whenever, I try to open this file cron.php in browser, it works fine.
I'm using Linux OS. Is there a way to debug it in order to come to know the error?
If you're using Ubuntu as I am, use the full path.
* * * * * /usr/bin/php -q /home/<username>/public_html/cron.php
Have you added an empty line (new line) after your cronjob?
To debug:
Append 2>&1 to the end of your Crontab command. This will redirect the stderr output to the stdout. Then ensure you're logging the crontab's Unix command.
* * * * * php -q /home/<username>/public_html/cron.php; ls -la >>/var/log/cronrun 2>&1
This will capture anything from the Unix command.
A couple of additional hints: Write out the environment variables by issuing the command set with no parameters. And get the shell to echo each command with the set -x command. At the top of your script issue;
set
set -x
For cPanel, you may want to test curl (in case it's installed on your server):
curl --silent --compressed http://www.your-domain.com/cron.php
So it should look similar to: http://grabilla.com/0450d-93d93a32-02ab-457c-ac1c-d2883552a940.html#
You may also want to try removing the -q from your command and see if it helps.
* * * * * php /home/<username>/public_html/cron.php
*/1 * * * * /usr/bin/php -q /home//public_html/cron.php
Add the above line to the crontab file and run it . It will add a cronjob every minute
I have a file in mysite.com/url1/url2/cronjob.php which has to be run every minute. I try every combination, but can't succeed. What should I run in command line? Thank you.
In case you'd set it up in a crontab, this works:
*/1 * * * * /usr/bin/wget -O /dev/null http://example.com/cron.php
Steps your shell
$ crontab -e
* * * * * php -f /path/to/cron.php
~
~
I got confused for the first time where to add all of these and finally found.
Type the following in the linux/ubuntu terminal
crontab -e
select an editor (sometime it asks for the editor) and this to run for every minute
* * * * * /usr/bin/php path/to/cron.php &> /dev/null
The PHP interpreter.
/[path-to-php]/php -f [your-php-script]
Are you looking for help to make an UNIX cronjob?
If so, have you tried to edit /etc/crontab, and add
\1 * * * * user command
where user is either root or your name. Im not exactly sure how to access and URL, but a dirty way could involve downloading the link as a file, e.g. "wget http://url.com/page.php"