Overclock.net › Forums › Software, Programming and Coding › Operating Systems › Linux, Unix › Help with cron jobs
New Posts  All Forums:Forum Nav:

Help with cron jobs

post #1 of 9
Thread Starter 
Hi, i am trying to setup a cron job. I ran the command :
Code:
crontab -e
and set this command and saved it.
Code:
*/5 * * * * wget -q http://www.gladopia.com/cron.php > /home/ubuntu/cronlogs/clean_tmp_dir.log

I assumed that an entry would be made into the logfile. But instead i see a empty file being created in /home/ubuntu folder every time the cron job is run. Could some one please tell me what i am doing wrong? I just want an entry made into a single logfile rather than creating a file every time. Thanks.
Green Transporter
(17 items)
 
  
CPUMotherboardGraphicsRAM
AMD X6 1090T (@ 3.64GHz) Asus 890 GX EVGA GTX 285 Corsair 4 GB DDR3 1600MHz 
Hard DriveHard DriveHard DriveHard Drive
WD 500 WD 500 WD 250 Segate 80 
Hard DriveOptical DriveOSMonitor
Samsung SDD LG BLU RAY DRIVE Windows 7 64-bit LG 23'' LED 
PowerCaseMouse
Corsair TX750W CM 690 Logitech  
  hide details  
Reply
Green Transporter
(17 items)
 
  
CPUMotherboardGraphicsRAM
AMD X6 1090T (@ 3.64GHz) Asus 890 GX EVGA GTX 285 Corsair 4 GB DDR3 1600MHz 
Hard DriveHard DriveHard DriveHard Drive
WD 500 WD 500 WD 250 Segate 80 
Hard DriveOptical DriveOSMonitor
Samsung SDD LG BLU RAY DRIVE Windows 7 64-bit LG 23'' LED 
PowerCaseMouse
Corsair TX750W CM 690 Logitech  
  hide details  
Reply
post #2 of 9
Please read the wget manpage to see what -q does.
Also, you'll need >> and not > if you want to append text to the end of the file and not erase and rewrite the file from the beginning every time the cronjob runs.
15s
(13 items)
 
  
CPUMotherboardGraphicsRAM
Athlon II x4 640 Gigabyte 880GM-D2H AMD HD4250 4 GB 
Hard DriveOSMonitorPower
640GB Arch GNU/Linux i686 Fujitsu Siemens 4612 FA 350 W 
Mouse
Logitech G9x 
  hide details  
Reply
15s
(13 items)
 
  
CPUMotherboardGraphicsRAM
Athlon II x4 640 Gigabyte 880GM-D2H AMD HD4250 4 GB 
Hard DriveOSMonitorPower
640GB Arch GNU/Linux i686 Fujitsu Siemens 4612 FA 350 W 
Mouse
Logitech G9x 
  hide details  
Reply
post #3 of 9
Thread Starter 
Thanks mate. I read that -q is for quite mode.
Green Transporter
(17 items)
 
  
CPUMotherboardGraphicsRAM
AMD X6 1090T (@ 3.64GHz) Asus 890 GX EVGA GTX 285 Corsair 4 GB DDR3 1600MHz 
Hard DriveHard DriveHard DriveHard Drive
WD 500 WD 500 WD 250 Segate 80 
Hard DriveOptical DriveOSMonitor
Samsung SDD LG BLU RAY DRIVE Windows 7 64-bit LG 23'' LED 
PowerCaseMouse
Corsair TX750W CM 690 Logitech  
  hide details  
Reply
Green Transporter
(17 items)
 
  
CPUMotherboardGraphicsRAM
AMD X6 1090T (@ 3.64GHz) Asus 890 GX EVGA GTX 285 Corsair 4 GB DDR3 1600MHz 
Hard DriveHard DriveHard DriveHard Drive
WD 500 WD 500 WD 250 Segate 80 
Hard DriveOptical DriveOSMonitor
Samsung SDD LG BLU RAY DRIVE Windows 7 64-bit LG 23'' LED 
PowerCaseMouse
Corsair TX750W CM 690 Logitech  
  hide details  
Reply
post #4 of 9
Quote:
Originally Posted by BinaryBird;14202653 
Thanks mate. I read that -q is for quite mode.

Therefore nothing is printed out.

What >> and > do is redirect the OUTPUT of the application (in this case 'wget') to the designated file. With zero output the file is well, empty. wink.gif
15s
(13 items)
 
  
CPUMotherboardGraphicsRAM
Athlon II x4 640 Gigabyte 880GM-D2H AMD HD4250 4 GB 
Hard DriveOSMonitorPower
640GB Arch GNU/Linux i686 Fujitsu Siemens 4612 FA 350 W 
Mouse
Logitech G9x 
  hide details  
Reply
15s
(13 items)
 
  
CPUMotherboardGraphicsRAM
Athlon II x4 640 Gigabyte 880GM-D2H AMD HD4250 4 GB 
Hard DriveOSMonitorPower
640GB Arch GNU/Linux i686 Fujitsu Siemens 4612 FA 350 W 
Mouse
Logitech G9x 
  hide details  
Reply
post #5 of 9
Redirections and wget's option have been covered nicely, but I'll offer some unsolicited advice...

First of all, if you're going to want to log entries from your cron jobs, it's very important you also log your 'stderr' which is where any sort of errors that the task encounters will typically be printed to. To do this you want to use the '&' operator as so...
Code:
task >> ./logfile 2>&1

What this does is it tells it to append the 'output" (stdout is its technical name) to your logfile. Now, error messages are printed on "stderr", and in order for the output to logfile to contain error messages you need to redirct stderr into stdout, and you do so by the "2>&1" operator which tells it, "Redirect stderr into stdout" and the ">>" operators tells it to append that info to your log file. The way the file redictors work can sometimes be confusing... With this example in specific, putting the "2>&1" after the file name and in addition to the ">>" operator is pretty counter-intuitive.

The other thing here is that wget has its own logging option, which you may find after reading the manual and think, "Well, that's a whole lot simpler, I'll just use that." That might in fact be a good idea, but the problem there is that if for some reason the execution of wget (or whatever your program is) fails and it just so happens it's not programmed to send its error message to the log file (most programs send errors to stderr and sometimes programmers forget to change this to account for log files), and instead sends it to stderr like a lot of programs do, then your task will not run and your log file will not show why.

The best way for you to do this is as so...
Code:
wget url -o logfile 2>> logfile

This way whatever output that wget generates will be sent directly to the log file without having to handle it with the bash redirector operators, meanwhile you still ensure that if any error message is printed to 'stderr' that it too will be recorded, even if the program itself is not smart enough to send errors to your logfile instead of 'stderr'.
The Media Machine
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom X4 9950 BE @ 3.2Ghz DFI LanParty JR 790GX-M2RS Nvidia GeForce 7950 2 GB OCZ "Special Ops Edition" PC6400 
Hard DriveOptical DriveOSMonitor
3 x 1TB SONY DVD RW DRU-830A Linux 2.6.30.5-custom 19" @ 1600x1200 + 26" @ 720x480 
PowerCase
Antec TruePower Trio 550 Antec P180 
  hide details  
Reply
The Media Machine
(13 items)
 
  
CPUMotherboardGraphicsRAM
Phenom X4 9950 BE @ 3.2Ghz DFI LanParty JR 790GX-M2RS Nvidia GeForce 7950 2 GB OCZ "Special Ops Edition" PC6400 
Hard DriveOptical DriveOSMonitor
3 x 1TB SONY DVD RW DRU-830A Linux 2.6.30.5-custom 19" @ 1600x1200 + 26" @ 720x480 
PowerCase
Antec TruePower Trio 550 Antec P180 
  hide details  
Reply
post #6 of 9
Thread Starter 
Thanks for that info.
This is my current cron job:
Code:
MAILTO=nithin.kv@jivity.com
*/5 * * * * wget -o http://www.gladopia.com/cron.php mlogfile 2>> /home/ubuntu/cronlogs/mlogfile

I see that in the /home/ubuntu/cronlogs folder mlogfile is created and it contains : wget: http://www.gladopia.com/cron.php: No such file or directory

Does this mean the cron.php doesnot exist or the mlogfile does not exist? Thanks again.
Green Transporter
(17 items)
 
  
CPUMotherboardGraphicsRAM
AMD X6 1090T (@ 3.64GHz) Asus 890 GX EVGA GTX 285 Corsair 4 GB DDR3 1600MHz 
Hard DriveHard DriveHard DriveHard Drive
WD 500 WD 500 WD 250 Segate 80 
Hard DriveOptical DriveOSMonitor
Samsung SDD LG BLU RAY DRIVE Windows 7 64-bit LG 23'' LED 
PowerCaseMouse
Corsair TX750W CM 690 Logitech  
  hide details  
Reply
Green Transporter
(17 items)
 
  
CPUMotherboardGraphicsRAM
AMD X6 1090T (@ 3.64GHz) Asus 890 GX EVGA GTX 285 Corsair 4 GB DDR3 1600MHz 
Hard DriveHard DriveHard DriveHard Drive
WD 500 WD 500 WD 250 Segate 80 
Hard DriveOptical DriveOSMonitor
Samsung SDD LG BLU RAY DRIVE Windows 7 64-bit LG 23'' LED 
PowerCaseMouse
Corsair TX750W CM 690 Logitech  
  hide details  
Reply
post #7 of 9
You're confusing your arguments. -o should be the argument before the file you're outputting to. This should work:

*/5 * * * * wget -omlogfile http://www.gladopia.com/cron.php 2>> /home/ubuntu/cronlogs/mlogfile

Don't forget that this will output it to your home directory, which you probably don't want. This should be more correct:

*/5 * * * * wget -o/home/ubuntu/cronlogs/mlogfile http://www.gladopia.com/cron.php 2>> /home/ubuntu/cronlogs/mlogfile
#well
(19 items)
 
Lenovo L530
(8 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 4770k Gigabyte Z87X-UD4H XFX Radeon HD 6950 Corsair CMX8GX3M2A2000C9 
Hard DriveHard DriveOptical DriveCooling
Samsung EVO 840 Hitachi HDS722020ALA330 Generic DVD±RW Burner Noctua NH-D14 
OSMonitorMonitorKeyboard
Windows 8.1 Dell U2711 LG W2453 Ducky DK9008 Overclock.net Edition, Cherry MX B... 
PowerCaseMouseMouse Pad
Corsair TX850 Antec Three Hundred Corsair Raptor M40 QPAD HeatoN M 
AudioAudioAudio
E-MU Tracker|pre Beyerdynamic DT-770 250 Ohm AntLion ModMIc 
CPUGraphicsRAMRAM
Intel Ivy Bridge 3210M Intel HD 4000 Graphics Soldered Corsair Vengeance  
Hard DriveOptical DriveOSMonitor
500GB DVD-+RW Windows 7 Professional 1600x900 
  hide details  
Reply
#well
(19 items)
 
Lenovo L530
(8 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 4770k Gigabyte Z87X-UD4H XFX Radeon HD 6950 Corsair CMX8GX3M2A2000C9 
Hard DriveHard DriveOptical DriveCooling
Samsung EVO 840 Hitachi HDS722020ALA330 Generic DVD±RW Burner Noctua NH-D14 
OSMonitorMonitorKeyboard
Windows 8.1 Dell U2711 LG W2453 Ducky DK9008 Overclock.net Edition, Cherry MX B... 
PowerCaseMouseMouse Pad
Corsair TX850 Antec Three Hundred Corsair Raptor M40 QPAD HeatoN M 
AudioAudioAudio
E-MU Tracker|pre Beyerdynamic DT-770 250 Ohm AntLion ModMIc 
CPUGraphicsRAMRAM
Intel Ivy Bridge 3210M Intel HD 4000 Graphics Soldered Corsair Vengeance  
Hard DriveOptical DriveOSMonitor
500GB DVD-+RW Windows 7 Professional 1600x900 
  hide details  
Reply
post #8 of 9
Thread Starter 
Quote:
Originally Posted by gonX View Post

You're confusing your arguments. -o should be the argument before the file you're outputting to. This should work:
*/5 * * * * wget -omlogfile http://www.gladopia.com/cron.php 2>> /home/ubuntu/cronlogs/mlogfile
Don't forget that this will output it to your home directory, which you probably don't want. This should be more correct:
*/5 * * * * wget -o/home/ubuntu/cronlogs/mlogfile http://www.gladopia.com/cron.php 2>> /home/ubuntu/cronlogs/mlogfile

I setup this cron :
Code:
*/5 * * * * wget -o/home/ubuntu/cronlogs/mlogfile http://www.jivity.com/cron.php 2>> /home/ubuntu/cronlogs/mlogfile

This is what the file mlogfile contains :
Code:
--2011-12-14 10:45:01--  http://www.jivity.com/cron.php
Resolving www.jivity.com... 175.41.148.169
Connecting to www.jivity.com|175.41.148.169|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 0 [text/html]
Saving to: `cron.php.5'

     0K                                                        0.00 =0s

2011-12-14 10:45:03 (0.00 B/s) - `cron.php.5' saved [0/0]

Also i see that an empty cron.php.1, cron.php.2 and so on being created in the /home/ubuntu folder.

How do i stop these multiple files being created? I just want one file. Thanks for helping me out.
Green Transporter
(17 items)
 
  
CPUMotherboardGraphicsRAM
AMD X6 1090T (@ 3.64GHz) Asus 890 GX EVGA GTX 285 Corsair 4 GB DDR3 1600MHz 
Hard DriveHard DriveHard DriveHard Drive
WD 500 WD 500 WD 250 Segate 80 
Hard DriveOptical DriveOSMonitor
Samsung SDD LG BLU RAY DRIVE Windows 7 64-bit LG 23'' LED 
PowerCaseMouse
Corsair TX750W CM 690 Logitech  
  hide details  
Reply
Green Transporter
(17 items)
 
  
CPUMotherboardGraphicsRAM
AMD X6 1090T (@ 3.64GHz) Asus 890 GX EVGA GTX 285 Corsair 4 GB DDR3 1600MHz 
Hard DriveHard DriveHard DriveHard Drive
WD 500 WD 500 WD 250 Segate 80 
Hard DriveOptical DriveOSMonitor
Samsung SDD LG BLU RAY DRIVE Windows 7 64-bit LG 23'' LED 
PowerCaseMouse
Corsair TX750W CM 690 Logitech  
  hide details  
Reply
post #9 of 9
A "belt and braces" wget command would be the following:
Code:
wget --user-agent="Mozilla/5.0 Gecko/20100401 Firefox/3.6.3" --timeout=5 --force-html --no-check-certificate -q -O - http://www.jivity.com/cron.php >> /home/ubuntu/cronlogs/mlogfile 2>&1

The user-agent bit and force-html aren't necessary if it's your script and you're happy with the way it handles crawlers and the output it produces.
no-check-certificate is also only necessary if you're ever likely to go to SSL (arguably you shouldn't ever need this even on SSL, but sometimes people let certs run out or use self signed, which will cause wget to fail when automated).

However, if this HP script is running on the same server as the cron job, then you should just run it locally (like any other shell script) instead of using wget. eg:
Code:
php /path/to/cron.php >> /home/ubuntu/cronlogs/mlogfile 2>&1
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Linux, Unix
Overclock.net › Forums › Software, Programming and Coding › Operating Systems › Linux, Unix › Help with cron jobs