简体   繁体   中英

Shell Script: execute & repeat a python program that needs extra input

I have to run this python command: python manage.py rebuild_index at a certain time and rerun again. Therefore, I am trying the shell script to run it. However, when run this python command, I have to choose y to continue.

在此处输入图片说明

my run_rebuild.sh :

#!/bin/sh
python manage.py rebuild_index

Moreover, how can I let the run_rebuild.sh rerun again after every 60 seconds?

尝试使用管道 (将输出从echo重定向到pythonstdin ):

echo "y" | python manage.py rebuild_index

You can use yes for this:

yes | python /manage.py rebuild_index

One way to run your script every minute:

while yes | python /manage.py rebuild_index; do
  sleep 60
done

The above will run your script every 60 second until it fails.

You can use echo "y" (as @LogicStuff said) and cron jobs to accomplish this.

Create a script file (let's say your_script.sh ) and add your command to it

echo "y" | python manage.py rebuild_index

To use cron you can use the following steps:-

crontab -e

This will open up the cronjob file in your default editor, add the following line to the file

* * * * * sh /path/to/your_script.sh

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM