1

I need to integrate " echo /bin/meteo | at 23:00 today " in to a python script.

In the python script the command "at 23:00 today" should call the bash script /bin/meteo

I did install plumbum and intergrated this in my python scrip.

from plumbum.cmd import echo, grep

Unfortunately I have no clue how to proceed from here.

I tryed:

#!/usr/bin/python2.7

    if pfd.input_pins[0].value ==0:
        cmd = "echo /bin/meteo | at 06:36 today"
        subprocess.Popen(cmd, shell=True)

but the lights in /bin/meteo are randomly swiching on and off (not blinking as they should)

They do it from 06:36 until 06:37 and not only 5 times.

/bin/meteo:

#!/bin/bash -x

for i in {1..5}; do   #blink 5x
echo -n -e "\x37\x00\x55" | nc -u -q 1 192.168.0.6 8899 #Zone 3 on
sleep 0.1
echo -n -e "\x3A\x00\x55" | nc -u -q 1 192.168.0.6 8899 #Zone 3 off
done
sleep 0.1
exit
1
  • 1
    subprocess.popen will do what you want. Commented Jul 6, 2014 at 18:56

2 Answers 2

2

subprocess.Popen will run the command:

import subprocess
cmd = "echo /bin/meteo | at 23:00 today "
subprocess.Popen(cmd, shell=True)

Execute a child program in a new process. On Unix, the class uses os.execvp()-like behavior to execute the child program. On Windows, the class uses the Windows CreateProcess() function. The arguments to Popen are as follows.

args should be a sequence of program arguments or else a single string. By default, the program to execute is the first item in args if args is a sequence. If args is a string, the interpretation is platform-dependent and described below. See the shell and executable arguments for additional differences from the default behavior. Unless otherwise stated, it is recommended to pass args as a sequence.

It is not totally clear what you want but you can run any commands like:

In [9]: cmd = "date"

In [10]: subprocess.call(cmd, shell=True)
Sun Jul  6 22:30:47 IST 2014

Or using sudo:

import subprocess
cmd = "sudo which python"
my_pass="xxxx"
subprocess.call('echo {} | sudo -S {}'.format(my_pass,cmd), shell=True)
In [29]: subprocess.call('echo {} | sudo -S {}'.format(my_pass,cmd), shell=True)
/usr/local/bin/python
Out[29]: 0
Sign up to request clarification or add additional context in comments.

Comments

1

With Python 3.4, it's easy to call a command and exchange input/output in a bulk:

subprocess.check_output(["at", "23:00", "today"], input="/bin/meteo")

Therefore in this very case, shell=True shouldn't be needed as we just call the at command with arguments and give it the script on input.

With older versions of python, this needs to be rewritten as:

process = subprocess.Popen(["at", "23:00", "today"])
process.communicate(input="/bin/meteo")

With the plumbum module, you could instead use:

from plumbum.cmd import at, echo
(echo["/bin/meteo"] | at["23:30", "today"])()

But I don't believe that it's very useful.

3 Comments

@PadraicCunningham: It depends on the way you use it. When you don't need to affect stdin/stdout as in the shell=True case, there's no reason to use subprocess.Popen. When you're using Python 3.4, subprocess.check_output is good enough. Only when you want to supply your own input and you're using an older version of Python, you have to use it.
so it is necessary for python 2?
@PadraicCunningham: In this case, you need the Popen with Python 2 but you also need to use it correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.