Send terminal input data when restarting a program (python)
<p>My script asks for input from the terminal first thing:</p>

<pre><code>ans = raw_input("Do thing A (1) / Do thing B (2)")
</code></pre>

<p>Then runs the code and restarts itself when an exception arises.</p>

<pre><code>def restart_program():

    python = sys.executable
    os.execl(python, python, * sys.argv)
</code></pre>

<p>The problem is I would need a human to input the option again so I tried this:</p>

<pre><code>def restart_program():

    python = sys.executable
    os.execl(python, python, '1', * sys.argv)
</code></pre>

<p>But it didn't work. How can I send the option when restarting?</p>

<p>like others mentioned in comments, try to run your python script via shell script using while loop, smthng like (first run wo cmd line arguments):</p>

<pre><code>arg=''
while true
do
    python restart.py $arg
    arg='1'
    sleep 1800 
done
</code></pre>

<p>and in your python code check if argument was provided:</p>

<pre><code>try:
    ans = sys.argv[1]
except IndexError:
    ans = raw_input("Do thing A (1) / Do thing B (2)")
</code></pre>

<p>You just need to exit your program after exception you are handling raises not to restart it from your code.</p>

python terminal restart