element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Enchanted Objects
  • Challenges & Projects
  • Design Challenges
  • Enchanted Objects
  • More
  • Cancel
Enchanted Objects
Blog Enchanted Objects Design Challenge - Python Start, Python Stop
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Workshopshed
  • Date Created: 24 Jun 2015 12:29 PM Date Created
  • Views 2006 views
  • Likes 3 likes
  • Comments 9 comments
  • enchanted_cottage
  • enchanted_objects
  • arduino_yun
Related
Recommended

Enchanted Objects Design Challenge - Python Start, Python Stop

Workshopshed
Workshopshed
24 Jun 2015

Putting on the roof

One of the big physical jobs left for Matilda and Hans was putting the roof back on. They realised that they could not do the job by themselves so they wandered down the Adam and Eve Inn to look for volunteers. It turned out that everyone wanted to help so the barkeeper closed the pub and he and the snake came along too.

 

The helpers were of various use, the Dwarves spent most of the time arguing and Farmer Hogg kept saying things like "I'd not have started from here". However the barkeeper and snake were very useful. Whenever the barkeeper wanted the snake to do something he'd command "Python Start" and whenever a job was done he'd say "Python Stop". The roof was quickly done and Hans and Matilda posed for a photo.

image

They thanked everyone who in turn decided to go back to the inn to celebrate. Hans and Matilda had plenty to do so celebrating would have to wait.

 

Hans had forgotten

Hans realised he'd forgotten a critical step in the process. How to start and stop his Python script. He looked back over his notes and realised that he could use bits from the Handshaking code and from the Fail2Ban setup.

 

Init script

An initialisation script was created.

 

#!/bin/sh /etc/rc.common
# Initialise Python Listening on Serial Port

workdir=/mnt/sda1/Python
START=8
STOP=14

start() {
    cd $workdir
    /usr/bin/python /mnt/sda1/Python/GetWeather.py &
    echo "Weather Server started."
}

stop() {
    pid=`ps -ef | grep '[p]ython /mnt/sda1/Python/GetWeather.py' | awk '{ print $1 }'`
    echo $pid
    kill $pid
    sleep 2
    echo "Weather Server killed."
}

 

Changes to Python Code

The python code needed minor changes so that it would respond to the shutdown request

# Handle exit and kill from OS
def set_exit_handler(func):
    signal.signal(signal.SIGTERM, func)
def on_exit(sig, func=None):
    print "exit handler triggered"
    sys.exit(1)

# Run program
if __name__ == '__main__':
    set_exit_handler(on_exit)
    sys.exit(main())

 

Testing

The init script was tested with

/etc/init.d/weather-daemon start

and

ps

to check it was running

 

Then shutdown using

/etc/init.d/weather-daemon stop

 

Finally, it was installed using

/etc/init.d/weather-daemon enable

 

Next: Enchanted Objects Design Challenge - Planning and Building

 

References

http://www.pietervanos.net/knowledge/start-python-script-from-init-d/

https://wolfpaulus.com/jounal/software/pythonlauncher/

http://wiki.openwrt.org/doc/techref/initscripts

  • Sign in to reply

Top Comments

  • clem57
    clem57 over 10 years ago +2
    Oh the devil in the details. I like the way you wraped this dtory up with a bow tie. Clem
  • DAB
    DAB over 10 years ago +1
    Good finish Andy. Everything looks great and the final tweeks to the software should make it run well. DAB
  • Workshopshed
    Workshopshed over 10 years ago in reply to DAB +1
    Not quite finished yet.... Got a few most posts queued up for today and tomorrow and hope to create a video demo
Parents
  • Jan Cumps
    Jan Cumps over 10 years ago

    ps -fe | grep weather-daemon

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 10 years ago in reply to Jan Cumps

    or ps -fe | grep python

    (I forgot after all these years)

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 10 years ago in reply to Jan Cumps

    Hehe, only saw that you already have covered that when I scrolled down your script>. It didn't show when I read the post.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Workshopshed
    Workshopshed over 10 years ago in reply to Jan Cumps

    Thanks Jan, yes the examples I found used that technique to pick the right process to stop. It's nice that the Python script can check for that event and if needed do a graceful shutdown

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • Workshopshed
    Workshopshed over 10 years ago in reply to Jan Cumps

    Thanks Jan, yes the examples I found used that technique to pick the right process to stop. It's nice that the Python script can check for that event and if needed do a graceful shutdown

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube