Python Forum
Sound Approach for Running a Shell Command?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sound Approach for Running a Shell Command?
#4
You've got a couple of choices here.

The only tricky bit is that you can't give all the info to "at" on its command line, you have to feed it via stdin or from a file. You can give stdin directly from your script via a pipe, or you can do it from an invoked shell.

"at" jobs run on the system, not the shell. The shell is just used for invoking normally at a CLI. You don't need it, and when posssible I avoid interacting with it because it can do powerful things that you're not expecting. But in this case it may be simpler.

Since I don't know of any python modules that directly interface with "at", I'd just use subprocess to run the command, and open a pipe to the subprocess for the necessary commands.

^D is a signal to your keyboard driver that you want to close the file down. In general when you're not using the keyboard, you just close the file. You don't send an explicit EOF. Also, most external communication requires a bytes object, not a str, so it has to be encoded.

Example 1, use a process and a pipe for commands
import subprocess
at_command = "at now + 1 hour".split()
p = subprocess.Popen(at_command, stdin=subprocess.PIPE)
p.communicate("/bin/ls > /tmp/foo\n/bin/ls /tmp/ > /tmp/bar\n".encode())
Example 2, use the shell. Note since the shell is doing all the work here, we pass the shell a string and it parses out everything.
import subprocess
at_command = "echo '/bin/ls > /tmp/foo' | at now + 1 hour"
p = subprocess.Popen(at_command, shell=True).communicate()
Reply


Messages In This Thread
RE: Sound Approach for Running a Shell Command? - by bowlofred - Dec-08-2020, 09:24 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Advice needed on how to approach this problem... sawtooth500 1 321 Apr-06-2024, 01:55 PM
Last Post: snippsat
  Open a dos shell for ssh command martinmistere 6 1,826 Jun-07-2022, 12:24 PM
Last Post: martinmistere
  batch file for running python scipt in Windows shell MaartenRo 2 1,977 Jan-21-2022, 02:36 PM
Last Post: MaartenRo
  Need feedback on my approach for python dashboard for Asana pashtett 0 1,360 Nov-24-2020, 11:51 AM
Last Post: pashtett
  Running Python in Command Prompt Anwar 3 3,158 Nov-15-2020, 03:15 PM
Last Post: snippsat
  Error when running mktorrent subprocess command pythonnewbie138 4 3,949 Sep-16-2020, 01:55 AM
Last Post: pythonnewbie138
  Error: How to to close and restart your shell after running 'conda init' angelica 3 10,344 May-27-2020, 10:00 AM
Last Post: snippsat
  Approach to creating Audit table pynewbie 4 3,928 Feb-24-2020, 06:12 PM
Last Post: pynewbie
  Running linux command line apps... dbrdh 0 1,691 Jan-30-2020, 01:14 PM
Last Post: dbrdh
  list approach due nested order 3Pinter 6 2,913 Oct-07-2019, 01:49 PM
Last Post: 3Pinter

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020