#/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