I read through a lot of the answers but can't figure out how to execute a command which I currently execute using cron
from subprocess
or something better?
# cron command
00 16 * * 1-5 DISPLAY=:10 /path/to/shell/script.sh > log/file.log 2>&1
The DISPLAY
is Xvfb
.
CodePudding user response:
Set the environment variable using os.environ
, then run the command using the subprocess
module.
import subprocess
import os
os.environ['DISPLAY'] = ':10'
with open('log/file.log') as out:
subprocess.Popen(['/path/to/shell/script.sh'], stdout=out, stderr=subprocess.STDOUT)
CodePudding user response:
Using the env=
argument to subprocess.Popen
, you can change the environment only for the child process, without modifying the environment of the Python process itself:
import subprocess
import os
with open('log/file.log') as out:
subprocess.Popen(['/path/to/shell/script.sh'],
env=dict(os.environ, DISPLAY=":10"),
stdout=out, stderr=subprocess.STDOUT)