0

During the test of my script.

The cronjob should run a script through the browser every 2 minutes. So I had the following cron:

*/2 * * * * wget -O http://192.10.10.1/mypage/myscript.php

The cron run a couple of times before I changed it to:

0 1 * * * wget -O http://192.10.10.1/mypage/myscript.php

to run the script everyday at 1am but it does not work.

I am a bit confused why the first one successfully run my script while the latter didn't. What am I doing wrong? Or what could possibly be the reason why running it at 1am does not work?

Kalle Richter
  • 6,180
  • 21
  • 70
  • 103
  • Are you running your cron editing as crontab -e as your normal user? – Terrance Apr 12 '16 at 02:23
  • @Terrance By normal user do you mean root? If yes, then no, I am logged in as another user and used crontab -e – MisterBushido Apr 12 '16 at 02:43
  • No, I didn't mean root, so you should be fine. If you run which wget, does it return /usr/bin/wget? If it does, check your crontab environment by adding a line to it that says * * * * * env > /home/<username>/env_dump.txt so that you can see what the environment is set to. Hopefully the /usr/bin is in your path environment. Remove the line from crontab after 1 minute. It will create the file. – Terrance Apr 12 '16 at 02:52
  • Your line looks like it should work fine though. – Terrance Apr 12 '16 at 02:53
  • I believe it's fine too. Though it confuses me that when it is ran every two minutes it works fine but when I set it at 1am it does not work. Does it affect that I have my terminal turned off at that time? (Sorry. I'm a noob at Linux) – MisterBushido Apr 12 '16 at 03:05
  • The computer itself would have to stay on at that time. You could set up a crontab job to turn off the computer at a specified time after that one is supposed to run. See here: http://askubuntu.com/questions/567955/automatic-shutdown-at-specified-times – Terrance Apr 12 '16 at 03:07
  • To me, the -O option to wget is missing a filename. If I try this command in a terminal I get an error. – Jean-Marie Apr 12 '16 at 15:10

1 Answers1

0

As pointed out in a comment above, the most obvious pbm is that you are not using the -O option correctly.

Look up man wget in terminal. Here is an extract:

-O file
--output-document=file.
The documents will not be written to the appropriate files, but all will be concatenated together and written to "file". If - is used as file, documents will be printed to standard output (stdout), disabling link conversion. Use ./- to print to a file literally named -.

Use of -O is analogous to shell redirection:
wget -O file http://foo is intended to work like wget -O - http://foo > file; where "file" will be truncated immediately, and all downloaded content will be written there.

If, as a non root user, you run a GUI cmd or direct yr output to stdout in an already running X session, make sure yr cron environment knows about the active display. To make cron GUI aware, i.e. to tell it, what display the program should use (:0 is the default in a desktop environment)

0 1 * * * export DISPLAY=:0; XAUTHORITY=~/.Xauthority /usr/bin/wget -O - http://192.10.10.1/mypage/myscript.php

or, if you want to set the DISPLAY environment variable ONLY for a specific cmd:

0 1 * * * DISPLAY=:0 XAUTHORITY=~/.Xauthority /usr/bin/wget -O - http://192.10.10.1/mypage/myscript.php

or, if redirecting output to a file for later perusal, the need to specify the correct display disappears because nothing actually goes to stdout :

0 1 * * *  /usr/bin/wget -O <filename> http://192.10.10.1/mypage/myscript.php

The latter solution makes more sense, if you run yr cron job at 1am and you are not sitting in front of yr desktop display.

==
Let us known in case of continuing problems.

Cbhihe
  • 2,761
  • 3
  • 24
  • 47