Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have cloned a git repository of some project. I want to automate git pull and compile the project once in a week. I am using a laptop which wont be on 24x7. Now, I cannot use cron as I should keep the system running at that exact moment. I cannot use anacron too as it might start before I connect to network. Is there some option in anacron that will run this particular job only when I am connected to internet? Or should I be using some other different tool for this?

share|improve this question
Your network configuration manager may be able to execute code after the network is brought up. – jordanm Sep 26 '12 at 19:25
Yes. but I dont want the job to be run everytime the network is up. I want to run only once in a week. – balki Sep 26 '12 at 19:30
The script could touch a file and check mtime on that file each time it runs. – jordanm Sep 26 '12 at 19:39
hmm.. yes. I can write a wrapper on my own but I think there must be some tool which handles it better. – balki Sep 26 '12 at 20:06
The question might be phrased specific, but the idea was to run anacron only when a particular condition is fulfilled (here: network connection) and that is widely useful. It would also be nice to have a conditional retry, e.g. “run this job once per day successfully, if it returns non-zero, retry once per hour”. – Marco Sep 26 '12 at 22:22

Use anacron, because you won't be up 24/7, then add some magic to ensure the network is up (by pinging the default gateway), and if not delay the execution.

Here's an example script that will get you close:

cd repo
JOB="git pull"
ping -c 2 $(netstat -rn | awk '/^ {print $2}') 2>&1 > /dev/null && $JOB || echo $JOB | at $(date --date="+1 hour" +%H:%M)
share|improve this answer

Run the job when you connect to the network. Most distributions have a scripting infrastructure that you can plug into, though you will need root permissions. If you connect with NetworkManager or Wicd, they have their own hook infrastructure as well. Add a cron job that only runs if the network is available (and, optionally, only if the job hasn't been run performed in a long time), in case the network remains connected for a long time.

You don't specify your distribution, so I'll give an example for a Debian-based distribution. The scripts in /etc/network/if-up.d are executed after an interface is brought up; see the interfaces(5) man page for more information. Write a script /etc/network/if-up.d/zzzz_balki_git_pull such as this:

su balki -c /home/balki/bin/pull_repository_if_old

with a pull_repository_if_old that does something like this (run git pull if the network is available unless there is a timestamp that is less than 7 days old):

set -e
if [ -z "$(find /path/to/.repository.pull.timestamp -mtime -7)" ] &&
   ping -c 1 -w 1 repository.example.com >/dev/null 2>/dev/null
  cd /path/to/repository
  git pull
  touch /path/to/.repository.pull.timestamp

And a crontab entry on your account:

28 4 * * * /home/balki/bin/pull_repository_if_old
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.