Search code examples
pythonlinuxprintingbackgroundmessage

print message from python program that started in background


I made a python program thats is called when I connect to a ppp network.The app requests www.noip.com to link any new IP to a Host I created in their website.

/etc/ppp/ip-up.d/noip.sh  #this script calls my python app 

The script runs fine when I connect to ppp, the python app is triggered and does change my IP addr on www.noip.com, but I'm not able to print messages to the console using just print. I have a few print statements in my app that only work if I run the app from the command line like ./myapp.py

How to show messages if my python app is called from background?

here is my Python code:

#!/usr/bin/python     

import requests                                                                 
import netifaces as ni

user = 'xxxxxxx'                                                          
pswd = 'xxxxxxx'                      

ni.ifaddresses('ppp0')                        
ip = ni.ifaddresses('ppp0')[2][0]['addr']        
myhostname = 'xxxxxxx'                                                  

payload = {'hostname' : myhostname , 'myip' : ip}
r = requests.get("http://dynupdate.no-ip.com/nic/update", params=payload, auth=(user,pswd))

print " "                                                                       
if "good" in r.text:                                                            
        print "Hello ", user, "!"        
        print "your IP was successfully updated to:", ip        
        print myhostname, "is up and running!"                                  
if "nochg" in r.text:                                                           
        print "Hello", user, "!"                                                
        print "Your IP", ip, "is still active, no change needed"                
if "nohost" in r.text:                                                          
        print "The given Host name", myhostname, "does not exist under specified account"
        print "Please review your Host name and try again"                      
if "badauth" in r.text:                                                 
        print "Login and/or Username incorrect"                                 
        print "Please correct your credentials and try again"           
if "911" in r.text:                                                     
        print "Sorry for the incovenience but we are experiencing some problems right now"
        print "Please try again later"                                          
print "noip.com says:", r.text                                                  
print " "  

Solution

  • The simplest to do this is to make the output go to a file in noip.sh:

    python myapp.py > /tmp/myapp.out

    Then when you want to see the output,

    tail -f /tmp/myapp.out