วันอังคารที่ 28 เมษายน พ.ศ. 2558

[HOW TO] Generate word ด้วย Python script

ไม่มีความคิดเห็น
Generate word ด้วย Python script(หัดทำ)
วิธีรัน 
1.เข้า Terminal ด้วยสิทธิ์ Root
2.เมื่อสร้างไฟล์เสร็จให้ทำการเปลี่ยนไฟล์ให้ execute ได้ ด้วย
chmod +x genword.py
3. รันด้วยคำสั่ง 
./genword.py -f <fileName.txt> -n <line Of Number> -w <word Generate>



#!/usr/bin/python
######################################################
#____________________________________________________#
#_______________Mr.Pornmongkon Pongsai_______________#
#________________Computer Engineering________________#
#_____________Generate Word for Project______________#
#____________Create Date : 28 April 2015_____________#
#____________________________________________________#
######################################################
import sys
import getopt

def main(argv):
 fileName = 'default'
 lineNumber = 0
 wordGen = 'default'
 try:
  opts, args = getopt.getopt(argv,"hf:n:w:",["file=","number=","word="])
 except getopt.GetoptError:
  print 'Help : genword.py -f  -n  -w '
  sys.exit(2)
 for opt, arg in opts:
  if opt == '-h':
   print 'Help : genword.py -f  -n  -w '
   sys.exit()
  elif opt in ("-f", "--file"):
   fileName = arg
  elif opt in ("-n", "--number"):
   lineNumber = int(arg)
  elif opt in ("-w", "--word"):
   wordGen = arg  
 print '[log] : generate word start!'
 dataFile = open(fileName, 'w')
 i = 0
 while ( i < lineNumber ):
  dataFile.write(wordGen+'\n')
  i = i + 1
 dataFile.close()
 print '[log] : generate word finish!'
if __name__ == "__main__":
 print '######################################################'
 print '#____________________________________________________#'
 print '#_______________Mr.Pornmongkon Pongsai_______________#'
 print '#________________Computer Engineering________________#'
 print '#_____________Generate Word for Project______________#'
 print '#____________Create Date : 28 April 2015_____________#'
 print '#____________________________________________________#'
 print '######################################################'
 print '!!!-h for help!!!'
 main(sys.argv[1:])

วันพุธที่ 15 เมษายน พ.ศ. 2558

[HOW TO] Check status service linux with Python

ไม่มีความคิดเห็น
เช็คสถานะ service linux ด้วย Python(หัดทำ)
    

     เพื่อเวลาที่ server ทำงานแล้วเกิดบาง service down เราจะได้ไม่ต้องมานั่ง start stop restart พวก service บ่อยๆ 
วิธีรัน 
1.เข้า Terminal ด้วยสิทธิ์ Root
2.เมื่อสร้างไฟล์เสร็จให้ทำการเปลี่ยนไฟล์ให้ execute ได้ ด้วย
chmod +x filename.py
3. รันด้วยคำสั่ง 
./filename.py 

#!/usr/bin/python
import commands
import time
import os
while True:
 #find process
        pc = commands.getoutput('ps -aux | grep nginx | grep -v grep').strip()

        if(len(pc) > 0):
                print 'process running. skip'
        else:
  print 'process not running'
                #stop program
                os.system('sudo service nginx stop')
                #start program
         os.system('sudo service nginx start')
  print 'process started'
 #Check every 1 minute
        time.sleep(1 * 60)