Python's more elegant way to add variables to the crontab

Source: Internet
Author: User

#/usr/bin/python Import osname = input ("Please enter a module name:") #这里是输入具体的模块名, product Manager each time the module name is different os.environ[' name '] = str (name) #把py Thon variable into shell variable Os.system (' echo ' */1 * * * * source/etc/profile && python/share/yunwei/script/watchdog/open/$ name/$name +check.py >/dev/null 2>&1 ">>/var/spool/cron/root")

Above is the Os.system method, which is to write the script with pure shell thinking.

Execution effect

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/88/32/wKioL1frrqWxXNwOAAA7BXfa9ZY257.png "title=" 1.png " alt= "Wkiol1frrqwxxnwoaaa7bxfa9zy257.png"/>

It is true that the name is converted into a variable, where Jordan appears in the path and is added to the crontab.

But the following approach is more elegant.


#/usr/bin/pythonimport osname = input ("Please enter a module name:") cmd_template = "echo ' */1 * * * * * source/etc/profile && Pytho n/share/yunwei/script/watchdog/open/{name}/{name}check.py >/dev/null 2>&1 ' >>/var/spool/cron/root "Os.system (Cmd_template.format (name=name))

The whole effect

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/88/36/wKiom1frr0yzu6AVAABQQ0y_0gE111.png "title=" 1.png " alt= "Wkiom1frr0yzu6avaabqq0y_0ge111.png"/>

This method not only more python thinking, but also solve the last "+" problem, more aesthetic, higher satisfaction!!

This article is from "Life is waiting for Gordo" blog, please make sure to keep this source http://chenx1242.blog.51cto.com/10430133/1857489

Python's more elegant way to add variables to the crontab

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.