Results 1 to 3 of 3

Thread: Cron job run with internet connection

  1. #1
    Join Date
    Jan 2012
    Beans
    5

    Thumbs down Cron job run with internet connection

    Hi all,

    I've written a script that downloads a certain file for Windows machines and saves it under a filename based on the date - that works fine and is available to anyone else who wants it.

    I have also set a cron job to run this script on a weekly basis.

    However, I cannot always guarantee that this machine will be on or connected to the internet at the time the job is set. What I need to know is how to make the job run at a time when the machine is on and has an internet connection, and if there isn't a connection (or the job is missed due to the machine being off) then to make it run at a later time, even the next day if necessary.

    Any thoughts on how I'd do this?

    Thanks in advance.

  2. #2
    Join Date
    Jan 2007
    Beans
    6,543
    Distro
    Ubuntu 13.04 Raring Ringtail

    Re: Cron job run with internet connection

    Quote Originally Posted by gnome_nz View Post
    Any thoughts on how I'd do this?
    Anacron.

    Regular cron is designed for machines that are always on. For desktops you want to use anacron. Drop your script into /etc/cron.weekly and it'll be run once a week. Anacron is also significantly less faff to set up than cron. If your script is in the folder, it gets run. Simple as that, no config required.

    As for the connection, just have the script ping google, or some other site you know will always be up, and exit if it fails.
    Last edited by Paqman; January 23rd, 2012 at 04:00 PM.

  3. #3
    Join Date
    Feb 2007
    Beans
    185

    Re: Cron job run with internet connection

    Yes. Ping something that works, and based upon the exit status, do your other stuff.

    Put it all in a shell script, and have the cron job run the script.

    Code:
    #!/bin/bash
    ping -c 1 -n 135.122.82.80 &> /dev/null
    if [[ $? -eq 0 ]] #exit status 0 is success.
    then
      #insert commands here
    echo success
    fi

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •