• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1051
  • Last Modified:

cron job

Hi, i have debian server .
I am new to  cron jobs and I am having some dificulty.

I want to exectute a php script daily after every 5 min.

i have php file in /var/www/test/test.php

and i have edited the crontab file in /etc/crontab

5 * * * * php /var/www/test/test.php

What is  the next step so that i can check whether its executing or not?

Please guide
Thanks


0
iilv
Asked:
iilv
  • 11
  • 5
  • 5
  • +2
1 Solution
 
woolmilkporcCommented:
Hi,
first, you should always use full paths in crontab, and you should capture or discard possible output of the script -
5 * * * * /path/to/php /var/www/test/test.php >/path/to/logfile 2>/path/to/errorlogfile
The script itself should also conatin absolute paths where applicable (calls to external programs)
Examine a) the logfiles from above to see waha happens, and b) look at /var/log/cron (or the like) to see cron's own logfile.
 
HTH
 
wmp
 
 
0
 
ozoCommented:
5 * * * *
is 5 minutes after every hour
*/5 * * * *
is every 5 minutes

see
man 5 crontab
0
 
Hugh FraserConsultantCommented:
You can have the output from the job sent to an email address by adding

MAILTO="user@somehost"

to the top of the crontab file (edit the file using crontab -e).

Also, the fields in the time spec are:
minute
hour
day of month
month
day of week

The time spect you have runs the job at 5 minutes after every hour. This may be what you want, but it's not every 5 minutes, and it's not a daily job. A job running every 5 minutes would look like:

*/5 * * * *
or
0,5,10,15,20,25,30,35,40,45,50,55 * * * *

A daily job would have an hour specified as well.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
iilvAuthor Commented:
Hi,

I have checked /var/log there is no log file for cron

Please help
Thanks
0
 
woolmilkporcCommented:
Hi,
could be in /var/log/syslog  under debian.
Search for lines containing 'cron' or 'CRON'
0
 
iilvAuthor Commented:
Hi,

I am sending you the log file for your reference.


Please guide
0
 
iilvAuthor Commented:
Hi,

This is the output of the log file:


Please help


Feb 24 01:29:01 localhost syslogd 1.5.0#5: restart.
Feb 24 01:39:01 localhost /USR/SBIN/CRON[7104]: (root) CMD (  [ -x /usr/lib/php5/maxlifetime ] && [ -d /var/lib/php5 ] && find /var/lib/php5/ -type f -cmin +$(/usr/lib/php5/maxlifetime) -print0 | xargs -n 200 -r -0 rm)
Feb 24 01:44:01 localhost /USR/SBIN/CRON[7119]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Feb 24 01:59:45 localhost -- MARK --
Feb 24 02:09:01 localhost /USR/SBIN/CRON[7130]: (root) CMD (  [ -x /usr/lib/php5/maxlifetime ] && [ -d /var/lib/php5 ] && find /var/lib/php5/ -type f -cmin +$(/usr/lib/php5/maxlifetime) -print0 | xargs -n 200 -r -0 rm)
Feb 24 02:19:44 localhost -- MARK --
Feb 24 02:39:01 localhost /USR/SBIN/CRON[7209]: (root) CMD (  [ -x /usr/lib/php5/maxlifetime ] && [ -d /var/lib/php5 ] && find /var/lib/php5/ -type f -cmin +$(/usr/lib/php5/maxlifetime) -print0 | xargs -n 200 -r -0 rm)
Feb 24 02:44:01 localhost /USR/SBIN/CRON[7230]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Feb 24 02:46:01 localhost /usr/sbin/cron[1020]: (*system*) RELOAD (/etc/crontab)
Feb 24 02:46:01 localhost cron[1020]: Error: bad minute; while reading /etc/crontab
Feb 24 02:48:01 localhost cron[1020]: Error: bad minute; while reading /etc/crontab
Feb 24 02:59:44 localhost -- MARK --
Feb 24 03:09:01 localhost /USR/SBIN/CRON[7280]: (root) CMD (  [ -x /usr/lib/php5/maxlifetime ] && [ -d /var/lib/php5 ] && find /var/lib/php5/ -type f -cmin +$(/usr/lib/php5/maxlifetime) -print0 | xargs -n 200 -r -0 rm)
Feb 24 03:14:26 localhost kernel: 4gb seg fixup, process ldconfig (pid 7339), cs:ip 73:0805d10e
Feb 24 03:14:26 localhost kernel: 4gb seg fixup, process ldconfig (pid 7339), cs:ip 73:0806d95e
Feb 24 03:14:26 localhost kernel: 4gb seg fixup, process ldconfig (pid 7339), cs:ip 73:08060475
Feb 24 03:14:26 localhost kernel: 4gb seg fixup, process ldconfig (pid 7339), cs:ip 73:08060483
Feb 24 03:14:26 localhost kernel: 4gb seg fixup, process ldconfig (pid 7339), cs:ip 73:08060475
Feb 24 03:14:26 localhost kernel: 4gb seg fixup, process ldconfig (pid 7339), cs:ip 73:08060483
Feb 24 03:14:26 localhost kernel: 4gb seg fixup, process ldconfig (pid 7339), cs:ip 73:0807807c
Feb 24 03:14:26 localhost kernel: 4gb seg fixup, process ldconfig (pid 7339), cs:ip 73:08060483
Feb 24 03:14:26 localhost kernel: 4gb seg fixup, process ldconfig (pid 7339), cs:ip 73:08060475
Feb 24 03:14:26 localhost kernel: 4gb seg fixup, process ldconfig (pid 7339), cs:ip 73:08060483
Feb 24 03:23:01 localhost cron[1020]: Error: bad minute; while reading /etc/crontab
Feb 24 03:39:01 localhost /USR/SBIN/CRON[7608]: (root) CMD (  [ -x /usr/lib/php5/maxlifetime ] && [ -d /var/lib/php5 ] && find /var/lib/php5/ -type f -cmin +$(/usr/lib/php5/maxlifetime) -print0 | xargs -n 200 -r -0 rm)
Feb 24 03:44:13 localhost crontab[7619]: (root) BEGIN EDIT (root)
Feb 24 03:48:03 localhost crontab[7627]: (root) BEGIN EDIT (root)
Feb 24 03:57:36 localhost crontab[7700]: (root) BEGIN EDIT (root)
Feb 24 04:00:01 localhost cron[1020]: Error: bad minute; while reading /etc/crontab
Feb 24 04:09:01 localhost /USR/SBIN/CRON[7818]: (root) CMD (  [ -x /usr/lib/php5/maxlifetime ] && [ -d /var/lib/php5 ] && find /var/lib/php5/ -type f -cmin +$(/usr/lib/php5/maxlifetime) -print0 | xargs -n 200 -r -0 rm)
Feb 24 04:12:50 localhost crontab[7843]: (root) BEGIN EDIT (root)
Feb 24 04:18:41 localhost crontab[7843]: (root) END EDIT (root)
Feb 24 04:18:50 localhost crontab[7860]: (root) BEGIN EDIT (root)
Feb 24 04:23:42 localhost crontab[7860]: (root) REPLACE (root)
Feb 24 04:23:42 localhost crontab[7860]: (root) END EDIT (root)
Feb 24 04:24:01 localhost cron[1020]: Error: bad minute; while reading /etc/crontab

Open in new window

0
 
woolmilkporcCommented:
Well, as you can see, the error is 'cron[1020]: Error: bad minute; while reading /etc/crontab'
Please post your crontab entry, or look at ozo's or hfraser's comments above on how to create a valid one.
0
 
iilvAuthor Commented:
Hi,

Whats the problem in my crontab file?
I am attaching thhe code of my /etc/crontab file.

Please help

Thanks
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
 
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
 
# m h dom mon dow user	command
44 * * * * root    cd / && run-parts --report /etc/cron.hourly
29 1 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
58 1 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
12 3 6 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
 
 
 
*/1 * * * *  php /var/www/twitter/twitterbot.php 

Open in new window

0
 
woolmilkporcCommented:
To have a job run every minute, a '*' is sufficient, as an asterisk always means "first to last possible value"
I'm not quite sure whether your cron doesn't support the */n notation or if */1 isn't allowed here.
0
 
iilvAuthor Commented:
Hi ,


I have also checked with the 1 * * * *  php /var/www/twitter/twitterbot.php but still it doesn't work.


Please guide.
0
 
ozoCommented:
what does your
man 5 crontab
say about */1
if it doesn't support it, you may need
* * * * *
for every minute
or
0,5,10,15,20,25,30,35,40,45,50,55 * * * *
for every 5 minutes
0
 
ozoCommented:
If it was recently Feb 24 04:24:01
then it may now be  Feb 24 05:01:01
and you may now see something in the log for
1 * * * *
0
 
iilvAuthor Commented:
Hi,


I have also checked with the * * * * *  php /var/www/twitter/twitterbot.php but still it doesn't work.
Whether I have to restart any service after making changes in crontab file?


Please guide.

Thanks
0
 
woolmilkporcCommented:
1) you can run /etc/init.d/cron restart , but afaik it's not needed with debian.
2) Does your script work when started outside cron?
 
0
 
iilvAuthor Commented:
Hi,

I have done as told by you but with no result.

My script runs outside cron & does the task as required.





Please guide


Thanks
0
 
ozoCommented:
is there any error indicated in the log file?
0
 
ozoCommented:
is php in
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
or is php the user, as suggested by
# m h dom mon dow user  command
0
 
Hugh FraserConsultantCommented:
Try changing the line to:

* * * * *  root php /var/www/twitter/twitterbot.php

It's the simplest form of the crontab entry, and includes the user field mentioned by ozo. It doesn't explain why cron's complaining about the minute field, but if it works, we can backtrack to get to where you want. It will run the script every minute as root.
0
 
iilvAuthor Commented:
Hi,


I have used * * * * *  root php /var/www/twitter/twitterbot.php in /etc/crontab file

I am getting this output in /var/log/syslog

Please guide


Thanks
Feb 26 03:44:01 localhost cron[5976]: Error: bad minute; while reading /etc/crontab
Feb 26 03:44:33 localhost /usr/sbin/cron[6029]: (CRON) INFO (pidfile fd = 3)
Feb 26 03:44:33 localhost /usr/sbin/cron[6030]: (CRON) STARTUP (fork ok)
Feb 26 03:44:33 localhost cron[6030]: Error: bad minute; while reading /etc/crontab
Feb 26 03:44:33 localhost /usr/sbin/cron[6030]: (CRON) INFO (Skipping @reboot jobs -- not system startup)

Open in new window

0
 
rionrocCommented:
Hello

Type at the prompt:

crontab -e

type something, eq: hello(soon to erase)
Save it, :x

Open your previous file that you've made, using gedit, eq: gedit YOURCRONFILE_WHEREIS
Copy cron data.
Open and edit crontab default location at: /var/spool/cron/USERNAME
USERNAME is root?
Paste your previous cron data.
Save it.

Restart cron.

Observe.


Great is our GOD.
:)
rionroc
0
 
iilvAuthor Commented:
Hi

I have also done the method of crontab -e but problem remains
I have also checked man 5 crontab

This is my /etc/crontab file . i was checking whether my command is wrong, so i  removed the last comment from default /etc/crontab file. Still it is displaying error.

Feb 26 22:01:01 localhost cron[10932]: Error: bad minute; while reading /etc/crontab


Please guide

 Thanks
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
 
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
 
# m h dom mon dow user	command
44 * * * * root    cd / && run-parts --report /etc/cron.hourly
29 1 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
58 1 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
12 3 6 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

Open in new window

0
 
rionrocCommented:
Hello,

try to follow my first comment.
and try to script like that:
*/2 * * * * echo "Cron is running every 2min"
44 * * * * run-parts /etc/cron.hourly
29 1 * * * run-parts /etc/cron.daily
58 1 * * 7 run-parts /etc/cron.weekly
12 3 6 * * run-parts /etc/cron.monthly

login to root and see whats going on.
if you see the report "Cron is running every 2min", meaning your cron is actively processing the scripts.

About your run-parts, check if there are entries invalid.

Great is our GOD.
:)
rionroc
0
 
iilvAuthor Commented:
Hi,


I have followed your instructions:
localhost:/# crontab -e
crontab: installing new crontab
localhost:/#

But i cannot see the report when i login as root but when i check the syslog file in /var/log it shows following (attached the code for your reference)
Is it mean its working now?



Please guide

Thanks,

Feb 27 01:10:01 localhost /USR/SBIN/CRON[11785]: (root) CMD (echo "Cron is running every 2 min")
Feb 27 01:10:01 localhost /USR/SBIN/CRON[11784]: (root) MAIL (mailed 28 bytes of output but got status 0x0001 )
Feb 27 01:12:01 localhost /USR/SBIN/CRON[11791]: (root) CMD (echo "Cron is running every 2 min")
Feb 27 01:12:01 localhost /USR/SBIN/CRON[11790]: (root) MAIL (mailed 28 bytes of output but got status 0x0001 )

Open in new window

0
 
iilvAuthor Commented:
Thanks for your suggestions as it resolved my problem.
0
 
rionrocCommented:
Yes, it is working now!
Just try to check your other scripts in run-parts, so there will be no errors coming from them.


Great our GOD.
:)
rionroc
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 11
  • 5
  • 5
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now