Cron not working with shell script

cron

In the process of learning/understanding Linux (difficult but enjoying it). I have written a very short shell script that uses wget to pull an index.html file from a website.

#!/bin/bash

#Script to wget website mywebsite and put it in /home/pi/bin

index=$(wget www.mywebsite.com)

And this works when i enter the command wget_test into command line. It outputs a .html file into /home/pi/bin.

I have started trying to do this via cron so i can do it at a specific time. I entered the following by using crontab -e

23 13 * * *   /home/pi/bin/wget_test

In this example I wanted the script to run at 13.23 and to output a .html file to /home/pi/bin but nothing is happening.

Best Answer

This line index=$(wget www.mywebsite.com) will set the variable $index to nothing. This is because (by default) wget doesn't write anything to stdout so there's nothing to put into the variable.

What wget does do is to write a file to the current directory. Cron jobs run from your $HOME directory, so if you want to write a file to your $HOME/bin directory you need to do one of two things

  1. Write wget -O bin/index.html www.mywebsite.com
  2. Write cd bin; wget www.mywebsite.com

Incidentally, one's ~/bin directory is usually where personal scripts and programs would be stored, so it might be better to think of somewhere else to write a file regularly retrieved from a website.

Related Question