Reputation: 7629
I have a Python script, say test.py
which interacts with the user everytime it is executed.
test.py
#!/usr/bin/env python3
def foo():
a = input()
print("Hello, {}".format(a))
if __name__ == '__main__':
foo()
I want to run this script every 5 minutes. So, I tried adding it to my crontab
but it seems that this doesn't work as I am not prompted to enter an input.
I have tested my crontab
and it is working fine.
Should I use libraries such as schedule
for this task?
What would be the ideal way of going about in this scenario?
NOTE: I want a cross platform solution which works on MacOSX and GNU/Linux.
Upvotes: 0
Views: 1509
Reputation: 1
You can get a script and use that in the crontab:
#!/bin/bash
/usr/bin/python3 /home/user/project/test.py << EOF
some input here
EOF
Your crontab will look like
* * * * * /home/user/project/test.sh >> /home/user/project/LOG 2>&1
Upvotes: 0
Reputation: 1929
If it is running via cron or schedule, it will not be running on your terminal, it will run as a background process. Therefore, it cannot be interactive and you will not be prompted.
If you just want to put a delay of 5 minutes, have you looked at the Linux "sleep" command or using python's "time.sleep"? In this way you can keep a terminal open and just have the job run in intervals.
To keep it simple:
#!/usr/bin/env python3
import time
def foo():
a = input()
print("Hello, {}".format(a))
if __name__ == '__main__':
while True:
foo()
time.sleep(300) # Sleeps 300 seconds
Upvotes: 0
Reputation: 870
its not possible to have cron interact interactively on your server.
Delink your application: Setup a db (like MySQL), have test.py run via cron every 5 mins to check if a new entry is made in the db: then, have a local application that saves user prompt information to your db.
Upvotes: 0
Reputation: 1079
Your could try the python module pexpect (http://pexpect.sourceforge.net/doc/) -- the usage is something like :
import pexpect
child = pexpect.spawn(script/python file etc...)
child.expect('Here goes the prompt you would expect')
child.sendline('What you want to send to it')
Upvotes: 1