LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   What is the best way to get a variable from another server with a shell script? (https://www.linuxquestions.org/questions/programming-9/what-is-the-best-way-to-get-a-variable-from-another-server-with-a-shell-script-700678/)

abefroman 01-28-2009 09:35 PM

What is the best way to get a variable from another server with a shell script?
 
What is the best way to get a variable from another server with a shell script?

IE. Can I have domain.com/var.txt
and var.txt contains either a 0 or a 1
and then have a shell script run every 5 minutes via crontab with the code as follows (so if var.txt contains a 1, dostuff.sh will execute):
#!/bin/bash
var = wget domain.com/var.txt
if $var == 1
/root/dostuff.sh

TIA!

secondmouse 01-29-2009 02:31 AM

There are number of ways, using wget with option -O (as in OK) like this should be the easiest:
Code:

var=$(wget -O - http://domain.com/var.txt)
the O option is to output result to stdout instead of a file.
[ More wget examples can be found @ http://www.editcorp.com/Personal/Lar...v1/wget_7.html ]

Or you can use a python script like this to do the job:
Code:

#!/usr/bin/env python
import os
from urllib import urlopen
raw_text=urlopen('http://domain.com/var.txt').read()
var=raw_text.strip()  # in case there's ending \n
if var=='1':
    f=os.popen('/root/dostuff.sh')
    # the stdout can then be obtained through f.readline(), if needed


abefroman 01-29-2009 03:34 PM

Thanks!

I actually ended up doing it in perl:
#!/usr/bin/perl
$var=qx[wget -O - http://domain/var.txt];
if($var > 0){
qx[sh /root/dostuff.sh];
}


All times are GMT -5. The time now is 06:44 AM.