Python timer usage + get script where absolute path + define log format + Shell launches script directly to post

Source: Internet
Author: User
Tags python script

Python timer usage + get script where absolute path + define log format test code

If you write a script with a timer in Python, if the script also read the configuration file, then the configuration file path if you write to die, one day to change the directory, you need to change the script configuration file path, and every time the script is located in the path with nohup boot to the background is very troublesome.
Using Os.path.split (Os.path.realpath (sys.argv[0])) [0] to get the absolute path of the file, the configuration file is also thrown to its peers, so that it can be started anywhere, once and for all ~ ~ ~

This usage station is commonly used in the operation of the thinking, put to any path can be called under any path, solve the problem of the path is not correct.

Python script
Vim test_parameters.py#!/usr/bin/python#-*-coding:utf-8-*-######################## #import threadingimport Loggingimport timeimport os,sys# Gets the path of the script location_path = Os.path.split (Os.path.realpath (sys.argv[0))) [0]# Defines the log format logging.basicconfig (level=logging.                    DEBUG, format= '% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s------->% (Message) s ', Datefmt= '%a,%d%b%Y%h:%m:%s ', filename= '%s/logs/test_timer.log '%location_path, # will log Print to Filemode= ' a ' at the path where the script is located #测试带参数的函数用法, the parameters of the function must be in [] def test_parameters (path): Logging.info ("This is the path of this script:% S "% path" Global Timer timer = Threading. Timer (Test_parameters,[path]) #每60秒运行一次 Timer.start () # Use Def test_nop () without parameters: Logging.info ("Hello world ...") Globa L Timer timer = Threading. Timer (3.0,TEST_NOP) #每三秒运行一次 Timer.start () if __name__ = = "__main__": Timer = Threading. Timer (10,test_parameters,[location_path]) Timer.start () #测试Test_Nop () #timer = Threading.Timer (10,TEST_NOP) # starts running Test_nop () #timer after 10 seconds. Start () 
Shell script Start stop
#!/bin/bashpypath=$(cd "$(dirname "$0")"; pwd)    # 获取脚本所在路径PID=`ps -ef | grep test_parameters | awk ‘!/grep/{print $2}‘`case $1 in  start)    nohup python $pypath/test_parameters.py &  ;;  stop)    kill -9 $PID  ;;  restart)    kill -9 $PID    sleep 3    nohup python $pypath/test_parameters.py &  ;;  *)    echo "$0 [ start | stop | restart ]"    exit 1  ;;esac

Python timer usage + get script where absolute path + define log format + Shell launches script directly to post

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.