วันพฤหัสบดีที่ 18 พฤษภาคม พ.ศ. 2560

วิธีใช้งาน Crontab (Cron Jobs) บน Raspberry

Cron GUI

A graphical application for Cron is available by installing the gnome-schedule package:
 
sudo apt-get install gnome-schedule
Crontab คือคำสั่งที่จะทำงานตามเวลาที่กำหนด ซึ่งจะช่วยทำให้เราสะดวกขึ้นมากในการที่จะต้องสั่งงานบางอย่างซ้ำๆ กันหลายๆ ครั้ง อาจจะเป็นทุกๆ ชั่วโมง ทุกๆ วัน หรือ ทุกๆ เดือน เช่นการสั่งให้ Server ทำ Backup ทุกๆ สัปดาห์

คำสั่งและ option ของ crontab มีดังนี้
#crontab filename การนำเอาคำสั่ง crontab เข้ามาจาก ไฟล์อื่น
#crontab -e แก้ไข crontab ปัจจุบัน (ส่วนมากเราจะเพิ่มคำสั่งลงไปในนี้เลย)
#crontab -l ดูคำสั่ง crontab ทั้งหมดที่มีอยู่
#crontab -r ลบคำสั่ง crontab ที่มีทั้งหมด
#crontab -u user เป็นคำสั่งของผู้ดูแลระบบเท่านั้น เพื่อใช้ดู แก้ไข ลบ crontab ของ user แต่ละคน

เมื่อเรียกคำสั่งตามข้างบนแล้ว crontab จะเข้าสู่ระบบการ กำหนด หรือแก้ไข ซึ่งการกำหนด หรือแก้ไขนี้จะเหมือนกับการใช้งาน vi

รูปแบบของคำสั่ง crontab มีทั้งหมด 6 fields ดังนี้
1 = minute มีค่า 0 - 59 เวลาเป็นนาที จะสั่งให้คำสั่งที่กำหนดทำงานทันที่เมื่อถึงนาทีที่กำหนด
2 = hour มีค่า 0 - 23 เวลาเป็นชั่วโมง จะสั่งให้คำสั่งที่กำหนดทำงานทันที่เมื่อถึงชั่วโมงที่กำหนด
3 = day มีค่า 1 - 31 เวลาเป็นวัน จะสั่งให้คำสั่งที่กำหนดทำงานทันที่เมื่อถึงวันที่กำหนด
4 = month มีค่า 1 - 12 เวลาเป็นเดือน จะสั่งให้คำสั่งที่กำหนดทำงานทันที่เมื่อถึงเดือนที่กำหนด
5 = weekday มีค่า 0 - 6 วันขะงแต่ละสัปดาห์ มีค่าดังนี้ (อาทิตย์ = 0, จันทร์ = 1, อังคาร = 2, พุธ = 3, พฤหัส = 4, ศุกร์ = 5 ,เสาร์ = 6 )
6 = command คำสั่ง เราสามารถกำหนดคำสั่งได้มากมาย รวมทั้ง script ต่าง ๆ ตามที่เราต้องการ

โดยสามารถจำเป็นรูปแบบง่ายได้ดังนี้
* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)


เริ่มเพิ่ม Cron Jobs โดยใช้คำสั่ง
crontab -e

จากนั้นเพิมพ์คำสั่งดังต่อไปนี้ได้เลย
1 2 3 4 5 /path/to/script.sh

ยกตัวอย่างคำสั่ง Crontab รูปแบบต่างๆ

การสั่งให้ /path/to/command ทำงานเมื่อเวลาผ่านไป 5นาที หลังเที่ยงคืน ในทุกๆ วัน
5 0 * * * /path/to/command

การสั่งให้ /path/to/script.sh ทำงานเวลา 14.25น. ของวันที่ 1 ในทุกๆ เดือน
15 14 1 * * /path/to/script.sh

การสั่งให้ /scripts/phpscript.php ทำงานเวลา 22.00น. ในทุกๆ วัน ยกเว้นวันเสาร์ อาทิตย์
0 22 * * 1-5 /scripts/phpscript.php

โดยปกติแล้วเมื่อ Cron Jobs ทำงานระบบจะส่ง E-Mail เพื่อแจ้งให้ Server Admin หรือ User ที่ตั้ง Cron Jobs ทราบว่า Cron Jobs ทำงานเรียบร้อยหรือติดปัญหาอย่างไร

หากต้องการให้ Cron Jobs ส่ง E-Mail ไปยังบัญชีที่เราตั้งค่าไว้เราสามารถเพิ่มคำสั่งได้ดังนี้
MAILTO="admin@domain.com"
0 3 * * * /root/backup.sh


หากเราไม่ต้องการให้ระบบส่ง E-Mail เราสามารถตั้งค่า Cron Jobs ได้โดยเพิ่ม /dev/null 2>&1 ตามหลัง command ที่จะสั่งให้ทำงาน ยกตัวอย่างเช่น
0 1 5 10 * /path/to/script.sh >/dev/null 2>&1

ทำการ save แล้วสั่ง
/etc/init.d/crond restart

Run a task on reboot

To run a command every time the Raspberry Pi starts up, write @reboot instead of the time and date. For example:
@reboot python /home/pi/myscript.py
This will run your Python script every time the Raspberry Pi reboots. If you want your command to be run in the background while the Raspberry Pi continues starting up, add a space and & at the end of the line, like this:
@reboot python /home/pi/myscript.py &
 
เช่น
รันคำสั่ง reboot ทุก 5 นาที
*/5 * * * * /sbin/reboot
หรือ
*/5 * * * * reboot
 รันคำสั่ง reboot ทุก 16:55
55 16 * * * /sbin/reboot

รันคำสั่ง reboot ทุก วันที่ 15 ของทุกเดือน
* * 15 * * /sbin/reboot 

Profiling and Memory Checking Tools QT

Profiling and Memory Checking Tools

This is a collection of tools for profiling and memory checking. It's mostly a summary of programs/libraries mentioned in the forums, notably in threads Advice on checking for memory leaks and dangling resources? and Other tools for Qt Development

Linux, Mac OS X

Valgrind is the tool of choice for analyzing a variety of issues on Linux and Mac OS X. It uses individual tools for specific analysis like profiling and memory checking. Qt Creator's Analyze mode integrates Valgrind, and provides both memory checking and profiling functionality from within the IDE. Valgrind needs to be installed for that to work. Valgrind is not available on Windows, but since memory issues are usually not platform dependent, analysis can be done on Linux or Mac OS X as well.
There is a tutorial on how to debug C++ code with Valgrind on Linux at http://devmentor.posterous.com/debugging-with-valgrind
Visualizer for Valgrind profiling results
Qt4-based GUI for the Valgrind 3.6.x and 3.7.x series
visualizer for the massif (one of the valgrind tools) output

Windows

  • AQTime Pro (Commercial - €539) Find Issues & Memory Leaks with Application Runtime Analysis & Performance Profiling
  • Borland Bounds Checker (Commercial)
  • Deleaker (Commercial - $99) Deleaker is a tool for C++ developers who want to find all possible known leaks in their projects. Detects memory, GDI and other leaks.
  • Dr.Memory (Open-source) v1.9.0 | 2015-10-02 | Memory monitoring tool
  • Intel Inspector XE (Commercial - $1599) Memory and Thread Debugger
  • Luke Stackwalker (Open-source) v0.9.9 | 2010-12-22 | CPU profiler based on sampling
  • UNICOM Systems PurifyPlus (Commercial) PurifyPlus is a runtime analysis tool suite that monitors your program as it runs and reports on key aspects of its behavior.
  • Visual Leak Detector for Visual C++ 2008-2015 (Open-source) v2.4rc2 | 2014-04-06 | Visual Leak Detector is a free, robust, open-source memory leak detection system for Visual C++.
  • Very Sleepy (Open-source) v0.90 | 2014-12-23 | CPU profiler based on sampling (can't be attached to Qt processes on Windows 10 x64)
  • Visual Studio Profiler (Free in Visual Studio 2013 and 2015 Community Editions) CPU Sampling, Instrumentation, .NET Memory allocation, Resource Contention
  • MTuner (Free) MTuner utilizes a novel approach to memory profiling & analysis, keeping entire time-based history of memory operations.
  • Memory Leak Detection Tool (Free, Open source code) High performance memory leak detection tool, 32/64-bit architectures are supported

set timedate rasberry pi

sudo date -s "Mon May  15 16:19:00 UTC+7 2016"

วันอังคารที่ 16 พฤษภาคม พ.ศ. 2560

วันพุธที่ 10 พฤษภาคม พ.ศ. 2560

Share Raspberry Pi files and folders with Windows machines

Installing Samba:
sudo apt-get update
sudo apt-get install samba
sudo nano /etc/samba/smb.conf


Configuration:
[global]
netbios name = RP2
server string = The Pi File Center
workgroup = WORKGROUP
hosts allow =
socket options = TCP_NODELAY IPTOS_LOWDELAY SO_RCVBUF=65536 SO_SNDBUF=65536
remote announce =
remote browse sync =

[HOMEPI]
path = /home/pi
comment = No comment
browsable = yes
read only = no
valid users =
writable = yes
guest ok = yes
public = yes
create mask = 0777
directory mask = 0777
force user = root
force create mode = 0777
force directory mode = 0777
hosts allow =

[HOME ASSISTANT]
path = /var/opt/homeassistant
comment = No comment
browsable = yes
read only = no
valid users =
writable = yes
guest ok = yes
public = yes
create mask = 0777
directory mask = 0777
force user = root
force create mode = 0777
force directory mode = 0777
hosts allow =


Creating Samba User:
sudo smbpasswd -a pi
type password, press enter

Restart Samba Service:
sudo service smbd restart