Search code examples
crondebianwget

wget in script not working when called from cron


I've read a load of similar cases and can't for the life of me figure this one out...

I'm running a wget command inside a .sh script which is called from cron on reboot as follows:

@reboot /home/user/reboot_script.sh

The .sh script starts with

#!/bin/bash

And I have done chmod +x reboot_script.sh

The line that fails is :

Either

mac=$(</home/user/mac.txt)

Which may not be providing the content to the variable in the wget

OR

/usr/bin/wget "http://my.domain.com/$mac/line.txt" -O /home/user/line.txt 

If I run the script from command line, it works absolutely fine but if it runs from the cron on reboot, the script runs, but line.txt is saved as an empty file (0 bytes). Again, if run directly from command line, it works fine.

I've looked at file permissions, absolute paths, everything I can think of, but I've been staring at this for hours now.

Any help would be appreciated. Thanks.


Solution

  • @reboot is too early in boot process IMHO. You should create a systemd script to wait network.

    As a workaround, you can add a

    sleep 30
    

    or better:

    until ping -c1 domain.com &>/dev/null; do
        sleep 5
    done
    

    before your wget