{"id":8767,"date":"2018-02-19T01:21:37","date_gmt":"2018-02-18T16:21:37","guid":{"rendered":"http:\/\/leavebehind.iobb.net\/wordpress\/?p=8767"},"modified":"2018-07-02T21:41:27","modified_gmt":"2018-07-02T12:41:27","slug":"daemonize_python_script","status":"publish","type":"post","link":"http:\/\/leavebehind.mydns.jp\/wordpress\/2018\/02\/19\/daemonize_python_script\/","title":{"rendered":"Python\u30b9\u30af\u30ea\u30d7\u30c8\u306e\u30c7\u30fc\u30e2\u30f3\u5316"},"content":{"rendered":"<p>\u30db\u30c3\u30bf\u30e9\u30ab\u30b7\u306eSenseHAT\u3092\u52d5\u304b\u3057\u3066\u307f\u308b\u3002<\/p>\n<p>LED\u3078\u6642\u8a08\u3092\u4e0a3\u6bb5\u306b\u8868\u793a\u30012\u9032\u6570\u8868\u793a\u306a\u306e\u3067\u53ef\u8aad\u6027\u306f\u5168\u304f\u306a\u3057\u3002<br \/>\n4\u6bb5\u76ee\u306f\u30e1\u30e2\u30ea\u4f7f\u7528\u7387\u3001\u6b8b\u308a4\u6bb5\u3092CPU\u30b3\u30a2\u306e\u4f7f\u7528\u7387\u3092\u8868\u793a\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-8772 size-large aligncenter\" src=\"http:\/\/leavebehind.iobb.net\/wordpress\/wp-content\/uploads\/2018\/02\/display_pi_load_status-1024x1024.jpg\" alt=\"\" width=\"750\" height=\"750\" srcset=\"http:\/\/leavebehind.mydns.jp\/wordpress\/wp-content\/uploads\/2018\/02\/display_pi_load_status-1024x1024.jpg 1024w, http:\/\/leavebehind.mydns.jp\/wordpress\/wp-content\/uploads\/2018\/02\/display_pi_load_status-150x150.jpg 150w, http:\/\/leavebehind.mydns.jp\/wordpress\/wp-content\/uploads\/2018\/02\/display_pi_load_status-300x300.jpg 300w, http:\/\/leavebehind.mydns.jp\/wordpress\/wp-content\/uploads\/2018\/02\/display_pi_load_status-768x768.jpg 768w, http:\/\/leavebehind.mydns.jp\/wordpress\/wp-content\/uploads\/2018\/02\/display_pi_load_status.jpg 1240w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/p>\n<p>Rasbian Streach\u306b\u306fSenseHAT\u7528Python\u30e2\u30b8\u30e5\u30fc\u30eb\u3082\u65e2\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u3066\u3044\u308b\u72b6\u614b\u306a\u306e\u3067\u3001\u96e3\u3057\u3044\u8a2d\u5b9a\u306f\u7121\u3057\u3002API\u30ea\u30d5\u30a1\u30ec\u30f3\u30b9\u3092\u8aad\u3081\u3070\u7279\u306b\u96e3\u3057\u3044\u3068\u3053\u308d\u3082\u306a\u304f\u3001\u3001\u3001<\/p>\n<p>\u305d\u308c\u3060\u3051\u3067\u306f\u9762\u767d\u304f\u306a\u3044\u306e\u3067systemd\u306b\u3088\u308b\u30c7\u30fc\u30e2\u30f3(\u30b5\u30fc\u30d3\u30b9)\u5316\u3082\u3057\u3066\u307f\u307e\u3057\u305f\u3002\u5341\u5206\u306a\u7406\u89e3\u306f\u51fa\u6765\u3066\u307e\u305b\u3093\u304c\u3001\u4ee5\u4e0bPython\u30b9\u30af\u30ea\u30d7\u30c8\u3067\u3059\u3002<\/p>\n<p><!--more--><\/p>\n<pre class=\"lang:python decode:true \" title=\"sensemessaged.py\">#!\/usr\/bin\/python\n\nfrom __future__ import with_statement\n\nimport sys\nimport time\nimport datetime\nimport psutil\nimport math\n\nfrom sense_hat import SenseHat\nfrom daemon import DaemonContext\nfrom lockfile.pidlockfile import PIDLockFile\n\nsense = SenseHat()\nsense.set_rotation(180)\nsense.low_light=True\nmessage = \"\"\n\nWHITE = [50,50,50]\nBLUE = [0,0,50]\nBLANK = [0,0,0]\nRED = [50,0,0]\nYELLOW = [50,50,0]\nGREEN = [0,50,0]\n\ndc = DaemonContext(\n        pidfile=PIDLockFile('\/tmp\/sense_messaged.pid'),\n        stderr=open('\/tmp\/sense_messaged.log','w+'))\n\ndef main():\n    '''\n    now = datetime.datetime.now()\n    message = now.strftime(\"Time:%H:%M:%S\")\n    sense.show_message(message ,text_colour=[50,50,50])\n    '''\n    now = datetime.datetime.now()\n    h = [BLANK if x == '0' else WHITE for x in list(format(now.hour,'08b'))]\n    m = [BLANK if x == '0' else WHITE for x in list(format(now.minute,'08b'))]\n    s = [BLANK if x == '0' else WHITE for x in list(format(now.second,'08b'))]\n    pict = []\n    pict.extend(h)\n    pict.extend(m)\n    pict.extend(s)\n\n    memory = int(max(0,min(psutil.virtual_memory().percent,100))\/100 * 8)\n    memoryPic = ([BLUE] * (8-memory)) + ([YELLOW] * memory)\n    pict.extend(memoryPic)\n\n    cpu = [int(max(0,min(x,100))\/100 * 8)  for x in psutil.cpu_percent(percpu=True)]\n    for c in cpu:\n        cPic = ([GREEN] * (8-c)) + ([RED] * c)\n        pict.extend(cPic)\n\n    sense.set_pixels(pict)\n\nwith dc:\n    while True:\n        main()\n        time.sleep(1)\nsys.exit(0)<\/pre>\n<p>daemon\u3001psutil\u30e2\u30b8\u30e5\u30fc\u30eb\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true \">$ sudo pip install python-daemon<\/pre>\n<p>psutil\u30e2\u30b8\u30e5\u30fc\u30eb\u306fpip\u304b\u3089\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3088\u3046\u3068\u3059\u308b\u3068\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3059\u308b\u306e\u3067apt\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"lang:sh decode:true\">$ sudo apt install python-psutil<\/pre>\n<p>\u3042\u3068\u306fsystemctl\u7528\u306eunit\u30d5\u30a1\u30a4\u30eb(ExecStart\u306fPython\u30b9\u30af\u30ea\u30d7\u30c8\u3078\u306e\u30d1\u30b9\u3067\u66f8\u304d\u304b\u3048)<\/p>\n<pre class=\"lang:sh decode:true\" title=\"\/etc\/systemd\/system\/sensemessaged.service\">[Unit]\nDescription=SenseHat Message\n[Service]\nExecStart=\/PATH_TO_PYTHON_SCRIPT\/sensemessaged.py\nRestart=always\nType=simple\nPIDFile=\/tmp\/sense_messaged.pid\n[Install]\nWantedBy=multi-user.target<\/pre>\n<p>\u8d77\u52d5\u3057\u3066\u307f\u308b\u3002<\/p>\n<pre class=\"lang:sh decode:true\">$ sudo systemctl reload-daemon\n$ sudo systemctl start sensemessaged<\/pre>\n<p>\u505c\u6b62<\/p>\n<pre class=\"lang:sh decode:true \">$ sudo systemctl start sensemessaged<\/pre>\n<p>&nbsp;<\/p>\n<p>\u81ea\u52d5\u3067\u8d77\u52d5\u3059\u308b\u3088\u3046\u306b\u8a2d\u5b9a<\/p>\n<pre class=\"lang:sh decode:true\">$ sudo systemctl enable\u00a0sensemessaged.service<\/pre>\n<p>pid\u30d5\u30a1\u30a4\u30eb\u306e\u7f6e\u304d\u5834\u6240\u7b49\u3001\u304a\u4f5c\u6cd5\u304c\u3042\u308b\u3068\u306f\u601d\u3044\u307e\u3059\u304c\u3001\u4eca\u306e\u6240\u306f\u3053\u308c\u3067OK\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30db\u30c3\u30bf\u30e9\u30ab\u30b7\u306eSenseHAT\u3092\u52d5\u304b\u3057\u3066\u307f\u308b\u3002 LED\u3078\u6642\u8a08\u3092\u4e0a3\u6bb5\u306b\u8868\u793a\u30012\u9032\u6570\u8868\u793a\u306a\u306e\u3067\u53ef\u8aad\u6027\u306f\u5168\u304f\u306a\u3057\u3002 4\u6bb5\u76ee\u306f\u30e1\u30e2\u30ea\u4f7f\u7528\u7387\u3001\u6b8b\u308a4\u6bb5\u3092CPU\u30b3\u30a2\u306e\u4f7f\u7528\u7387\u3092\u8868\u793a\u3057\u3066\u307f\u307e\u3057\u305f\u3002 Rasbian Streach\u306b\u306fS [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[316,16],"tags":[230,318,326,325],"class_list":["post-8767","post","type-post","status-publish","format-standard","hentry","category-scripts","category-toys","tag-python","tag-raspberry-pi","tag-sensehat","tag-systemctl"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p8YSE4-2hp","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/leavebehind.mydns.jp\/wordpress\/wp-json\/wp\/v2\/posts\/8767","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/leavebehind.mydns.jp\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/leavebehind.mydns.jp\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/leavebehind.mydns.jp\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/leavebehind.mydns.jp\/wordpress\/wp-json\/wp\/v2\/comments?post=8767"}],"version-history":[{"count":15,"href":"http:\/\/leavebehind.mydns.jp\/wordpress\/wp-json\/wp\/v2\/posts\/8767\/revisions"}],"predecessor-version":[{"id":8873,"href":"http:\/\/leavebehind.mydns.jp\/wordpress\/wp-json\/wp\/v2\/posts\/8767\/revisions\/8873"}],"wp:attachment":[{"href":"http:\/\/leavebehind.mydns.jp\/wordpress\/wp-json\/wp\/v2\/media?parent=8767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/leavebehind.mydns.jp\/wordpress\/wp-json\/wp\/v2\/categories?post=8767"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/leavebehind.mydns.jp\/wordpress\/wp-json\/wp\/v2\/tags?post=8767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}